Introduction


JDBC is a set of classes and interfaces written in Java that allows Java programs to access a database. Although Sybase (currently part of SAP), and other commercial vendors offer Sybase JDBC type 4 drivers, there is also a free alternative called jTDS. jTDS supports both Sybase and Microsoft SQL Server.

Download driver package


At the time of testing, I got a package called jtds-1.3.1-dist.zip. I found the latest driver here. We can verify the content of the file, looking for the JAR driver:

fm@susie:~> unzip -l jtds-1.3.1-dist.zip |grep jar
   317816  13-06-08 12:27   jtds-1.3.1.jar

Install the driver package


In order to work with the driver, Java must be able to find it when called. We can achieve this by adding the drivers jar file location to the Java classpath, or by simply placing the driver files into the Java standard directory for extensions, for JAVA JRE this is the $JAVA_HOME/jre/lib/ext (i.e. /usr/lib64/jvm/jre-1.6.0-ibm/lib/ext/) directory. We need to copy the extracted jtds-1.3.1.jar file.

Use the driver to access Sybase through Java


The following example code JdbcTestSybase.java can be used to quickly access and test the JDBC connection. Sybase does not have a well-defined default port. TCP-5000 is the proposed installer value in version 15.7 (TCP-7100 is another frequently used value). Due to its close relation to Microsoft SQL Server, Sybase has a similar structure: a "master" system database and a "sa" superuser.

vi JdbcTestSybase.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;

class JdbcTestSybase {
   public static void main (String args[]) {
      try {
         Class.forName("net.sourceforge.jtds.jdbc.Driver");
      }
      catch (ClassNotFoundException e) {
         System.err.println (e);
         System.exit (-1);
      }
      try {
         // open connection to database
         Connection connection = DriverManager.getConnection(
         //jdbc:jtds:sybase://localhost:5000/dbname;user=dbuser;password=dbpwd;
         "jdbc:jtds:sybase://192.168.101.78:5000/master;user=sa;password=pass;");

         // build query, here we use the internal table "dbo.sysdatabases"
         String query = "SELECT * From sysdatabases";

         // execute query
         Statement statement = connection.createStatement ();
         ResultSet rs = statement.executeQuery (query);

         // return query result
         while ( rs.next () )
            // display content from column "name"
            System.out.println ("Sybase Query result: " + rs.getString ("name"));
         connection.close ();
      }
      catch (java.sql.SQLException e) {
         System.err.println (e);
         System.exit (-1);
      }
   }
}

Compile and test run


fm@susie:~> javac JdbcTestSybase.java
fm@susie:~> java JdbcTestSybase
Sybase Query result: master
Sybase Query result: model
Sybase Query result: tempdb
Sybase Query result: sybsystemdb
Sybase Query result: sybsystemprocs
Sybase Query result: pubs2
Sybase Query result: pubs3

I tested it against Sybase Adaptive Server Enterprise (ASE) version 15.7. Should the test run fail, typical reasons are:

Additional Notes


The original, non-free vendor-provided Sybase JDBC driver is jconn4.jar. The current product name is jConnect 7 and installs with the server. I had no chance to test, but above example should work by simply switching the driver to: Class.forName("com.sybase.jdbc4.jdbc.SybDriver"); and the JDBC URL Format to Connection connection = DriverManager.getConnection("jdbc:sybase:Tds:192.168.101.78:5000?ServiceName=master", "sa", "pass");.

Additional Literature: Sybase Adaptive Server Enterprise (ASE) 15.7