Using the EDB JDBC Connector with Java applications v42.5.4.2

With Java and the EDB JDBC Connector in place, a Java application can access an EDB Postgres Advanced Server database. This example creates an application that executes a query and prints the result set.

import java.sql.*;
public class ListEmployees
{
  public static void main(String[] args)
  {
    try
    {
      String url      = "jdbc:edb://localhost:5444/edb";
      String user     = "enterprisedb";
      String password = "enterprisedb";
      Connection con  = DriverManager.getConnection(url, user, password);
      Statement stmt  = con.createStatement();
      ResultSet rs    = stmt.executeQuery("SELECT * FROM emp");
      while(rs.next())
      {
        System.out.println(rs.getString(1));
      }

      rs.close();
      stmt.close();
      con.close();
      System.out.println("Command successfully executed");
    }
    catch(SQLException exp)
    {
      System.out.println("SQL Exception: " + exp.getMessage());
      System.out.println("SQL State:     " + exp.getSQLState());
      System.out.println("Vendor Error:  " + exp.getErrorCode());
    }
  }
}

This example is simple, but it shows the fundamental steps required to interact with an EDB Postgres Advanced Server database from a Java application:

  • Load the JDBC driver.
  • Build connection properties.
  • Connect to the database server.
  • Execute a SQL statement.
  • Process the result set.
  • Clean up.
  • Handle any errors that occur.

loading_the_advanced_server_jdbc_connector connecting_to_the_database executing_sql_statements_through_statement_objects retrieving_results_from_a_resultset_object freeing_resources handling_errors