Contents Index Connecting from a JDBC client application using jConnect How the external connection example works

ASA Programming Guide
  JDBC Programming
    Establishing JDBC connections
      Connecting from a JDBC client application using jConnect

External 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 main method and the ASAConnect method of the file JDBCExamples.java in the Samples\ASA\Java directory under your SQL Anywhere directory:

import java.sql.*;             // JDBC
import com.sybase.jdbc2.jdbc.*;      // Sybase jConnect
import java.util.Properties;   // Properties
import sybase.sql.*;           // Sybase utilities
import asademo.*;              // Example classes
public class JDBCExamples{
  private static Connection conn;
public static void main( String args[] ){
    // Establish a connection
    conn = null;
    String machineName = 
       ( args.length == 1 ? args[0] : "localhost" );
    ASAConnect( "DBA", "SQL", machineName );
    if( conn!=null ) {
        System.out.println( "Connection successful" );
    }else{
        System.out.println( "Connection failed" );
    }

    try{
       getObjectColumn();
       getObjectColumnCastClass();
       insertObject();
    }
    catch( Exception e ){
      System.out.println( "Error: " + e.getMessage() );
      e.printStackTrace();
    }
  }
private static void ASAConnect( String userID,
                String password,
                String machineName ) {
    // Connect to an Adaptive Server Anywhere
    String coninfo = new String( machineName );

    Properties props = new Properties();
    props.put( "user", userID );
    props.put( "password", password );
    props.put("DYNAMIC_PREPARE", "true");
// Load jConnect
    try {
      Class.forName( 
         "com.sybase.jdbc2.jdbc.SybDriver" ).newInstance();
      String dbURL = "jdbc:sybase:Tds:" + machineName  +
                     ":2638/?JCONNECT_VERSION=5";
      System.out.println( dbURL );
      conn = DriverManager.getConnection( dbURL , props );
    }
    catch ( Exception e ) {
      System.out.println( "Error: " + e.getMessage() );
      e.printStackTrace();
    }
  }
}

Contents Index Connecting from a JDBC client application using jConnect How the external connection example works