这是我尝试连接 Db2 的班级:
package Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Db2Connection {
public static void main(String[] args) {
Connection conn = null;
String driverName = "COM.ibm.db2.jdbc.app.DB2Driver";
String db2ConnectString = "jdbc:db2:DB2XAT33;UID=G37RSF4;PWD=HOT22RST";
try {
Class.forName(driverName);
conn = DriverManager.getConnection(db2ConnectString);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("ClassNotFoundException Message -- "+ e.getMessage());
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("SQLException Message -- " + e.getMessage());
e.printStackTrace();
}catch (UnsatisfiedLinkError e) {
// TODO Auto-generated catch block
System.out.println("UnsatisfiedLinkError Message -- " + e.getMessage());
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("General Exception Message -- " + e.getMessage());
e.printStackTrace();
}
}
}
当我运行它时,我得到以下异常:
java.lang.UnsatisfiedLinkError: SQLAllocEnv
at COM.ibm.db2.jdbc.app.DB2Driver.<init>(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2Driver.<clinit>(Unknown Source)
at java.lang.Class.forName1(Native Method)
at java.lang.Class.forName(Class.java:142)
我在文件夹“C:\Program Files\IBM\SQLLIB\java\”中有 Db2.zip。我通过 JavaBuildpath 将 Db2.zip 和 ojdbc.jar 添加到了我的包中。