Skip to Content
Author's profile photo Jason Hinsperger

Connecting to SQL Anywhere using JDBC

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?

Assigned Tags

      21 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Jason.

      Can we have an update for SQL Anywhere 17?

      Thank you.

      Author's profile photo Jason Hinsperger
      Jason Hinsperger
      Blog 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;..." );

      Author's profile photo Former Member
      Former Member

      Thank you Jason.

      I found it looking the jar from netbeans...

      By the way, very helpful post.

      Thank you.

      Author's profile photo Former Member
      Former Member

      Jason,Thank you.

      Where do i download the driver?

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Former Member
      Former Member

      Thank you Andreas.

      Author's profile photo Robert Huber
      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

      Author's profile photo Robert Huber
      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

      Author's profile photo Jason Hinsperger
      Jason Hinsperger
      Blog Post Author

      Hi Robert,
      Sorry for the delay in responding.  I am not sure what could be happening- I admittedly not a Mac user at all.  All I can suggest is that you validate that you have configured things correctly.

      If that doesn't help, you might have better luck posting your question on the SQL Anywhere Forum to get more eyes on it.

      --Jason

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Jason Hinsperger
      Jason Hinsperger
      Blog 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).

      Author's profile photo Former Member
      Former Member

      Hi Jason,

      I am using SQL Anywhere 11.0.0.1264.

      I've read your above article. Where should I write the drive class name?

      Below is the print screen of the class path? it coreect ?

      Please help..

      I am trying to connect my database to sap lumira for BI application, which require the JDBC driver for my database...Until now, I have not success in connecting them..

      Thank you very much...Really appreciate that.

      Author's profile photo Jason Hinsperger
      Jason Hinsperger
      Blog Post Author

      In the Add Dataset wizard for Lumira, SQL Anywhere is included as a datasource.  Just use that - it may be a newer version of the driver, but newer SQL Anywhere JDBC drivers should be able to connect to older database servers.

       

      Author's profile photo Former Member
      Former Member

      Where do you put server information in the connection strings?

      Author's profile photo Jason Hinsperger
      Jason Hinsperger
      Blog Post Author

      Use the "ServerName" connection parameter.

      You can find the list of connection parameters here .

       

      Author's profile photo Satish Soni
      Satish Soni

      How can we specify the Charset used for JDBC Connection.

       

      Is it to be specified somewhere in parameters or JDBC configuration in .sbo file under URL Format parameter.

      Author's profile photo Claudiomiro Walter
      Claudiomiro Walter

      hello, where do I get the drive sajdbc4.jar  ?

      Author's profile photo Jason Hinsperger
      Jason Hinsperger
      Blog Post Author

      It comes as part of the SQL Anywhere software, downloadable from the SAP software downloads if you have the entitlement, or you can get the driver from the free developer edition download:

      https://www.sap.com/cmp/td/sap-sql-anywhere-developer-edition-free-trial.html

      Author's profile photo Ravi Condamoor
      Ravi Condamoor

      Hi,

      Am trying to connect to SAP IQ using the SDI Camel Adapter. I tried both jconnect and sajdbc4.jar (sql anywhere) to connect but am getting some errors.

      With sajdbc4.jar, I get

      caught: com.sap.hana.dp.adapter.sdk.AdapterException: Failed to query schemas. Context: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (The sajdbc.jar build does not match the shared object build)

      I have both sajdbc4.jar and libdbjdbc17.so under ....camel/lib

      With jconn4.jar under ../camel/lib, I get

      caught: com.sap.hana.dp.adapter.sdk.AdapterException: Failed to query schemas. Context: java.sql.SQLException: JZ0SB: Parameter index out of range: 1.

      Looks like with jconn4 I was connecting to the db but running into an issue with retrieving the schema/db.

      Anyone successful in connecting to SAP IQ with SDI?  BTW, I was able to connect to IQ with SDA/ODBC driver.

      Thanks

      -ravi

      Author's profile photo Uwe Schuerrle
      Uwe Schuerrle

      Hello ,

      I have problems connecting to a database (hsabiso2.db) via KNIME. The database is on a server in our network. For example, if I use access with Windows ODBC to connect to the database and its tables, from my (client) PC, this works without problems (I have admin rights).

      SQL Anywhere 17 is installed.

      [ODBC 32 bit Drivers]
      HSAB SQL Anywhere 17 (32 bit)=Installed
      [HSAB SQL Anywhere 17 (32 bit)]
      Driver=C:\Program Files (x86)\Hs\Ab\Sa17\Bin64\dbodbc17.dll
      Setup=C:\Program Files (x86)\Hs\Ab\Sa17\Bin64\dbodbc17.dll
      32Bit=1

      hsabiso2.dsf

      DSN=HSAB_HSABISO2
      Driver=HSAB SQL Anywhere 17
      ServerName=hsab_isoloc_s_app02
      DatabaseName= hsabiso2
      CommLinks=TCPIP{}
      Integrated=NO
      MSApplications=YES

      I have Integrated  the driver sap. jdbc4. sqlanywhere. IDriver in the KNIME Database, because in KNIME Windows ODBC data sources is no longer supported out of the box.

      But I don’t know the correct parameterization of the database URL “jdbc: sqlanywhere: .. .”
      I copied (and renamed) the database locally on my PC and tried that, but it doesn’t work with KNIME.
      But with Access the access works here too.

      I got two errors:

      (1) Execute failed: Could not create connection to database: Ungültiger ODBC-Handle
      or
      (2) Execute failed: Could not create connection to database: Connection to database 'jdbc:sqlanywhere://C:\Users\MyUsername\Documents\HSDATENSICH\HSAB_HSABISO2_20210108_165405\HSABisotest.db' timed out

      I would like to say to myself that I am an absolute beginner. There is no JAVA knowledge, not even in SQL.

      I hope I was able to explain the problem reasonably understandably.

      Greetings from germany

      Author's profile photo Jason Hinsperger
      Jason Hinsperger
      Blog Post Author

      Hi Uwe,

      I would suggest you post your question to the SQLAnywhere forum.  This is a great place to ask questions, with many more experts who can help.

      Regards,

      --Jason