So, ...  I like to try new things and since I had to migrate an old version of an SQL Anywhere database into an open source database, I thought I might as well do it the fun way.

I will write a little Java Spring Batch Applicaiton in order to move the data from SQL Anywhere into MariaDB in a professional way.

In this article you can learn how to connect your client to an SQL Anywhere database using Java.

Note, this is only tested for SQL Anywhere 16. Connection Strings and library versions might differ in older or newer versions as always undecided.

Preparations Java Path and Libraries

First you need to locate your SQL Anywhere installation and in there the Java directory.

This directory also holds legacy jars to support older versions, but we are interested in the sajdbc4.jar.

Copy this file ...

 

... and paste it into the root of your Java project.

Open the Java project properties,

go to Java Build Path

and select the tab Libraries, 

Click on Add JARs... and select the just copied JAR from your projects root directory.

Click on OK and the JAR has been added to your projects build path.

This makes the classes from the JAR available in the project scope.

For some reason I still had to add the following path to the Windows PATH variable, in order to get the connection to work.

C:\Program Files\SQL Anywhere 16\Bin64\dbjdbc16.dll

Reason is that the database driver for SQL Anywhere is not a pure Java driver and it requires a native library in addition.

In case you do not reference the library, you will face exceptions like this.

Exception in thread "main" java.lang.UnsatisfiedLinkError: no dbjdbc12 in java.library.path at java.lang.ClassLoader.loadLibrary
(Unknown Source) at java.lang.Runtime.loadLibrary0
(Unknown Source) at java.lang.System.loadLibrary
(Unknown Source) at sybase.jdbc4.sqlanywhere.IDriver.try_load
(IDriver.java:455) at sybase.jdbc4.sqlanywhere.IDriver.
(IDriver.java:396) at sun.reflect.NativeConstructorAccessorImpl.newInstance0
(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance
(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance
(Unknown Source) at java.lang.reflect.Constructor.newInstance
(Unknown Source) at java.lang.Class.newInstance
(Unknown Source) at java.util.ServiceLoader$LazyIterator.nextService
(Unknown Source) at java.util.ServiceLoader$LazyIterator.next
(Unknown Source) at java.util.ServiceLoader$1.next
(Unknown Source) at java.sql.DriverManager$2.run
(Unknown Source) at java.sql.DriverManager$2.run
(Unknown Source) at java.security.AccessController.doPrivileged
(Native Method) at java.sql.DriverManager.loadInitialDrivers
(Unknown Source) at java.sql.DriverManager.(Unknown Source)
...

However, this is not enough. You also have to add the library (for Windows a dynamic link library DLL) to the Java library path.

I just chose to load the library before every execution of the program using the following command.

System.loadLibrary("dbjdbc16");

If you follow these instructions, it should work fine.

Source Code

Below a little JUnit test case, which shows how you can connect to an SQL Anywhere database using Java.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package sql.anywhere.test;
 
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
 
import org.junit.jupiter.api.Test;
 
class TestSqlAnywhere {
 
	@Test
	void testConnection() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		try {
			System.loadLibrary("dbjdbc16");
			DriverManager.registerDriver((Driver) Class.forName("sybase.jdbc4.sqlanywhere.IDriver").newInstance());
			Connection con = DriverManager.getConnection("jdbc:sqlanywhere:UID=DBA;PWD=osd;eng=osddb");
			System.out.println(con.isClosed());
 
			con.close();
		} catch (java.lang.UnsatisfiedLinkError ex) {
			System.out.println(ex.getMessage());
		}
	}
 
}

SQL Anywhere / JDBC Connection Strings 

The following table is from https://blogs.sap.com/2014/05/02/connecting-to-sql-anywhere-using-jdbc-2/.

The author also explains a lot more on how to connect to the different versions of SQL Anywhere via JDBC.

However, I think I remember that I still had to register the Driver for version 16.0 although he states that this is not necessary for JDBC 4.0.

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

References

An SAP blog entry explaining how to connect to different SQL Anywhere versions using Java and JDBC drivers.

https://blogs.sap.com/2014/05/02/connecting-to-sql-anywhere-using-jdbc-2/

A stackoverflow chat regarding the Linked Library to support the JDBC Driver.

https://stackoverflow.com/questions/32672407/error-connecting-java-to-sql-anywhere-database

Hope it helps!