Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - Programming » SQL Anywhere JDBC API

Database options set for jConnect connections Next Page

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 application 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 on 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 2.0 version of the iAnywhere JDBC driver to connect to the database. (To use the JDBC 3.0 version of the iAnywhere JDBC driver replace ianywhere.ml.jdbcodbc.IDriver with ianywhere.ml.jdbcodbc.jdbc3.IDriver.) Code alternatives are included as comments in the source code if you want to use the jConnect 6.0.5 driver.

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

public class JDBCConnect
{
  public static void main( String args[] )
  {
    try
    {
      // Open the connection. May throw a SQLException.
      DriverManager.registerDriver( (Driver)
        Class.forName(
        // "com.sybase.jdbc3.jdbc.SybDriver").newInstance()
        "ianywhere.ml.jdbcodbc.IDriver").newInstance()
        );

      Connection con = DriverManager.getConnection(
          // "jdbc:sybase:Tds:localhost:2638", "DBA", "sql");
          "jdbc:ianywhere:driver=SQL Anywhere 10;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