Java - JDBC PostgreSQL Connection Example

In this tutorial, we will show you how to download PostgreSQL JDBC driver, and connect to the PostgreSQL database server from a Java program.
Check out all Java PostgreSQL examples at Java PostgreSQL Tutorial.
PostgreSQL is a powerful, open-source object-relational database system. It is a multi-user database management system. 

Technologies used

We use below technologies in this tutorial:
  • JDK - 1.8 or later
  • PostgreSQL- 42.2.9
  • IDE - Eclipse Neon
  • JDBC - 4.2

Download PostgreSQL JDBC Driver

To connect to the PostgreSQL database server from a Java program, you need to have a PostgreSQL JDBC driver. You can download the latest version of the driver on the postgresql.org website via the download page.
Add the PostgreSQL JDBC driver jar file to the project classpath.
For maven users:
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.9</version>
</dependency>
For gradle users:
// https://mvnrepository.com/artifact/org.postgresql/postgresql
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.9'

PostgreSQL Database Setup

We are going to show how to install a PostgreSQL database on Windows 10. Make sure that you have installed the PostgreSQL server on your system.
You can use the following command to create a database in PostgresSQL server:
CREATE DATABASE mydb;

JDBC Connection to the PostgreSQL Database Server

Let's construct the PostgreSQL JDBC connection string by using the following format:
jdbc:postgresql://<database_host>:<port>/<database_name>
This <port> is optional.
In our example, the connection string is:
jdbc:postgresql://localhost/mydb
To establish a connection to the PostgreSQL database server, you call the getConnection method of the DriverManager class. This method returns a Connection object.
The following connect() method connects to the PostgreSQL database server and returns a Connection object.
/**
     * Connect to the PostgreSQL database
     *
     * @return a Connection object
     */
    public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);

            if (conn != null) {
                System.out.println("Connected to the PostgreSQL server successfully.");
            } else {
                System.out.println("Failed to make connection!");
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        return conn;
    }
The complete program for connecting to PostgreSQL database server is as follows:
package net.javaguides.postgresql.tutorial;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCPostgreSQLConnection {
    private final String url = "jdbc:postgresql://localhost/myDB";
    private final String user = "postgres";
    private final String password = "root";

    /**
     * Connect to the PostgreSQL database
     *
     * @return a Connection object
     */
    public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);

            if (conn != null) {
                System.out.println("Connected to the PostgreSQL server successfully.");
            } else {
                System.out.println("Failed to make connection!");
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        return conn;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JDBCPostgreSQLConnection app = new JDBCPostgreSQLConnection();
        app.connect();
    }
}
Output:
Connected to the PostgreSQL server successfully.

So we can connect to the PostgreSQL database server successfully.
In this tutorial, we have shown you how to connect with the PostgreSQL database using a Java program.
Check out all Java PostgreSQL examples at Java PostgreSQL Tutorial.

Comments