I originally posted this a few years ago to help people configure their Java applications to use the iAnywhere JDBC driver, which was replaced by the SQL Anywhere JDBC driver in SQL Anywhere version 12.  I am reposting it here with a few minor updates, since I still refer to it occasionally and I think it is still useful.

I have heard from customers that connecting to SQL Anywhereover JDBC can be difficult at times.  In my investigations of this, I have found that this is almost always due to confusion over the classname to use to register the JDBC driver, and the URL’s to use to actually connect to the database.  In stepping back, I can see how people might easily get confused based on the history of the JDBC driver.  Here is my attempt to clarify things by following the history of the driver, starting with SQL Anywhere version 9.

Before I go into detail on the history of the SQL Anywhere JDBC driver, here is a table which explains classpath settings, jar files required, driver name URLs and sample connection URLs.

SQL Anywhere Version JDBC jar file to include in classpath Driver classname Connection URL
9.0.2 %ASA90%\java\jodbc.jar ianywhere.ml.jdbcodbc.IDriver jdbc:odbc:Driver=Adaptive Server Anywhere 9.0;UID=DBA;PWD=sql;eng=demo
10.0.0 %SQLANY10%\java\jodbc.jar ianywhere.ml.jdbcodbc.jdbc3.IDriver jdbc:odbc:Driver=SQL Anywhere 10 Demo;UID=DBA;PWD=sql;eng=demo
10.0.1 %SQLANY10%\java\jodbc.jar ianywhere.ml.jdbcodbc.jdbc3.IDriver jdbc:ianywhere:Driver=SQL Anywhere 10;DSN= SQL Anywhere 10 Sample
11.0.0 %SQLANY11%\java\jodbc.jar ianywhere.ml.jdbcodbc.jdbc3.IDriver jdbc:ianywhere:Driver=SQL Anywhere 10;DSN= SQL Anywhere 11 Sample
11.0.1 %SQLANY11%\java\sajdbc.jar sybase.jdbc.sqlanywhere.IDriver jdbc:sqlanywhere:uid=DBA;pwd=sql;eng=demo
12.0.0 %SQLANY12%\java\sajdbc4.jar no longer required for JDBC 4.0 jdbc:sqlanywhere:uid=DBA;pwd=sql;eng=demo
16.0 %SQLANY16%\java\sajdbc4.jar no longer required for JDBC 4.0 jdbc:sqlanywhere:uid=DBA;pwd=sql;eng=demo
  1. Adaptive Server Anywhere version 9.0 (aka SQL Anywhere 9.0)
    In version 9, SQL Anywhere supported JDBC 2.0 using an iAnywhere generic JDBC-ODBC bridge driver (similar to but different from the Sun JDBC/ODBC driver).  The jar file is jodbc.jar, and resides in the %ASA90%\java directory.  To use the iAnywhere JDBC driver, you need to include the jar in your classpath.  Then, you need to register it in your java app using the following code:

    DriverManager.registerDriver(
        (Driver)Class.forName( "ianywhere.ml.jdbcodbc.IDriver" ).newInstance() );

    Since the iAnywhere JDBC/ODBC driver is a bridge driver, to connect to your SQL Anywhere database, you need to specify a “DRIVER=” parameter along with the rest of your connect string.  For example:

    Connection con = DriverManager.getConnection(
        "jdbc:odbc:Driver=Adaptive Server Anywhere 9.0;UID=DBA;PWD=sql;eng=demo" );

    or, you could use an ODBC data source like this:

    Connection con = DriverManager.getConnection(
        "jdbc:odbc:DSN= Adaptive Server Anywhere 9.0 Sample" );

  2. SQL Anywhere 10.0.0
    In version 10, we added support for JDBC 3.0.  To use the version 10 iAnywhere JDBC/ODBC bridge driver, you need to again include %SQLANY10%\java\jodbc.jar in your classpath.  However, the class name for driver registration is slightly different:

    DriverManager.registerDriver(
        (Driver)Class.forName( "ianywhere.ml.jdbcodbc.jdbc3.IDriver" ).newInstance() );

    Once registered, the connection URL was the same as in verison 9, above.

  3. SQL Anywhere 10.0.1
    After version 10 was released, we noticed that in some customer issues involving JDBC, the iAnywhere driver was not always being loaded when it was supposed to be, particularly when the Sun JDBC/ODBC bridge driver was present.  It turns out that our use of “jdbc:odbc” in the connection URL was not sufficient to guarantee that the iAnywhere driver would be used during a connection.  If the Sun bridge were present, it could be picked up and used instead, which lead to all sorts of unexpected behaviour. To resolve this problem, the 10.0.1 maintenance release introduced a new URL header for the iAnywhere driver, “jdbc:ianywhere”.  From this point forward, the URL to register the driver was the same as with v10, but the correct URL to use when connecting to the database was as follows:

    Connection con = DriverManager.getConnection(
        "jdbc:ianywhere:Driver=SQL Anywhere 10;DSN= SQL Anywhere 10 Sample" );

    The “jdbc:ianywhere” portion of the connection string was actually back-ported to a 9.0.1 ebf, so if you are running one of the later 9.0.1 or 9.0.2 ebfs, the above connection URL will work for you as well.

  4. SQL Anywhere version 11.0.0
    In SQL Anywhere version 11, there was no change in classname for the driver or URL for the connection string, but we did update to a newer version of the JDK. This meant we had to drop the JDBC 2.0 driver, because JDK 1.4 and newer no longer supported it.  To make things easier for our customers, we kept the JDBC 2.0 class names in the version 10 JDBC 3.0 jar.  They simply pointed to the JDBC 3.0 equivalents.

  5. SQL Anywhere 11.0.1
    In SQL Anywhere version 11.0.1, a new SQL Anywhere JDBC driver was introduced.  No longer a generic iAnywhere JDBC driver, it is a JDBC driver specific to SQL Anywhere.  This was done to make it easier (ie. less confusing) for people to use JDBC with SQL Anywhere.  With the new driver, there is no need to install ODBC on the system.  This wasn’t a problem for Windows, but our Linux and Unix customers often had problems with this.  As an added bonus, the performance of the driver was improved slightly because we no longer have to go through the ODBC driver manager. This change involved adding 2 new files to the SQL Anywhere installation:  sajdbc.jar and dbjdbc11.dll. To use the new driver, you need to include %SQLANY11%\java\sajdbc.jar in your classpath.  Then, the driver registration is as follows:

    DriverManager.registerDriver(
       (Driver) Class.forName( "sybase.jdbc.sqlanywhere.IDriver" ).newInstance() );

    Then, to connect, you use the following URL:

    Connection con = DriverManager.getConnection(
        "jdbc:sqlanywhere:uid=DBA;pwd=sql;eng=demo" );
  6. SQL Anywhere 12/16
    SQL Anywhere 12 deprecated the use of the iAnywhere JDBC/ODBC bridge driver in favor of the new SQLAnywhere driver.  In addition, SQL Anywhere 12 will support JDBC 4.0 (which requires JDK 1.6 or newer). To continue to use the JDBC 3.0, users do not have to make any changes from previous versions.  However, to use the JDBC 4.0 support, the new driver name is “sybase.jdbc4.sqlanywhere.IDriver”, and requires that %SQLANY12%\java\sajdbc4.jar be in your classpath. However, there is no need to call DriverManager.registerDriver(…) to register the driver before using it anymore.  Sun has implemented automatic driver registration so that applications just need to make sure that sajdbc4.jar is in the classpath (and dbjdbc12.dll is in the path), and use the “jdbc:sqlanywhere” URL header to connect.  So, to connect with SQL Anywhere 12 and JDBC 4.0, all you need is something like the following line of code:

    Connection con = DriverManager.getConnection(
        "jdbc:sqlanywhere:uid=DBA;pwd=sql;eng=demo" );

That concludes our history lesson. Confused yet?

To report this post you need to login first.

11 Comments

You must be Logged on to comment or reply to a post.

    1. Jason Hinsperger Post author

      The only difference is the driver name, which is now:

      sap.jdbc4.sqlanywhere.IDriver”


      v17 connection strings are the same as v12/16:

      Connection con = DriverManager.getConnection( “jdbc:sqlanywhere:UserID=DBA;Password=passwd;…” );

      (0) 
        1. Andreas Mykonios

          Hi.

          The driver should be part of SQL Anywhere’s installation. Also there is a tool called deploy to windows which lets you create a package with everything you need to deploy to the client. You may find a link in SQL Anywhere xx\Administration Tools.

          This is a screenshot.

          Andreas.

          (0) 
      1. Robert Huber

        I am trying to use the native driver on OS X 10.11.5 but I am not successful till now. I made one step further since entering the new driver name you mention, i. e. sap.jdbc4.sqlanywhere.IDriver.

        You say the dbjdbc12.dll (libdbjdbc17.dylib on OS X). I put this file at the same location as the sajdbc4.jar file. Also placed the other files mentioned here:

        http://dcx.sap.com/index.html#sqla170/en/html/815de54e6ce21014b42afce8c8d9eef9.html

        to the same place. Still doesn’t work. I get the error:

        What’s wrong?

        Regards,
        Robert

        (0) 
        1. Robert Huber

          i found that I used the sajdbc.jar from version 17.0.0. I made sure now that I have all files from from the latest 17.0.4.2129 installation. Now, I get following error:

          Could it be that the lib… files are still at the wrong place? Where should they be on OS X?

          Regards, Robert

          (0) 
  1. Arun Kumar

    import java.io.*;
    import java.sql.*;
    import com.sybase.jdbc4.jdbc.*;

    public class JDBCConnect
    {
    public static void main( String args[] )
    {
    try
    {
    String arg;
    Connection con;

    // Select the JDBC driver and create a connection.
    // May throw a SQLException.
    // Choices are:
    // 1. jConnect driver
    // 2. SQL Anywhere JDBC 3.0 driver
    // 3. SQL Anywhere JDBC 4.0 driver
    arg = “jdbc4”;
    if( args.length > 0 ) arg = args[0];
    if( arg.compareToIgnoreCase( “jconnect” ) == 0 )
    {
    System.out.println(“jconnect started”);
    con = DriverManager.getConnection(
    “jdbc:sybase:Tds:localhost:2638”, “DBA”, “sql”);
    }
    else
    {
    con = DriverManager.getConnection(
    “jdbc:sqlanywhere:uid=DBA;pwd=sql” );
    }

    System.out.println(“Using “+arg+” driver”);

    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery(
    “SELECT test_id, unitnm, addrm1 FROM mill”);

    while (rs.next())
    {
    int value = rs.getInt(1);
    String FirstName = rs.getString(2);
    String LastName = rs.getString(3);
    }
    rs.close();
    stmt.close();
    con.close();
    }
    catch (SQLException sqe)
    {
    System.out.println(“Unexpected exception : ” +
    sqe.toString() + “, sqlstate = ” +
    sqe.getSQLState());
    System.exit(1);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    System.exit(1);
    }

    System.exit(0);
    }
    }
    D:\arun\java\tesnomax>java JDBCConnect
    Using jdbc4 driver
    6304 null null
    D:\arun\java\tesnomax>java JDBCConnect jconnect
    jconnect started
    Unexpected exception : java.sql.SQLException: JZ006: Caught IOException: java.ne
    t.ConnectException: Connection refused: connect, sqlstate = JZ006

    I have been receiving this for for JConnect driver.But it is working with jdbc4. Please update in this regard.

    (0) 
    1. Jason Hinsperger Post author

      Are you using the database engine or server?  If you are using the database engine (dbeng*), then you need to make sure you start the tcpip link (-x tcpip).

      (0) 

Leave a Reply