JDBC Tutorial - JDBC PostgreSQL








The following code shows how to connect to PostgreSQL database.

Example

import java.sql.Connection;
import java.sql.DriverManager;
//  ww w .j a  v a2  s  . c  o m
public class Main {
  public static void main(String[] argv) throws Exception {
    Class.forName("org.postgresql.Driver");
    Connection connection = DriverManager.getConnection(
        "jdbc:postgresql://127.0.0.1:5432/testdb", "userName",
        "password");
    if (connection != null) {
      System.out.println("Connected");
    } else {
      System.out.println("Failed to make connection!");
    }
  }
}