PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL JDBC / Connecting To The PostgreSQL Database

Connecting To The PostgreSQL Database

Summary: in this tutorial, we will show you how to setup Java environment, download PostgreSQL JDBC driver, and connect to the PostgreSQL database server from a Java program.

Setting up Java development environment

To develop a Java program, you need to have JDK installed on your computer. To setup JDK, first, you go to the Oracle website to download the latest JDK. Then, you install it on your computer.

PostgreSQL JDBC Download JDK and NetBeans

The setup is straightforward, you just need to accept the default parameters provided the installer and you are set.

Practically, to develop java applications, you need to have a good IDE. There are many good IDE available for free such as Eclipse, NetBeans, IntelliJ IDEA Community Edition, etc.

In this tutorial, we will use the NetBeans IDE to develop Java application. You can download the NetBeans IDE directly from the Oracle website or you can download it from the NetBeans download page.

You can follow the steps as shown in the animated picture below to install the NetBeans IDE.PostgreSQL JDBC - install netbean

Download PostgreSQL JDBC Driver

To connect to the PostgreSQL database server from a Java program, you need to have PostgreSQL JDBC driver. You can download the latest version of the driver on the postgresql.org website via the download page.

The downloaded file is a jar file. You should copy it to a specific folder e.g. C:\demo\libs so that you can remember its location and be able to add it to your Java application later.

Connect to the PostgreSQL database server

First, create a new project named PostgreSQLJDBC and the main class named App in the com.postgresqltutorial package.

Second, add the PostgreSQL JDBC driver jar file to the project.Create a project and add the PostgreSQL JDBC Driver jar File

Third, you need to prepare the following:

  • The address of the PostgreSQL database server e.g., localhost
  • The database name e.g., dvdrental
  • The username and password of the account that you will use to connect to the database.

For this information, you can construct the PostgreSQL JDBC connection string by using the following format:

1
jdbc:postgresql://<database_host>:<port>/<database_name>

The <port> is optional.

In our example, the connection string is:

1
jdbc:postgresql://localhost/dvdrental

To make it easier, we can define the attributes of the App class for storing connection string, user, and password:

1
2
3
private final String url = "jdbc:postgresql://localhost/dvdrental";
private final String user = "postgres";
private final String password = "<add your password>";

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.

1
2
3
4
5
6
7
8
9
10
11
public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Connected to the PostgreSQL server successfully.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
 
        return conn;
    }

The complete program for connecting to PostgreSQL database server is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.postgresqltutorial;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
/**
*
* @author postgresqltutorial.com
*/
public class App{
 
    private final String url = "jdbc:postgresql://localhost/dvdrental";
    private final String user = "postgres";
    private final String password = "<add your password>";
 
    /**
     * Connect to the PostgreSQL database
     *
     * @return a Connection object
     */
    public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Connected to the PostgreSQL server successfully.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
 
        return conn;
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        App app = new App();
        app.connect();
    }
}

PostgreSQL JDBC - Run Connecting to PostgreSQL Database Program

So we can connect to the PostgreSQL database server successfully.

In this tutorial, we have shown you how to setup the Java development environment for working with the PostgreSQL databases.

Next Tutorial: How To Call PostgreSQL Stored Function Using JDBC

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL JDBC

  • Connect To PostgreSQL Database in JDBC
  • Query Data from PostgreSQL using JDBC
  • Insert Data Into a Table Using JDBC
  • Update Data in PostgreSQL Using JDBC
  • Call PostgreSQL Stored Function in JDBC
  • Delete Data From PostgreSQL Using JDBC
  • PostgreSQL JDBC Transaction

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.