How to connect to the Postgres database using Eclipse and Netbeans

SUMMARY: This article explains how to connect to a Postgres database using the Eclipse and Netbeans IDEs. It first defines what an Integrated Development Environment (IDE) is, then walks through the steps for connecting to both IDEs. 

1. Integrated Development Environments

2. Connecting to Postgres with Eclipse

3. Connecting to Postgres with Netbeans

        1. Sample Java for connecting with Netbeans 

 

Integrated Development Environments

An Integrated Development Environment (IDE) is an application that provides tools for software development. It helps users to write and debug software programs. Examples of such IDE's are  NetBeans, Eclipse, IntelliJ, and Visual Studio. 

 

Connecting to Postgres with Eclipse

What is Eclipse?

Eclipse is a platform that has been designed from the ground up for building integrated web and application development tooling. Developers used the Eclipse IDE for Java-based software development.

For the demonstration, I have used Eclipse IDE for Java Developers Version: 2019-03 (4.11.0).

 

Step 1 

First, download the Postgres JDBC driver for the Java version you are using from the following link:

https://jdbc.postgresql.org/download.html

In our sample example, I have copied the JAR file postgresql-42.2.8.jar into the directory

“/opt/postgres_jdbc_driver”.

 

Step 2 

Open the Database Development Perspective.

I will use the Database Development Perspective for the database connection. Follow the below path to open the Database Development perspective:

Open Database Development Perspective. Go to menu Window > Open Perspective > Other > select Database Development Perspective from the list of perspectives and click OK.

 

Step 3 

Make the connection to the Postgres Database:

  • Right click on Database Connections (on left-hand side)
  • Click on New
  • Select PostgreSQL
  • Enter the name for the connection profile

 

Step 4 

Select Drivers for the connection:

  • Click on the drivers
  • Select JAR List
  • Add JAR
  • Select the JAR file (downloaded in step 1)
  • Click OK

 

Step 5 

Enter the database details (i.e., port, database name, username, password) for the connection.

 

Step 6 

Test the connection.

After filling the details click on Test Connection. It will show you the alert Ping succeeded if you successfully connected to the database. 

 

Click Finish.

 

Step 7 

Interact with the Postgres database by using the SQL scrapbook.

SQL Scrapbook enables you to quickly create and execute SQL commands and queries, as well as procedural objects without creating a SQL file.

 

Connecting to Postgres with Netbeans

What is Netbeans?

NetBeans IDE lets you quickly and easily develop Java desktop, mobile, and web applications, as well as HTML5 applications with HTML, JavaScript, and CSS. The IDE also provides tools for PHP and C/C++ developers. It is free and open-source and has a large community of users and developers around the world.

The NetBeans IDE can boost your productivity when you're working with Java SE, Java EE, or Java ME technology as well as PHP, Groovy, JavaScript, and C/C++. Visual tools that generate skeleton let you create a basic application without writing a single line of code.

 

Step 1 

Open the Netbeans IDE 8.2 and open the new project using the following path:

File > New Project > Categories: Java > Projects: Java Application 

Then click Next.

 

Step 2 

Specify the Project Name and the name for the main class.

The Project Folder will get updated after filling in the Project Name. Click on Create Main Class if it’s not already selected.

 

Click Finish. You will then see the window below:

 

Step 3 

Add the PostgreSQL JDBC Driver.

NetBeans IDE provides drivers for the PostgreSQL database servers so that you can connect to this database very easily:

  • Right-click on Libraries.
  • Click on PostgreSQL JDBC Driver 
  • Click on Add Library

 

Step 4

Clear the sample code which is already present and add the sample Java code provided below for the database connection.

In the sample code below, I am using the JDBC connection string for the connection. With JDBC, a database is represented by a URL (Uniform Resource Locator). Change the database server hostname, database port, database port, and database name as per your environment, or else you will get an error at the time of the connection. You can collect these details from your database server.

jdbc:postgresql://<host>:<port>/<database_name> 

 

  • In the sample code change the database username and database user password as per your environment.
  • Execute the program. You will get the message "You are successfully connected to the PostgreSQL database server." after the successful connection with the database. 

 

In the sample code below, we need to first define three variables:

Database_connection_string: the JDBC database connection URL, so that the code knows where it needs to connect.

Database_user_name, database_user_password: the database username and password for the connection.

By using the Connection connect() code we are defining the connection object, which will connect to the database and after the successful connectivity will print the successful connection message.

Sample Java Code

package netbeanstopostgres;



import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;



public class Netbeanstopostgres

{ 

    private  String database_connection_string = "jdbc:postgresql://localhost:5432/postgres";

    private  String database_user_name = "postgres";

    private  String database_user_password = "postgres@123";

    /**

     * By using below code we are connecting to the database and returning the connection object.

     * @return 

    */

    public Connection connect() {

        Connection conn = null;

        try {

            conn = DriverManager.getConnection(database_connection_string, database_user_name, database_user_password );

            System.out.println("You are successfully connected to the PostgreSQL database server.");

        } catch (SQLException e) 

        {

            System.out.println(e.getMessage());

        }



        return conn;

    }



    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) 

    {

        Netbeanstopostgres conn = new Netbeanstopostgres();

       conn.connect();

    }

}