I have the following Java code. Purpose of this code is to establish a connection
to a remote MySQL database ProductionDb ( a data source defined in my /etc/odbc.ini file ).
import java.sql.*;
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
Connection conn = null;
PreparedStatement s = null;
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver).newInstance();
conn = DriverManager.getConnection("jdbc:odbc:ProductionDb");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
The /etc/odbc.ini file is:
$ cat /etc/odbc.ini
[ProductionDb]
Driver = /usr/lib/odbc/libmyodbc.so
Description = Production Database
Server = [ hidden ]
Port = 3306
User = [ hidden ]
Password = [ hidden ]
Database = ProductionDb
By the way - I am using Java 7 and Ubuntu :
$java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
$lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 11.04
Release: 11.04
Codename: natty
When I try to run my program I get the following error:
$java Test
java: symbol lookup error: /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libJdbcOdbc.so: undefined symbol: SQLAllocEnv
Does anyone know why I get this error ?
What is wrong here ?
P.S By the way I did run sudo apt-get install unixodbc-dev , sudo apt-get install libmyodbc and sudo apt-get install libmysql-java :-)
UPDATE:
I have also tried the idea suggested in one of the replies below ( by Benny Hill ) : to use the /etc/odbcinst.ini as well as /etc/odbc.ini. Still doesn't work and I get the same error message.
$ cat /etc/odbc.ini
[ProductionDb]
Driver = MySQL Driver
Description = Production Database
Server = [ hidden ]
Port = 3306
User = [ hidden ]
Password = [ hidden ]
Database = ProductionDb
$ cat /etc/odbcinst.ini
[MySQL Driver]
Driver = /usr/lib/odbc/libmyodbc.so
ADDITIONAL NOTE:
I can use this ODBC data source successfully from the R
programming language.
> library(odbc)
> con = odbcConnect("ProductionDb")
> con
RODBC Connection 1
Details:
case=nochange
DSN=ProductionDb
解决方案
Sounds like a missing or mismatched library. Try to debug the ldd processing.
First, check what
$ ldd /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libJdbcOdbc.so
says, do all listed dependencies exist?
Then, try setting LD_DEBUG and and start your Java program again, to see the loader debug.
$ export LD_DEBUG=all
$ java Test