how to connect postgresql database with java 1

how to connect postgresql database with java

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;
    }
Code language: Java (java)

Here is what the above code is Doing:
1. Load the PostgreSQL JDBC driver using Class.forName() method.
2. Establish a connection to the database using DriverManager.getConnection() method.
3. Close the connection using Connection.close() method.

Similar Posts