Use JDBC To Connect PostgreSQL Database

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

1. Install PostgreSQL Database. 

If you do not installed it before, you should install it first.

  1. Go to PostgreSQL download page to download suitable version. My version is windows 64 bit version.
    download postgresql server page
  2. Click Windows link in above page, you will be redirected to windows installer page. There has two graphical installer, you can choose the one you like.
    download postgresql windows installer
  3. After download click the installer to install it. Please remember the password you entered during the installation.
  4. After install, you can start PostgreSQL server manager by click pgAdmin4 in windows startup menu.
    postgresql admin manager
  5. When the manager startup, double click the server icon in left panel, there will popup a password dialog, input the password you configured during the installation. My password is “postgresql” also.
    postgresql connection page
  6. If your password is correct, you will see the database list in left panel.
    postgresql database browser

2. Create Example Table

This example table is just the table we used in this postgresql jdbc tutorial.

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

3. Download PostgreSQL JDBC Driver.

  1. Go to PostgreSQL JBDC driver page.
  2. Choose suitable version jar file and download.
  3. Add the download jar into Eclipse java project build path.
    add postgresql jdbc jar in eclipse java project build path
READ :   How To Fix MySQL JDBC 08001 Database Connection Error

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

7. Source Code:

  1. Download “PostgreSQLExample.zip” PostgreSQLExample.zip – Downloaded 157 times – 931 B

(Visited 1,985 times, 1 visits today)

Leave a Reply

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.