cancel
Showing results for 
Search instead for 
Did you mean: 

Having trouble connection to SQL Anywhere 17 with sajdbc4.jar driver

Former Member
5,744

Hi,

I successfully installed SQL Anywhere 17 on a Linux environment. I am able to connect to it with various client applications, using the jconn4.jar driver. However, I have been directed by the customer to use the sajdbc4.jar driver, and I am running into problem.

I developed a simple Java application to try and figure out what I'm doing wrong; I have the source file below.

I run it from the command line like this:

$ java -classpath .:./sajdbc4.jar -Djava.library.path=/opt/sqlanywhere17/lib64 DriverTest

I get the following error:

Connecting to database... java.sql.SQLException: Invalid ODBC handle at sap.jdbc4.sqlanywhere.IDriver.makeODBCConnection(Native Method) at sap.jdbc4.sqlanywhere.IDriver.connect(IDriver.java:809) at java.sql.DriverManager.getConnection(DriverManager.java:571) at java.sql.DriverManager.getConnection(DriverManager.java:215) at DriverTest.main(DriverTest.java:26)

Line 26 corresponds to the line where I'm calling the DriverManager.getConnection method.

I've tried various combinations of JDBC URL values, and I have made sure that all the shared libraries that sajdbc4.jar needs are indeed in the /opt/sqlanywhere17/lib64 DriverTest folder.

Any ideas as to how I can solve this problem?

thanks, Gonzalo

import java.sql.*;

public class DriverTest {

static final String JDBC_DRIVER = "sap.jdbc4.sqlanywhere.IDriver";

//static final String DB_URL = "jdbc:sqlanywhere:uid=DBA;pwd=sql;links=tcpip(host=127.0.0.1);eng=demo;port=2638";
static final String DB_URL = "jdbc:sqlanywhere:links=tcpip(host=127.0.0.1);eng=demo;port=2638";
//static final String DB_URL = "jdbc:sqlanywhere:ServerName=localhost;eng=demo;port=2638";
//static final String DB_URL = "jdbc:sqlanywhere:uid=DBA;pwd=sql;eng=demo;port=2638";

//static final String DB_URL = "jdbc:sqlanywhere:UserID=DBA;Password=sql;eng=demo;host=localhost;port=2638";

static final String USER = "DBA";
static final String PASS = "sql";

public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    try {
        Class.forName(JDBC_DRIVER);

        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        //conn = DriverManager.getConnection(DB_URL);

        System.out.println("Creating statement...");
        stmt = conn.createStatement();
        String sql;
        sql = "SELECT PersonId, LastName, FirstName, Address, City FROM Persons";
        ResultSet rs = stmt.executeQuery(sql);

        while (rs.next()) {
            int id  = rs.getInt("PersonId");
            String lastName = rs.getString("LastName");
            String firstName = rs.getString("FirstName");
            String address = rs.getString("Address");
            String city = rs.getString("City");

            System.out.print("ID: " + id);
            System.out.print(", Last Name: " + lastName);
            System.out.print(", First Name: " + firstName);
            System.out.println(", Address: " + address);
            System.out.println(", City: " + city);
        }
        rs.close();
        stmt.close();
        conn.close();
    } catch (SQLException se) {
        se.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException se2) {
        }// nothing we can do
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }
    System.out.println("Goodbye!");
}

}

0 Kudos

There is a discussion about this happening under Windows at http://sqlanywhere-forum.sap.com/questions/16238/error-connecting-remotely-using-sajdbc4jar. Don't know if it will be helpful to you or not.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Folks,

I had to set an environment variable called LD_LIBRARY_PATH, and have it point to the directory with the shared libraries for SQL Anywhere 17. Using the java.library.path JVM parameter wasn't right.

So I was able to run it like this:

$ export LD_LIBRARY_PATH=/opt/sqlanywhere17/bin64

$ java -classpath .:./sajdbc4.jar DriverTest

thanks, Gonzalo

Answers (1)

Answers (1)

Former Member

This should help you out. http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc01776.1600/doc/html/san13...

Billy

How to Load the SQL Anywhere JDBC 4.0 Driver

Ensure that the SQL Anywhere JDBC 4.0 driver is in your class file path.

set classpath=%IQDIR%\\java\\sajdbc4.jar;%classpath%

The JDBC 4.0 driver takes advantage of the new automatic JDBC driver registration. The driver is automatically loaded at execution startup when it is in the class file path. Required Files

The Java component of the SQL Anywhere JDBC 4.0 driver is included in the sajdbc4.jar file installed into the Java subdirectory of your SAP Sybase IQ installation. For Windows, the native component is dbjdbc16.dll in the bin32 or bin64 subdirectory of your SAP Sybase IQ installation; for Unix, the native component is libdbjdbc16.so. This component must be in the system path.