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 ASIQ-12_7/Samples/ASA/JavaSQL/manual-examples directory on Windows or ASIQ-12_7/samples/asa/java on UNIX under your Sybase IQ installation directory:

// Import the necessary classes
import java.sql.*;             // JDBC
import com.sybase.jdbc.*;      // Sybase jConnect
import java.util.Properties;   // Properties
import sybase.sql.*;           // Sybase utilities
import asademo.*;              // Example classes

private static Connection conn;
public static void main(String args[])  {

    conn = null;
    String machineName;
    if ( args.length != 1 ) {
      machineName = "localhost";
    } else {
      machineName = new String( args[0] );
    }

    ASAConnect( "dba", "sql", machineName );
    if( conn!=null ) {
        System.out.println( "Connection successful" );
    }else{
        System.out.println( "Connection failed" );
    }

    try{
      serializeVariable();
      serializeColumn();	
      serializeColumnCastClass();
    }
    catch( Exception e ) {
      System.out.println( "Error: " + e.getMessage() );
      e.printStackTrace();
    }  
  }
}
private static void ASAConnect( String UserID, 
                              String Password, 
                              String Machinename ) {
    // uses global Connection variable
    
    String _coninfo = new String( Machinename );
    
    Properties _props = new Properties();
    _props.put("user", UserID );
    _props.put("password", Password );
    
    // Load the Sybase Driver
    try {
      Class.forName("com.sybase.jdbc.SybDriver").newInstance();
      
      StringBuffer temp = new StringBuffer();
      // Use the Sybase jConnect driver...
      temp.append("jdbc:sybase:Tds:"); 
      // to connect to the supplied machine name...
      temp.append(_coninfo);
      // on the default port number for ASA...
      temp.append(":2638");
      // and connect.
      System.out.println(temp.toString());
      conn = DriverManager.getConnection( temp.toString() , _props );
    }
    catch ( Exception e ) {
      System.out.println("Error: " + e.getMessage());
      e.printStackTrace();
    }   
  }