This is a Java tutorial for the Postgresql database. It covers the basics of Postgresql programming with Java. In this tutorial,we use the Postgresql JDBC Driver driver. It is the official JDBC driver for Postgresql. The examples were created and tested on Ubuntu Linux. There is a similar MysqL Java tutorial,Spring JdbcTemplate tutorial,or Apache Derby tutorial on ZetCode.
JDBC
JDBC is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases. From a technical point of view,the API is as a set of classes in the java.sql package. To use JDBC with a particular database,we need a JDBC driver for that database.
About Postgresql database
Postgresql is a powerful,open source object-relational database system. It is a multi-user database management system. It runs on multiple platforms including Linux,FreeBSD,Solaris,Microsoft Windows,and Mac OS X. Postgresql is developed by the Postgresql Global Development Group.
Before we start
We need to install postgresql package. We need to install the JDK,Java Development Kit,for compiling and running Java programs. Finally,we need the Postgresql Java driver. If we are using NetBeans IDE,than we have already the driver at hand. Inside the Projects tab,right click on the Libraries node and select Add Library option. From the list of options,select Postgresql JDBC Driver. Note that the driver may be older. Older driver may cause problems on newer Postgresql versions. In such a case,we can add the newest driver from by choosing add JAR/Folder and selecting a driver from the filesystem.
NetBeans project libraries
Figure: NetBeans project libraries
If we want to compile the examples from the command line,go to the site http://jdbc.postgresql.org/download.html and download the latest Postgresql Java Driver.
$ javac zetcode/Version.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode/Version
Postgresql 9.1.2 on i686-pc-linux-gnu,compiled by
gcc-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1,32-bit
Assuming that we have put the driver JAR file into the lib directory and using package zetcode,we compile and run the first example this way.
If we don’t already have Postgresql installed,we must install it.
$ sudo apt-get install postgresql
This command installs the Postgresql server and varIoUs other packages.
$ sudo update-rc.d -f postgresql remove
Removing any system startup links for /etc/init.d/postgresql …
/etc/rc0.d/K21postgresql
/etc/rc1.d/K21postgresql
/etc/rc2.d/S19postgresql
/etc/rc3.d/S19postgresql
/etc/rc4.d/S19postgresql
/etc/rc5.d/S19postgresql
/etc/rc6.d/K21postgresql
If we install the Postgresql database from packages,it is automatically added to the start up scripts of the operating system. If we are only learning to work with the database,it is unnecessary to start the database each time we boot the system. The above command removes any system startup links for the Postgresql database.
$ /etc/init.d/postgresql status
Running clusters: 9.1/main
$ service postgresql status
Running clusters: 9.1/main
We check if the Postgresql server is running. If not,we need to start the server.
$ sudo service postgresql start
* Starting Postgresql 9.1 database server [ OK ]
On Ubuntu Linux we can start the server with the service postgresql start command.
$ sudo service postgresql stop
[sudo] password for janbodnar:
* Stopping Postgresql 9.1 database server [ OK ]
We use the service postgresql stop command to stop the Postgresql server.
Next,we are going to create a new database user and a new database. We will use the createuser and createdb commands. These are installed with the Postgresql database.
$ sudo -u postgres createuser -W user12
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
Password:
We create a new database user. The user is called user12 and it is created with a password (-W option).
$ sudo -u postgres createdb testdb -O user12
We create a new testdb database. We will use this database throughout the tutorial. We set the new user to be the owner of the database (the -O option).
Postgresql version
If the following program runs OK,then we have everything installed OK. We check the version of the Postgresql server.
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Version {
public static void main(String[] args) { Connection con = null; Statement st = null; ResultSet rs = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,user,password); st = con.createStatement(); rs = st.executeQuery("SELECT VERSION()"); if (rs.next()) { System.out.println(rs.getString(1)); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(Version.class.getName()); lgr.log(Level.SEVERE,ex.getMessage(),ex); } finally { try { if (rs != null) { rs.close(); } if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(Version.class.getName()); lgr.log(Level.WARNING,ex); } } }
}
We connect to the database and get some info about the Postgresql server.
String url = “jdbc:postgresql://localhost/testdb”;
This is the connection URL for the Postgresql database. Each driver has a different Syntax for the URL. In our case,we provide a host,a port and a database name.
con = DriverManager.getConnection(url,password);
We establish a connection to the database,using the connection URL,user name and password.
st = con.createStatement();
The createStatement() method of the connection object creates a Statement object for sending sql statements to the database.
rs = st.executeQuery(“SELECT VERSION()”);
The executeQuery() method of the connection object executes the given sql statement,which returns a single ResultSet object. The ResultSet is a table of data returned by a specific sql statement.
if (rs.next()) {
System.out.println(rs.getString(1));
}
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next() method moves the cursor to the next row. If there are no rows left,the method returns false. The getString() method retrieves the value of a specified column. The first column has index 1.
} catch (sqlException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.SEVERE,ex);
}
In case of an exception,we log the error message. For this console example,the message is displayed in the terminal.
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
…
Inside the finally block,we close the database resources. We also check if the objects are not equal to null. This is to prevent null pointer exceptions. Otherwise we might get a NullPointerException,which would terminate the application and might leave the resources not cleaned up.
} catch (sqlException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.WARNING,ex);
}
We log an error message when the resources could not be closed.
$ javac zetcode/Version.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode.Version
Postgresql 9.1.2 on i686-pc-linux-gnu,compiled by gcc-4.6.real
(Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1,32-bit
This is a sample output of the program.
Creating and populating tables
Next we are going to create database tables and fill them with data. These tables will be used throughout this tutorial.
DROP TABLE IF EXISTS books,authors,testing,images;
CREATE TABLE IF NOT EXISTS authors (
id serial PRIMARY KEY,
name VARCHAR(25)
);
CREATE TABLE IF NOT EXISTS books (
id serial PRIMARY KEY,
author_id INT references authors(id),title VARCHAR(100)
);
CREATE TABLE IF NOT EXISTS testing(id INT);
CREATE TABLE IF NOT EXISTS images(id serial,data bytea);
INSERT INTO authors(id,name) VALUES(1,‘Jack London’);
INSERT INTO authors(id,name) VALUES(2,‘Honore de Balzac’);
INSERT INTO authors(id,name) VALUES(3,‘Lion Feuchtwanger’);
INSERT INTO authors(id,name) VALUES(4,‘Emile Zola’);
INSERT INTO authors(id,name) VALUES(5,‘Truman Capote’);
INSERT INTO books(id,author_id,title) VALUES(1,1,‘Call of the Wild’);
INSERT INTO books(id,title) VALUES(2,‘Martin Eden’);
INSERT INTO books(id,title) VALUES(3,2,‘Old Goriot’);
INSERT INTO books(id,title) VALUES(4,‘Cousin Bette’);
INSERT INTO books(id,title) VALUES(5,3,‘Jew Suess’);
INSERT INTO books(id,title) VALUES(6,4,‘Nana’);
INSERT INTO books(id,title) VALUES(7,‘The Belly of Paris’);
INSERT INTO books(id,title) VALUES(8,5,‘In Cold blood’);
INSERT INTO books(id,title) VALUES(9,‘Breakfast at Tiffany’);
We have a tables.sql file. It creates four database tables,books,and images. We place a foreign key constraint on the author_id column of the books table. We fill the authors and books tables with initial data.
$ psql testdb
psql (9.1.2)
Type “help” for help.
testdb=#
testdb=# \i tables.sql
DROP TABLE
psql:tables.sql:6: NOTICE: CREATE TABLE will create implicit
sequence “authors_id_seq” for serial column “authors.id”
psql:tables.sql:6: NOTICE: CREATE TABLE / PRIMARY KEY will create
implicit index “authors_pkey” for table “authors”
CREATE TABLE
…
The psql is a terminal-based front-end to Postgresql. We can use it to perfom interactive queries,issue them to Postgresql,and see the query results. Inside the psql tool,we import and execute the tables.sql file.
testdb=# \dt
List of relations
Schema | Name | Type | Owner
——–+———+——-+———–
public | authors | table | janbodnar
public | books | table | janbodnar
public | images | table | janbodnar
public | testing | table | janbodnar
(4 rows)
We check the created tables.
Prepared statements
Now we will concern ourselves with prepared statements. When we write prepared statements,we use placeholders instead of directly writing the values into the statements. Prepared statements increase security and performance.
In Java a PreparedStatement is an object which represents a precompiled sql statement.
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.sqlException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Prepared {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { int id = 6; String author = "Trygve Gulbranssen"; con = DriverManager.getConnection(url,password); String stm = "INSERT INTO authors(id,name) VALUES(?,?)"; pst = con.prepareStatement(stm); pst.setInt(1,id); pst.setString(2,author); pst.executeUpdate(); } catch (sqlException ex) { Logger lgr = Logger.getLogger(Prepared.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(Prepared.class.getName()); lgr.log(Level.SEVERE,ex); } } }
}
We add a new author to the authors table.
String stm = “INSERT INTO authors(id,?)”;
pst = con.prepareStatement(stm);
Here we create a prepared statement. When we write prepared statements,we use placeholders instead of directly writing the values into the statements. Prepared statements are faster and guard against sql injection attacks. The ? is a placeholder,which is going to be filled later. In our case we will fill two values. An integer id and a string name.
pst.setInt(1,id);
A value is bound to the placeholder.
pst.setString(2,author);
Another value is bound to the placeholder.
pst.executeUpdate();
The prepared statement is executed. We use the executeUpdate() method of the statement object when we do not expect any data to be returned. This is when we create databases or execute INSERT,UPDATE,and DELETE statements.
$ javac zetcode/Prepared.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode.Prepared
testdb=# SELECT * FROM authors;
id | name
—-+——————–
1 | Jack London
2 | Honore de Balzac
3 | Lion Feuchtwanger
4 | Emile Zola
5 | Truman Capote
6 | Trygve Gulbranssen
(6 rows)
We have a new author inserted into the table.
For the following two examples,we will use the Testing table. We will execute a normal statement and a prepared statement 1000 times. We check if there is some difference in execution time.
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.sqlException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NotPrepared {
public static void main(String[] args) { Connection con = null; Statement st = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); st = con.createStatement(); for (int i=1; i<=1000; i++) { String query = "INSERT INTO Testing(Id) VALUES(" + 2*i + ")"; st.executeUpdate(query); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(NotPrepared.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(NotPrepared.class.getName()); lgr.log(Level.SEVERE,ex); } } }
}
The first example uses the normal Statement object.
for (int i=1; i<=1000; i++) {
String query = “INSERT INTO Testing(Id) VALUES(” + 2*i + “)”;
st.executeUpdate(query);
}
We build the query and execute it 1000 times.
$ javac zetcode/NotPrepared.java
$ /usr/bin/time java -cp .:lib/postgresql.jdbc4.jar zetcode.NotPrepared
0.81user 0.14system 0:32.27elapsed 2%cpu (0avgtext+0avgdata 89824maxresident)k
960inputs+88outputs (1major+6008minor)pagefaults 0swaps
We use the time command to measure the time which the program ran. Note that we use a standard Linux command,not the built-in bash time command. It took 32s to insert 1000 rows into the table using the Statement object.
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.sqlException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Prepared2 {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); pst = con.prepareStatement("INSERT INTO Testing(Id) VALUES(?)"); for (int i = 1; i <= 1000; i++) { pst.setInt(1,i * 2); pst.executeUpdate(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(Prepared2.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(Prepared2.class.getName()); lgr.log(Level.SEVERE,ex); } } }
}
Now we use the PreparedStatement to do the same task.
pst = con.prepareStatement(“INSERT INTO Testing(Id) VALUES(?)”);
We create the prepared statement using the prepareStatement() method.
for (int i = 1; i <= 1000; i++) {
pst.setInt(1,i * 2);
pst.executeUpdate();
}
We bind a value to the prepared statement,execute it in a loop thousand times.
$ javac zetcode/Prepared2.java
$ /usr/bin/time java -cp .:lib/postgresql.jdbc4.jar zetcode.Prepared2
0.59user 0.11system 0:15.08elapsed 4%cpu (0avgtext+0avgdata 76912maxresident)k
0inputs+64outputs (0major+4941minor)pagefaults 0swaps
This time it took 15s to insert 1000 rows. We have saved 17s using prepared statements.
Retrieving data
Next we will show,how to retrieve data from a database table. We get all data from the authors table.
package zetcode;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Retrieve {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); pst = con.prepareStatement("SELECT * FROM authors"); rs = pst.executeQuery(); while (rs.next()) { System.out.print(rs.getInt(1)); System.out.print(": "); System.out.println(rs.getString(2)); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(Retrieve.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(Retrieve.class.getName()); lgr.log(Level.WARNING,ex); } } }
}
We get all authors from the authors table and print them to the console.
pst = con.prepareStatement(“SELECT * FROM authors”);
rs = pst.executeQuery();
We execute a query that selects all columns from the authors table. We use the executeQuery() method. The method executes the given sql statement,which returns a single ResultSet object. The ResultSet is the data table returned by the sql query.
while (rs.next()) {
System.out.print(rs.getInt(1));
System.out.print(“: “);
System.out.println(rs.getString(2));
}
The next() method advances the cursor to the next record. It returns false,when there are no more rows in the result set. The getInt() and getString() methods retrieve the value of the designated column in the current row of this ResultSet object as an int and String in the Java programming language.
$ javac zetcode/Retrieve.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode/Retrieve
1: Jack London
2: Honore de Balzac
3: Lion Feuchtwanger
4: Emile Zola
5: Truman Capote
We have ids and names of authors printed to the console.
Properties
It is a common practice to put the configuration data outside the program in a separate file. This way the programmers are more flexible. We can change the user,the password or the connection URL without needing to recompile the program. It is especially useful in a dynamic environment,where is a need for a lot of testing,debugging,securing data etc.
In Java,the Properties is a class used often for this. The class is used for easy reading and saving of key/value properties.
db.url=jdbc:postgresql://localhost/testdb
db.user=user12
db.passwd=34klq*
We have a database.properties file,in which we have three key/value pairs. These are dynamically loaded during execution of the program.
package zetcode;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.sql.PreparedStatement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Retrieve2 {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; Properties props = new Properties(); FileInputStream in = null; try { in = new FileInputStream("database.properties"); props.load(in); } catch (IOException ex) { Logger lgr = Logger.getLogger(Retrieve2.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (in != null) { in.close(); } } catch (IOException ex) { Logger lgr = Logger.getLogger(Retrieve2.class.getName()); lgr.log(Level.SEVERE,ex); } } String url = props.getProperty("db.url"); String user = props.getProperty("db.user"); String passwd = props.getProperty("db.passwd"); try { con = DriverManager.getConnection(url,passwd); pst = con.prepareStatement("SELECT * FROM Authors"); rs = pst.executeQuery(); while (rs.next()) { System.out.print(rs.getInt(1)); System.out.print(": "); System.out.println(rs.getString(2)); } } catch (Exception ex) { Logger lgr = Logger.getLogger(Retrieve2.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(Retrieve2.class.getName()); lgr.log(Level.WARNING,ex); } } }
}
We connect to the testdb database and print the contents of the authors table to the console. This time,we load the connection properties from a file. They are not hard coded in the proram.
Properties props = new Properties();
FileInputStream in = null;
try {
in = new FileInputStream(“database.properties”);
props.load(in);
…
The Properties class is created. The data is loaded from the file called database.properties,where we have our configuration data.
String url = props.getProperty(“db.url”);
String user = props.getProperty(“db.user”);
String passwd = props.getProperty(“db.passwd”);
The values are retrieved with the getProperty() method.
Multiple statements
It is possible to execute multiple sql statements in one query.
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Multiple {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); String query = "SELECT id,name FROM authors WHERE Id=1;" + "SELECT id,name FROM authors WHERE Id=2;" + "SELECT id,name FROM authors WHERE Id=3"; pst = con.prepareStatement(query); boolean isResult = pst.execute(); do { rs = pst.getResultSet(); while (rs.next()) { System.out.print(rs.getInt(1)); System.out.print(": "); System.out.println(rs.getString(2)); } isResult = pst.getMoreResults(); } while (isResult); } catch (sqlException ex) { Logger lgr = Logger.getLogger(Multiple.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(Multiple.class.getName()); lgr.log(Level.WARNING,ex); } } }
}
In the code example,we retrieve three rows from the authors table. We use three SELECT statements to get three rows.
String query = “SELECT id,name FROM authors WHERE Id=1;”
+ “SELECT id,name FROM authors WHERE Id=2;”
+ “SELECT id,name FROM authors WHERE Id=3”;
Here we have a query with multiple statements. The statements are separated by a semicolon.
boolean isResult = pst.execute();
We call the execute() method of the prepared statement object. The method returns a boolean value indicating if the first result is a ResultSet object. Subsequent results are called using the getMoreResults() method.
do {
rs = pst.getResultSet();
while (rs.next()) { System.out.print(rs.getInt(1)); System.out.print(": "); System.out.println(rs.getString(2)); } isResult = pst.getMoreResults();
} while (isResult);
The processing of the results is done inside the do/while loop. The ResultSet is retrieved with the getResultSet() method call. To find out if there are other results,we call the getMoreResults() method.
$ javac zetcode/Multiple.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode.Multiple
1: Jack London
2: Honore de Balzac
3: Lion Feuchtwanger
The output of the example. The first three rows were retrieved from the authors table.
Metadata
Metadata is information about the data in the database. Metadata in a Postgresql database contains information about the tables and columns,in which we store data. Number of rows affected by an sql statement is a Metadata. Number of rows and columns returned in a result set belong to Metadata as well.
Metadata in Postgresql can be obtained by calling the getMetaData() method of the result set object or from the information_schema table.
Next we will show,how to print column headers with the data from the database table.
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.sqlException;
import java.util.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ColumnHeaders {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); String query = "SELECT name,title From authors," + "books WHERE authors.id=books.author_id"; pst = con.prepareStatement(query); rs = pst.executeQuery(); ResultSetMetaData Meta = rs.getMetaData(); String colname1 = Meta.getColumnName(1); String colname2 = Meta.getColumnName(2); Formatter fmt1 = new Formatter(); fmt1.format("%-21s%s",colname1,colname2); System.out.println(fmt1); while (rs.next()) { Formatter fmt2 = new Formatter(); fmt2.format("%-21s",rs.getString(1)); System.out.print(fmt2); System.out.println(rs.getString(2)); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(ColumnHeaders.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(ColumnHeaders.class.getName()); lgr.log(Level.WARNING,ex); } } }
}
In this program,we select authors from the authors table and their books from the books table. We print the names of the columns returned in the result set. We format the output.
String query = “SELECT name,” +
“books WHERE authors.id=books.author_id”;
This is the sql statement which joins authors with their books.
ResultSetMetaData Meta = rs.getMetaData();
To get the column names we need to get the ResultSetMetaData. It is an object that can be used to get information about the types and properties of the columns in a ResultSet object.
String colname1 = Meta.getColumnName(1);
String colname2 = Meta.getColumnName(2);
From the obtained Metadata,we get the column names.
Formatter fmt1 = new Formatter();
fmt1.format(“%-21s%s”,colname2);
System.out.println(fmt1)
We print the column names to the console. We use the Formatter object to format the data.
while (rs.next()) {
Formatter fmt2 = new Formatter();
fmt2.format(“%-21s”,rs.getString(1));
System.out.print(fmt2);
System.out.println(rs.getString(2));
}
We print the data to the console. We again use the Formatter object to format the data. The first column is 21 characters wide and is aligned to the left.
$ javac zetcode/ColumnHeaders.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode/ColumnHeaders
name title
Jack London Call of the Wild
Jack London Martin Eden
Honore de Balzac Old Goriot
Honore de Balzac Cousin Bette
Lion Feuchtwanger Jew Suess
Emile Zola Nana
Emile Zola The Belly of Paris
Truman Capote In Cold blood
Truman Capote Breakfast at Tiffany
This is the output of the program.
In the following example we will list all tables in the testdb database.
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ListTables {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); String query = "SELECT table_name FROM information_schema.tables " + "WHERE table_schema = 'public'"; pst = con.prepareStatement(query); rs = pst.executeQuery(); while (rs.next()) { System.out.println(rs.getString(1)); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(ListTables.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(ListTables.class.getName()); lgr.log(Level.WARNING,ex); } } }
}
The code example prints all available tables in the current database to the terminal.
String query = “SELECT table_name FROM information_schema.tables ”
+ “WHERE table_schema = ‘public’”;
The table names are stored inside the system information_schema table.
$ javac zetcode/ListTables.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode.ListTables
authors
books
testing
images
Listing available tables in the testdb database.
Writing images
Some people prefer to put their images into the database,some prefer to keep them on the file system for their applications. Technical difficulties arise when we work with lots of images. Images are binary data. Postgresql database has a special data type to store binary data called bytea. This is a non-standard data type. The standard data type in databases is BLOB.
For this example,we use the images table.
package zetcode;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.sqlException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WriteImage {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; FileInputStream fin = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { File img = new File("woman.jpg"); fin = new FileInputStream(img); con = DriverManager.getConnection(url,password); pst = con.prepareStatement("INSERT INTO images(data) VALUES(?)"); pst.setBinaryStream(1,fin,(int) img.length()); pst.executeUpdate(); } catch (FileNotFoundException | sqlException ex) { Logger lgr = Logger.getLogger(WriteImage.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } if (fin != null) { fin.close(); } } catch (IOException | sqlException ex) { Logger lgr = Logger.getLogger(WriteImage.class.getName()); lgr.log(Level.WARNING,ex); } } }
}
In the preceding example,we read a JPG image from the current working directory and insert in into the images table.
pst = con.prepareStatement(“INSERT INTO images(data) VALUES(?)”);
This is the sql to insert an image.
File img = new File(“woman.jpg”);
fin = new FileInputStream(img);
We create a File object for the image file. To read bytes from this file,we create a FileInputStream object.
pst.setBinaryStream(1,(int) img.length());
The binary stream is set to the prepared statement. The parameters of the setBinaryStream() method are the parameter index to bind,the input stream and the number of bytes in the stream.
pst.executeUpdate();
We execute the statement.
Reading images
In the prevIoUs example,we have inserted an image into the database table. Now we are going to read the image back from the table.
Warning: if we are using Postgresql 9 and later,we must also use the latest JDBC driver. Postgresql has changed the way bytea data is stored. Therefore we run into troubles when using older driver with the Postgresql 9.x. Notice that NetBeans or other applications might use older drivers.
package zetcode;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ReadImage {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; FileOutputStream fos = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); String query = "SELECT data,LENGTH(data) FROM images WHERE id = 1"; pst = con.prepareStatement(query); ResultSet result = pst.executeQuery(); result.next(); fos = new FileOutputStream("woman2.jpg"); int len = result.getInt(2); byte[] buf = result.getBytes("data"); fos.write(buf,len); } catch (IOException | sqlException ex) { Logger lgr = Logger.getLogger(ReadImage.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } if (fos != null) { fos.close(); } } catch (IOException | sqlException ex) { Logger lgr = Logger.getLogger(ReadImage.class.getName()); lgr.log(Level.WARNING,ex); } } }
}
We read one image from the images table.
String query = “SELECT data,LENGTH(data) FROM images WHERE id = 1”;
We select the data and the size of the image from the database table.
fos = new FileOutputStream(“woman2.jpg”);
The FileOutputStream object is created to write to a file. It is meant for writing streams of raw bytes such as image data.
int len = result.getInt(2);
We get the length of the image data. In other words,we get the number of bytes.
byte[] buf = result.getBytes(“data”);
The getBytes() method retrieves all bytes from the result set,as an array of bytes.
fos.write(buf,len);
The bytes are written to the output stream. The image is created on the filesystem.
Transaction support
A transaction is an atomic unit of database operations against the data in one or more databases. The effects of all the sql statements in a transaction can be either all committed to the database or all rolled back.
When a connection is created,it is in autocommit mode. This means that each individual sql statement is treated as a transaction and is automatically committed right after it is executed. This is true for all JDBC drivers,including the Postgresql’s one. To start a new transaction,we turn the autocommit off.
In direct sql,a transaction is started with BEGIN TRANSACTION statement and ended with END TRANSACTION,COMMIT statement. In Postgresql these statements are BEGIN and COMMIT. However,when working with drivers these statements are omitted. They are handled by the driver. Exact details are specific to the driver. For example the psycopg2 Python driver starts a transaction after the first sql statement. The autocommit mode must be set by setting the autocommit property to True. In constrast,JDBC driver is by default in the autocommit mode. And to start a new transaction,the autocommit must be turned off.
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.sqlException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Transaction {
public static void main(String[] args) { Connection con = null; Statement st = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); st = con.createStatement(); con.setAutoCommit(false); st.executeUpdate("UPDATE authors SET name = 'Leo Tolstoy' " + "WHERE Id = 1"); st.executeUpdate("UPDATE books SET title = 'War and Peace' " + "WHERE Id = 1"); st.executeUpdate("UPDATE books SET titl = 'Anna Karenina' " + "WHERE Id = 2"); con.commit(); } catch (sqlException ex) { if (con != null) { try { con.rollback(); } catch (sqlException ex1) { Logger lgr = Logger.getLogger(Transaction.class.getName()); lgr.log(Level.WARNING,ex1.getMessage(),ex1); } } Logger lgr = Logger.getLogger(Transaction.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(Transaction.class.getName()); lgr.log(Level.WARNING,we want to change the name of the author in the first row of the authors table. We must also change the books associated with this author. A good example where a transaction is necessary. If we change the author and do not change the author’s books,the data is corrupted.con.setAutoCommit(false);
To work with transactions,we must set the autocommit to false. By default,a database connection is in autocommit mode. In this mode each statement is committed to the database,as soon as it is executed. A statement cannot be undone. When the autocommit is turned off,we commit the changes by calling the commit() or roll it back by calling the rollback() method.st.executeUpdate(“UPDATE books SET titl = ‘Anna Karenina’ ”
+ “WHERE Id = 2”);
The third sql statement has an error. There is no titl column in the table.con.commit();
If there is no exception,the transaction is committed. If the autocommit is turned off,we must explicitly call the commit() method.if (con != null) {
try {
con.rollback();
} catch (sqlException ex1) {
Logger lgr = Logger.getLogger(Transaction.class.getName());
lgr.log(Level.WARNING,ex1);
}
}
In case of an exception,the transaction is rolled back. No changes are committed to the database.$ javac zetcode/Transaction.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode.Transaction
Feb 03,2012 1:48:51 PM zetcode.Transaction main
SEVERE: ERROR: column “titl” of relation “books” does not exist
Position: 18
org.postgresql.util.PsqlException: ERROR: column “titl” of
relation “books” does not exist
…testdb=# SELECT name,title FROM authors,books WHERE authors.id=books.author_id;
name | title
——————-+———————-
Jack London | Call of the Wild
Jack London | Martin Eden
Honore de Balzac | Old Goriot
Honore de Balzac | Cousin Bette
Lion Feuchtwanger | Jew Suess
Emile Zola | Nana
Emile Zola | The Belly of Paris
Truman Capote | In Cold blood
Truman Capote | Breakfast at Tiffany
(9 rows)
An exception was thrown. The transaction was rolled back and no changes took place.However,without a transaction,the data is not safe.
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.sqlException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;public class NonTransaction {
public static void main(String[] args) { Connection con = null; Statement st = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); st = con.createStatement(); st.executeUpdate("UPDATE authors SET name = 'Leo Tolstoy' " + "WHERE Id = 1"); st.executeUpdate("UPDATE books SET title = 'War and Peace' " + "WHERE Id = 1"); st.executeUpdate("UPDATE books SET titl = 'Anna Karenina' " + "WHERE Id = 2"); con.close(); } catch (sqlException ex) { Logger lgr = Logger.getLogger(NonTransaction.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(NonTransaction.class.getName()); lgr.log(Level.WARNING,ex); } } }}
We have the same example. This time,without the transaction support.$ javac zetcode/NonTransaction.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode.NonTransaction
Feb 03,2012 1:59:04 PM zetcode.NonTransaction main
SEVERE: ERROR: column “titl” of relation “books” does not exist
Position: 18
…testdb=# SELECT name,books WHERE authors.id=books.author_id;
name | title
——————-+———————-
Leo Tolstoy | Martin Eden
Honore de Balzac | Old Goriot
Honore de Balzac | Cousin Bette
Lion Feuchtwanger | Jew Suess
Emile Zola | Nana
Emile Zola | The Belly of Paris
Truman Capote | In Cold blood
Truman Capote | Breakfast at Tiffany
Leo Tolstoy | War and Peace
(9 rows)
An exception is thrown again. Leo Tolstoy did not write Martin Eden. The data is corrupted.Batch updates
When we need to update data with multiple statements,we can use batch updates. Batch updates are available for INSERT,and DELETE statements as well as for CREATE TABLE and DROP TABLE statements.
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.sqlException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;public class BatchUpdate {
public static void main(String[] args) { Connection con = null; Statement st = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); st = con.createStatement(); con.setAutoCommit(false); st.addBatch("DROP TABLE IF EXISTS friends"); st.addBatch("CREATE TABLE friends(id serial,name VARCHAR(10))"); st.addBatch("INSERT INTO friends(name) VALUES ('Jane')"); st.addBatch("INSERT INTO friends(name) VALUES ('Tom')"); st.addBatch("INSERT INTO friends(name) VALUES ('Rebecca')"); st.addBatch("INSERT INTO friends(name) VALUES ('Jim')"); st.addBatch("INSERT INTO friends(name) VALUES ('Robert')"); int counts[] = st.executeBatch(); con.commit(); System.out.println("Committed " + counts.length + " updates"); } catch (sqlException ex) { System.out.println(ex.getNextException()); if (con != null) { try { con.rollback(); } catch (sqlException ex1) { Logger lgr = Logger.getLogger(BatchUpdate.class.getName()); lgr.log(Level.WARNING,ex1); } } Logger lgr = Logger.getLogger(BatchUpdate.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (sqlException ex) { Logger lgr = Logger.getLogger(BatchUpdate.class.getName()); lgr.log(Level.WARNING,ex); } } }}
This is an example program for a batch update. We create a new table called friends and insert five rows into it.con.setAutoCommit(false);
Autocommit should always be turned off when doing batch updates.st.addBatch(“DROP TABLE IF EXISTS friends”);
st.addBatch(“CREATE TABLE friends(id serial,name VARCHAR(10))”);
st.addBatch(“INSERT INTO friends(name) VALUES (‘Jane’)”);
st.addBatch(“INSERT INTO friends(name) VALUES (‘Tom’)”);
…
We use teh addBatch() method to add a new command to the statement.int counts[] = st.executeBatch();
After adding all commands,we call the executeBatch() to perform a batch update. The method returns an array of committed changes.con.commit();
Batch updates are committed in a transaction.$ javac zetcode/BatchUpdate.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode.BatchUpdate
Committed 7 updates
testdb=# SELECT * FROM friends;
id | name
—-+———
1 | Jane
2 | Tom
3 | Rebecca
4 | Jim
5 | Robert
(5 rows)
We execute the BatchUpdate program. We have created a new friends table and successfully inserted 5 rows.Export and import of data
Postgresql has a COPY statement which can be used to copy data between a table and a file. From the JDBC point of view,it is an extension to the standard.
package zetcode;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;public class CopyTo {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; FileWriter fw = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); CopyManager cm = new CopyManager((BaseConnection) con); fw = new FileWriter("friends.txt"); cm.copyOut("COPY friends TO STDOUT WITH DELIMITER AS '|'",fw); } catch (sqlException | IOException ex) { Logger lgr = Logger.getLogger(CopyTo.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } if (fw != null) { fw.close(); } } catch (sqlException | IOException ex) { Logger lgr = Logger.getLogger(CopyTo.class.getName()); lgr.log(Level.WARNING,ex); } } }}
In the prevIoUs example a simple friends table was created. In the above code,we will copy the friends table to a file.CopyManager cm = new CopyManager((BaseConnection) con);
Here we create an instance of the CopyManager. The CopyManager is the API for Postgresql COPY bulk data transfer.fw = new FileWriter(“friends.txt”);
We will write the data to the friends.txt file.cm.copyOut(“COPY friends TO STDOUT WITH DELIMITER AS ‘|’”,fw);
We pass the results of a COPY TO STDOUT query from database into a Writer using the copyOut() method. The columns will be delimited with the | character.$ javac -cp .:lib/postgresql.jdbc4.jar zetcode/CopyTo.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode.CopyTo
$ cat friends.txt
1|Jane
2|Tom
3|Rebecca
4|Jim
5|Robert
This is the file created.In the second example,we do the reverse operation. We copy the data from the file into the database table.
package zetcode;
import java.io.FileReader;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;public class CopyFrom {
public static void main(String[] args) { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; FileReader fr = null; String url = "jdbc:postgresql://localhost/testdb"; String user = "user12"; String password = "34klq*"; try { con = DriverManager.getConnection(url,password); CopyManager cm = new CopyManager((BaseConnection) con); fr = new FileReader("friends.txt"); cm.copyIn("COPY friends FROM STDIN WITH DELIMITER '|'",fr); } catch (sqlException | IOException ex) { Logger lgr = Logger.getLogger(CopyFrom.class.getName()); lgr.log(Level.SEVERE,ex); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } if (fr != null) { fr.close(); } } catch (sqlException | IOException ex) { Logger lgr = Logger.getLogger(CopyFrom.class.getName()); lgr.log(Level.WARNING,ex); } } }}
The example uses the FileReader class to read the contents of the friends.txt table and the COPY statement to transfer the data to the friends class.fr = new FileReader(“friends.txt”);
We will read from the friends.txt file.cm.copyIn(“COPY friends FROM STDIN WITH DELIMITER ‘|’”,fr);
We copy the data from the file using the COPY statement.testdb=# DELETE FROM friends;
DELETE 5$ javac -cp .:lib/postgresql.jdbc4.jar zetcode/CopyFrom.java
$ java -cp .:lib/postgresql.jdbc4.jar zetcode.CopyFrom
testdb=# SELECT * FROM friends;
id | name
—-+———
1 | Jane
2 | Tom
3 | Rebecca
4 | Jim
5 | Robert
(5 rows)
First we delete the rows from the friends table. Then we compile and run the CopyFrom application. Finally,we check the contents of the friends table.Tweet
This was the Postgresql Java tutorial. You might be also interested in MysqL Java tutorial,MongoDB Java tutorial,or Postgresql Python tutorial.reprint from http://zetcode.com/db/postgresqljavatutorial/?spm=5176.100239.blogcont68506.15.kXywuy