Connect to DB2 database in eclipse via jdbc

匿名 (未验证) 提交于 2019-12-03 01:14:02

问题:

I'm trying to connect to an IBM DB2 database with Eclipse (version Juno) via JDBC. I've added the drivers (external jar files) to my project and the driver is loaded correctly ...

public static void main(String[] args) throws SQLException, ClassNotFoundException {     Class.forName("com.ibm.db2.jcc.DB2Driver");     System.out.println("Driver loaded");      Connection dbConn = DriverManager.getConnection("jdbc:db2://***.**.***.*:50000/BWUEBDB", "username", "password");     System.out.println("Connected"); }

I also know that the connection data (database path, username, password) is correct. But I get a java.lang.NoClassDefFoundError:

Exception in thread "main" java.lang.NoClassDefFoundError: sun/io/UnknownCharacterException     at com.ibm.db2.jcc.b.a.(a.java:238)     at com.ibm.db2.jcc.b.b.a(b.java:1624)     at com.ibm.db2.jcc.c.p.a(p.java:350)     at com.ibm.db2.jcc.c.p.(p.java:404)     at com.ibm.db2.jcc.b.b.(b.java:256)     at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:163)     at java.sql.DriverManager.getConnection(Unknown Source)     at java.sql.DriverManager.getConnection(Unknown Source)     at AppTest.main(AppTest.java:17) Caused by: java.lang.ClassNotFoundException: sun.io.UnknownCharacterException     at java.net.URLClassLoader$1.run(Unknown Source)     at java.net.URLClassLoader$1.run(Unknown Source)     at java.security.AccessController.doPrivileged(Native Method)     at java.net.URLClassLoader.findClass(Unknown Source)     at java.lang.ClassLoader.loadClass(Unknown Source)     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)     at java.lang.ClassLoader.loadClass(Unknown Source)     ... 9 more

There must be something wrong with the project properties but I really don't know where to look for.

回答1:

I was getting the same error. I was running it using java 8, I switched to Java 7 and it worked.



回答2:

What Version of driver do you use? I encountered the same problem with Java 1.8 and driver version 2 (named db2jcc.jar). Using the driver version 4 (named db2jcc4.jar) it works flawlessly. Be sure, when using e.g. tomcat, that only the right driver is persistent in the lib-directory. If both drivers are persistent, it will strictly use the old one (version 2).



回答3:

Try to use the direct connection from the driver, having the same classpath and user. In this way you can ensure that your environment is correctly configured, and the problem is from Java.

http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.apdv.java.doc/src/tpc/imjcc_rjv00004.html

java com.ibm.db2.jcc.DB2Jcc -url jdbc:db2://mysys.myloc.svl.ibm.com:446/MYDB -user db2user -password db2pass -tracin

You can even configure this line in Eclipse, by passing the parameters in the "Run..." dialog, and using the same classpath as you run your app.



回答4:

After I change the DB2 jar to db2jcc4-9.7.jar it works correctly without any issue.

The JDK version that I am using is 1.8.0_131



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!