Using the Advanced Server JDBC Connector with Java applications v42.2.19.1

Edit this page

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

Listing 1.1

import java.sql.*;
public class ListEmployees
{
  public static void main(String[] args)
  {
    try
    {
      Class.forName("com.edb.Driver");
      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(ClassNotFoundException e)
    {
      System.out.println("Class Not Found : " + e.getMessage());
    }
    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 demonstrates the fundamental steps required to interact with an Advanced Server database from a Java application:

  • Load the JDBC driver
  • Build connection properties
  • Connect to the database server
  • Execute an SQL statement
  • Process the result set
  • Clean up
  • Handle any errors that may 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