This article discusses a simple Java program which connects to an ASA database using the Sybase jConnect JDBC driver. Although simple, this code is commonly requested. Having this code at hand will prove as a handy reference. Further research into JDBC and the Java.sql package is highly recommended to better understand JDBC programming.
Here is the code necessary to connect to an ASA database via jConnect. This sample retrIEves a result set consisting of all the rows and columns of the employee table.
The myConnection code:
import Java.sql.*;
public class myConnection {
public static void main( String[] args ) {
try {
// Using Sybase jConnect 4.2
Class.forName("com.Sybase.jdbc.SybDriver");
// Using Sybase jConnect 5.2
//Class.forName("com.Sybase.jdbc2.jdbc.SybDriver");
Connection conn = DriverManager.getConnection("jdbc:Sybase:Tds:testMachine-pc:2638", "dba", "sql");
Statement stmt = conn.createStatement( );
ResultSet rs = stmt.executeQuery( "SELECT * FROM EMPLOYEE");
while ( rs.next( ) ) {
System.out.println( rs.getInt( 1 ) );
}
} catch ( Exception e ) {
System.out.println( "An exception occurred." );
}
} // end of main
} // end of MyConnection class
BrIEf explanation of the code:
In order to understand how this code works, important sections of code will be discussed.
Section 1:
import Java.sql.*;
This line of code is necessary to have Access to the database connectivity classes. The Java.sql package contains the connection, statement, and result set classes necessary to retrIEve and display data from a database.
Section 2:
// Using Sybase jConnect 4.2
Class.forName("com.Sybase.jdbc.SybDriver");
// Using Sybase jConnect 5.2
//Class.forName("com.Sybase.jdbc2.jdbc.SybDriver");
These two lines of code are used to dynam