Connecting from a JDBC client application

Database metadata is always available when using the iAnywhere JDBC driver.

If you want to access database system tables (database metadata) from a JDBC application that uses jConnect, you must add a set of jConnect system objects to your database. These procedures are installed to all databases by default. The dbinit -i option prevents this installation.

For more information about adding the jConnect system objects to a database, see Using the jConnect JDBC driver.

The following complete Java application is a command line program that connects to a running database, prints a set of information to your command line, and terminates.

Establishing a connection is the first step any JDBC application must take when working with database data.

This example illustrates an external connection, which is a regular client/server connection. For information about how to create an internal connection from Java classes running inside the database server, see Establishing a connection from a server-side JDBC class.

Connection example code

The following is the source code for the methods used to make a connection. The source code can be found in the file JDBCConnect.java in the samples-dir\SQLAnywhere\JDBC directory. As presented, the example uses the JDBC 3.0 version of the iAnywhere JDBC driver to connect to the database. To use the jConnect 6.0.5 driver replace ianywhere.ml.jdbcodbc.jdbc3.IDriver with com.sybase.jdbc3.jdbc.SybDriver. You must also change the connection string if you want to use the jConnect 6.0.5 driver. Code alternatives are included as comments in the source code.

import java.io.*;
import java.sql.*;

public class JDBCConnect
{
  public static void main( String args[] )
  {
    try
    {
      // Select the JDBC driver. May throw a SQLException.
      // Choices are jConnect 6.0 driver
      // or iAnywhere JDBC 3.0 driver.
      // Currently, we use the iAnywhere JDBC 3.0 driver.
      DriverManager.registerDriver( (Driver)
        Class.forName(
        // "com.sybase.jdbc3.jdbc.SybDriver").newInstance()
        "ianywhere.ml.jdbcodbc.jdbc3.IDriver").newInstance()
        );

      // Create a connection. Choices are TDS using jConnect,
      // Sun's JDBC-ODBC bridge, or the iAnywhere JDBC driver.
      // Currently, we use the iAnywhere JDBC driver.
      Connection con = DriverManager.getConnection(
        // "jdbc:sybase:Tds:localhost:2638", "DBA", "sql");
        // "jdbc:odbc:driver=SQL Anywhere 11;uid=DBA;pwd=sql" );
        "jdbc:ianywhere:driver=SQL Anywhere 11;uid=DBA;pwd=sql" );

      // Create a statement object, the container for the SQL
      // statement. May throw a SQLException.
      Statement stmt = con.createStatement();

      // Create a result set object by executing the query.
      // May throw a SQLException.
      ResultSet rs = stmt.executeQuery(
        "SELECT ID, GivenName, Surname FROM Customers");

      // Process the result set.
      while (rs.next())
      {
        int value = rs.getInt(1);
        String FirstName = rs.getString(2);
        String LastName = rs.getString(3);
        System.out.println(value+" "+FirstName+" "+LastName);
      }
      rs.close();
      stmt.close();
      con.close();
    }
    catch (SQLException sqe)
    {
      System.out.println("Unexpected exception : " +
                sqe.toString() + ", sqlstate = " +
                sqe.getSQLState());
      System.exit(1);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      System.exit(1);
    }

    System.exit(0);
  }
}

How the connection example works
Running the connection example
Establishing a connection from a server-side JDBC class
Notes on JDBC connections