How the External Connection Example Works

The external connection example is a Java command-line application.

Importing Packages

The application requires several libraries, which are imported in the first lines of JDBCExamples.java:

  • The java.sql package contains the Sun Microsystems JDBC classes, which are required for all JDBC applications. You'll find it in the classes.zip file in your Java subdirectory.

  • Imported from com.sybase.jdbc, the Sybase jConnect JDBC driver is required for all applications that connect using jConnect. You'll find it in the jdbcdrv.zip file in your Java subdirectory.

  • The application uses a property list. The java.util.Properties class is required to handle property lists. You'll find it in the classes.zip file in your Java subdirectory.

  • The sybase.sql package contains utilities used for serialization. You'll find it in the asajdbc.zip file in your Java subdirectory.

  • The asademo package contains example classes used in some examples. You'll find it in the asademo.jar file in your java subdirectory.

The Main Method

Each Java application requires a class with a method named main, which is the method invoked when the program starts. In this simple example, JDBCExamples.main is the only method in the application.

The JDBCExamples.main method carries out the following tasks:

  • Processes the command-line argument, using the machine name if supplied. By default, the machine name is localhost, which is appropriate for the personal database server.

  • Calls the ASAConnect method to establish a connection.

  • Executes several methods that scroll data to your command line.

The ASAConnect Method

The JDBCExamples.ASAConnect method carries out the following tasks:

  • Connects to the default running database using Sybase jConnect.

    • Class.forName loads jConnect. Using the newInstance method works around issues in some browsers.

    • The StringBuffer statements build up a connection string from the literal strings and the supplied machine name provided on the command line.

    • DriverManager.getConnection establishes a connection using the connection string.

  • Returns control to the calling method.