Solution for Old Sybase Connection problem- ODBC ...

137 downloads 0 Views 695KB Size Report
Sales simulator is a Java app that mainly connects to Sybase database and updates it periodically based on some requirements. • For ex: UPDATE ...
Java Sybase Database Connection Problems: ODBC & JDBC Ahmad Karawash

Versions • We have two versions of Sybase SQL anywhere installed in two windows machines. • First version is: 10.0.1.4310

• Second version is:10.0.1.4181

Use Case 10.0.1.4310

Sybase server Windows server 1

Client

Sales Simulator (Java application that update the database periodically)

10.0.1.4181

Sybase server Windows server 2

Sales Simulator job • Sales simulator is a Java app that mainly connects to Sybase database and updates it periodically based on some requirements. • For ex: UPDATE POSHEADER SET NUMCUST = " + NUMCUST + " WHERE TRANSACT =" + ID ;

Options to connect to Sybase database • JDBC-ODBC Bridge • This method requires odbc configuration and use of jodbc.jar driver

• JDBC • This method requires use of jdbc.jar driver only

Java Connect via JDBC-ODBC

JDBC-ODBC • Each Sybase version has its own jodbc.jar file located on:

Example of ODBC Sybase configuration

Import jar library to our code

Java connection code • • • • •

try { Class.forName("ianywhere.ml.jdbcodbc.jdbc3.IDriver"); }catch (ClassNotFoundException e) { System.out.println(e.getMessage());} try { dbConnection = DriverManager getConnection("jdbc:ianywhere:uid=username;pwd=password;:Driver=SQL Anywhere 10;DSN=P..Sqlbase;");

• return dbConnection; • } catch (SQLException sqlex) { • log.warn("SQL Exception in Sybase connection. Dropping connection." + sqlex.toString()); • dbConnection.close(); • }

Java Update Code • dbConnection = getDBConnection(); • Statement stmt = dbConnection.createStatement(); • String query = "UPDATE POSHEADER SET NUMCUST = " + NUMCUST + " WHERE TRANSACT =" + ID; • ResultSet rs = stmt.executeQuery(query);

JDBC-ODBC connection issue • Our java code will connect only to one of the Sybase servers. • Each Sybase version has its own jodbc driver to be used to get successful connect.

Result of connection to first Sybase server

Result of connection to second Sybase server

Recommendation • I didn’t find a common JODBC Driver that works on both Sybase servers

Java Connect via JDBC

Import JDBC jconn2.jar

Java connection code • try { • Class.forName("com.sybase.jdbc3.jdbc.SybDriver"); • } catch (Exception e) { • System.out.println("Fail to initialize Sybase JDBC driver: " + e.toString() + "

"); • } • • String dbUser = “username"; • String dbPasswd = “pass"; • String dbURL = "jdbc:sybase:Tds:localhost:2638/database";

Java Update Code • dbConnection = getDBConnection(); • Statement stmt = dbConnection.createStatement(); • String query = "UPDATE POSHEADER SET NUMCUST = " + NUMCUST + " WHERE TRANSACT =" + ID; • ResultSet rs = stmt.executeQuery(query);

JDBC-ODBC connection issue • The connection works fine on both Sybase versions. • But the update query works only on one version.

Sybase 10.0.1.4310

Sybase 10.0.1.4181

Solution • Issue is on the update annotation itself. Use the below annotation it works fine on both versions: • dbConnection = getDBConnection(); • Statement stmt = dbConnection.createStatement(); • String query = "UPDATE POSHEADER SET NUMCUST = ? WHERE TRANSACT =?"; • PreparedStatement preparedStmt = dbConnection.prepareStatement(query); • preparedStmt.setInt (1, NUMCUST); • preparedStmt.setInt(2, ID); • preparedStmt.executeUpdate();

Result • Use JDBC Driver instead of JDBC-ODBC bridge • Use “preparedstatement” annotation for update