Introduction to PostgreSQL JDBC Driver
While creating a database based application and using any of the databases in your java application, you will need to follow certain steps to use the JDBC (Java Database Connectivity), which is an API .i.e. Application Programming Interface that helps us to communicate between our java application and our database. The database can be any database like MySQL, PostgreSQL, etc. This article will learn how we can connect our java application with our PostgreSQL database with theJDBC driver’s helpr.
While creating a database-based java application, it is required to have few things installed in your system, which are as follows –
- Java JDK toolkit
It can be checked by typing the command java -version in your command, which will provide you with the output specifying the version of java available in your system like the following.
If not present, you should firstly install java. Here, we used java version “1.8.0_201”.
- PostgreSQL and psql should be installed on your machine that can be checked by typing the command psql -V that should give output as follows –
If not available, install it before proceeding with JDBC driver installation. Mine is psql (PostgreSQL) 12.2 (Ubuntu 12.2-2.pgdg18.04+1).
- The last thing is the JDBC driver for the PostgreSQL jar file, which can be downloaded from the link https://jdbc.postgresql.org/download.html. We will use it further while establishing the JDBC connection.
Steps for JDBC initialization
- We first need to import JDBC using the import statement –
You should be careful here; you should not import org.postgresql package in your application as doing so will confuse the javac for compiling the source file.
- The second step is where you will need to load your JDBC driver. This can be done in two ways. One by using the Class.forName() method and others by passing your driver as a parameter to the JVM while initializing. The second step is more preferable as in case if in your future your application needs to change its database server, then it can be done without changing the connection related code easily.
In the first method, we will use the Class.forName() method in the following way –
where org.postgresql.Driver specifies your PostgreSQL JDBC driver usage. This method can give ClassNotFoundException if our driver is not found. This method is most often used in JDBC applications.
The second method consists of passing your JDBC driver as a parameter to the initialization string using the -D option as follows –
java -Djdbc.drivers=org.postgresql.Driver example.ImageViewer
- Now is the time to connect to the database. The database is represented by a URL (Uniform Resource Locator) in JDBC applications which can be in either of the three forms while using the PostgreSQL database.
the hostName is localhost by default, and if remote is the Ipv6 address of the machine.
portNumber is socket/port address which is by default 5432 for PostgreSQL.
DatabaseName is the name of the database you want to connect to for your JDBC application.
Finally, you can now connect using the statement.
Connection dbConnectionObject= DriverManager.getConnection(url, username, password);
- The last step is closing your JDBC connection once you are done with performing all your database related manipulations, which can simply be done by closing your Connection object as follows –
Example of PostgreSQL JDBC Driver
Let us consider one example of a JDBC application with PostgreSQL. For this, We will firstly need to create a database in our PostgreSQL database server. We will create a database named educba and will connect to it using our JDBC program in java.
Create a new file named EducbaJdbcExample.java which will contain a program like this -Our program’s first three statementsm are for importing the packages required for using the JDBC related methods. The next thing is that we are trying to establish the connection with our PostgreSQL using the following statement –
Connection myConnection = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/educba", "postgres", "a")
where 127.0.0.1 stands for localhost, i.e. same machine address, and 5432 is the PostgreSQL port, and we want to connect to the educba database name, and our username and password are ‘a’ and ‘a’ respectively. Here, we have inserted this statement in try because if any exception arises while establishing the connection it will give an immediate exception and display the message related to the exception. If the connection is established, then the message saying “Successfully connected to the educba database!” will be displayed; else “Sorry! Failed to establish the JDBC connection” message will be displayed.
After saving, compiling and running the application, if it gives the exception like following –
In case, if you are using Eclipse IDE and create a java class file for your program and the by right-clicking your file ->Run As -> Java application option you can run your program. In that case, it will give the following output. This is because no JDBC driver file of PostgreSQL has downloaded present in our current project.
This is because the JDBC driver is not loaded. In such applications, we will need to load our JDBC driver manually using cp. For this, it is required that your program EducbaJdbcExample .java and the downloaded jar file of JDBC driver for PostgreSQL are stored in the same path if you are running through the command line or you have imported jar file in your current project if you are using Eclipse IDE as follows –
Then you can run your application as a java application if you are using eclipse –
As can be seen, the message on the console is now “Successfully connected to educba database!”. So, we are connected to our database now.
We can connect with our PostgreSQL database from our java application by following all the above steps in a proper manner.
This is a guide to PostgreSQL JDBC Driver. Here we discuss the example of PostgreSQL JDBC Driver along with the Steps for JDBC initialization and Pre-requisites. You may also have a look at the following articles to learn more –