The following sample code illustrates how to invoke the jConnect driver, make a connection, issue a SQL statement, and process results.
import java.io.*; import java.sql.*; public class SampleCode { public static void main(String args[]) { try { /* * Open the connection. May throw a SQLException. */ DriverManager.registerDriver(
(Driver) Class.forName(
"com.sybase.jdbc4.jdbc.SybDriver").newInstance());
Connection con = DriverManager.getConnection( "jdbc:sybase:Tds:myserver:3767", "sa", ""); /* * 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 1"); /* * Process the result set. */ if (rs.next()) { int value = rs.getInt(1); System.out.println("Fetched value " + value); }
rs.close()
stmt.close()
con.close() }//end try /* * Exception handling. */ catch (SQLException sqe) { System.out.println("Unexpected exception : " + sqe.toString() + ", sqlstate = " + sqe.getSQLState()); System.exit(1); }//end catch
catch (Exception e) { e.printStackTrace();
System.exit(1); }//end catch System.exit(0); } }