askIT

Connect Java to PostgreSQL database

document-save.png Download Java connect and disconnect to PostgreSQL code

Step 1 - Start pgAdmin

Make sure that pgAdmin is running. You can start it by clicking on pgAdmin4.exe.

start_pgadmin_in_windows.png


Step 2- Create a database

Go to http://127.0.0.1:55042/browser/. Expand Servers -> PostgreSQL -> Databases.

Right click Databases and select Create Database....

pgadmin_create_new_database.png

Then add a name for your database.
Name: coffe_shop

pgadmin_create_database.png


Step 3 - Create a IntelliJ project

Start IntelliJ. Create a New Java project.

intellij_new_project_java.png

Select the template Command Line App.

intellij_new_project_command_line_app.png

Set the project name, location and base package:

intellij_new_project_project_name.png

You should now have a empty project with one class.

intellij_new_empty_project.png


Step 4 - Inport PostgreSQL driver

Download the PostgreSQL JDBC Driver. I downloaded postgresql-42.2.2.jar.

document-save.png Download PostgreSQL database driver

To import this driver into your porject go to File -> Project Structure...

intellij_project_structure.png

Click on Libraries and the green plus. Click Java.
Browse the path to postgresql-42.2.2.jar.

intellij_project_structure_libraries.png

Underneath External Libraries the postgresql director should appear.

intellij_external_library_postgre.png


Step 5 - Use the PostgreSQL driver

Right click your package com.nettport.coffeshop and select New -> Package.

dao_new_package.png

Right click your package com.nettport.coffeshop.dao and select New -> Java Class.

dao_new_class.png

package com.nettport.coffeshop.dao;

import java.sql.*;

public class DbAdapter {

    /* 01 Variables ---------------------------------------- */
    String jdbcUrl = "jdbc:postgresql://localhost:5432/coffe_shop";
    String username = "postgres";
    String password = "postgre";

    /* 02 Database variables ------------------------------- */
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    /* 03 Constructor for DbAdapter ------------------------ */
    public DbAdapter() {
    }

    /**
    * Connect to a database
    */

    public void connect() {
        try {
            // Step 2 - Open connection
            conn = DriverManager.getConnection(jdbcUrl, username, password);

            // Print connected
            System.out.println("DbAdapter: Connection to database established");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    } // connect

    /**
    * Disconnect from database
    */

    public void disconnect() {
        try {

            // Step 5 Close connection
            if (stmt != null) {
                stmt.close();
            }
            if (rs != null) {
                rs.close();
            }
            if (conn != null) {
                conn.close();
            }
            // Print connected
            System.out.println("DbAdapter: Connection to database closed");

        } catch (Exception e) {
            e.printStackTrace();
        }

    } // disconnect
}

Now you can use the connection in your Main class:

package com.nettport.coffeshop;

import com.nettport.coffeshop.dao.DbAdapter;

public class Main {

    public static void main(String[] args) {
        DbAdapter dbAdapter = new DbAdapter();
        dbAdapter.connect();
        dbAdapter.disconnect();

    }
}

When you run the program this should be your output:

dbadapter_program.png

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.