Use JDBC To Connect PostgreSQL Database

This article will show you how to install, use the PostgreSQL database server and how to use JDBC to connect to it.

1. Install PostgreSQL Database.

  1. If you do not install it before, you should install it first.
  2. Go to the PostgreSQL download page to download a suitable version. My version is the Windows 64-bit version.
  3. Click the Windows link on the above page, you will be redirected to the windows installer page. There are two graphical installers, you can choose the one you like.
  4. After download, clicks the installer to install it. Please remember the password you entered during the installation.
  5. After install, you can start the PostgreSQL server manager by clicking the Windows Start —> PostgreSQL 9.6 —> pgAdmin 4.
  6. When the manager startup, double click the server icon in the pgAdmin 4 PostgreSQL database server manager left panel, there will popup a password dialog, input the password you configured during the installation. My password is “postgresql” also.
  7. If your password is correct, you will see the database list in the left panel.

2. Create Example PostgreSQL Table.

This example table is just the table we used in this PostgreSQL JDBC tutorial.

  1. Right-click the server icon in the pgAdmin-4 left panel. Click “Create —> Database“. Name the database as student.
  2. Right-click the student database just created, click “Create —> Schema“, name it with UserAccount.
  3. Right click UserAccount schema, click “Create —> Table” to create a new table UserInfo.
  4. The table has two columns, UserName, and Password.

3. Download PostgreSQL JDBC Driver.

  1. Go to the PostgreSQL JBDC driver page.
  2. Choose a suitable version jar file and download it.
  3. Add the downloaded jar into the Eclipse java project build path follow the below steps.
  4. Right-click the eclipse java project, click the Properties menu item.
  5. In the popup window, click the Java Build Path item on the left side, then click the Libraries tab on the right side.
  6. Click the Add External JARs… button to browse and select the PostgreSQL JDBC driver jar file and add it to the java project java build path.

4. PostgreSQL JDBC Connection Java Code Example.

/* Register jdbc driver class. */
Class.forName("org.postgresql.Driver");
			
/* Create connection url. */
String mysqlConnUrl = "jdbc:postgresql://localhost:5432/student";
			
/* user name. */
String mysqlUserName = "postgres";
			
/* password. */
String mysqlPassword = "postgres";
			
/* Get the Connection object. */
java.sql.Connection conn = DriverManager.getConnection(mysqlConnUrl, mysqlUserName , mysqlPassword);

5. Complete Example code.

public class PostgreSQLExample {

	public static void main(String[] args) {
		
		try
		{
			PostgreSQLExample postgreSQLExample = new PostgreSQLExample();
			
			Connection conn = postgreSQLExample.getPostgreSQLConnection();
			
			/* You can use the connection object to do any insert, delete, query or update action to the mysql server.*/
			
			/* Do not forget to close the database connection after use, this can release the database connection.*/
			conn.close();
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	/* This method return java.sql.Connection object. */
	public Connection getPostgreSQLConnection()
	{
		/* Declare and initialize a sql Connection variable. */
		Connection ret = null;
		
		try
		{
		
			/* Register jdbc driver class. */
			Class.forName("org.postgresql.Driver");
			
			/* Create connection url. */
			String mysqlConnUrl = "jdbc:postgresql://localhost:5432/student";
			
			/* user name. */
			String mysqlUserName = "postgres";
			
			/* password. */
			String mysqlPassword = "postgres";
			
			/* Get the Connection object. */
			ret = DriverManager.getConnection(mysqlConnUrl, mysqlUserName , mysqlPassword);
			
			/* Get related meta data for this mysql server to verify db connect successfully.. */
			DatabaseMetaData dbmd = ret.getMetaData();
			
			String dbName = dbmd.getDatabaseProductName();
			
			String dbVersion = dbmd.getDatabaseProductVersion();
			
			String dbUrl = dbmd.getURL();
			
			String userName = dbmd.getUserName();
			
			String driverName = dbmd.getDriverName();
			
			System.out.println("Database Name is " + dbName);
			
			System.out.println("Database Version is " + dbVersion);
			
			System.out.println("Database Connection Url is " + dbUrl);
			
			System.out.println("Database User Name is " + userName);
			
			System.out.println("Database Driver Name is " + driverName);
						
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}finally
		{
			return ret;
		}
	}

}

6. Example Output.

Database Name is PostgreSQL
Database Version is 9.6.4
Database Connection Url is jdbc:postgresql://localhost:5432/student
Database User Name is postgres
Database Driver Name is PostgreSQL JDBC Driver

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.