eclipse eclipse中通过jdbc连接DB2数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23801841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect to DB2 database in eclipse via jdbc
提问by mosquito87
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 ...
我正在尝试通过 JDBC 连接到带有 Eclipse(版本 Juno)的 IBM DB2 数据库。我已将驱动程序(外部 jar 文件)添加到我的项目中,并且驱动程序已正确加载...
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:
我也知道连接数据(数据库路径、用户名、密码)是正确的。但我得到一个 java.lang.NoClassDefFoundError:
Exception in thread "main" java.lang.NoClassDefFoundError: sun/io/UnknownCharacterException
at com.ibm.db2.jcc.b.a.<init>(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.<init>(p.java:404)
at com.ibm.db2.jcc.b.b.<init>(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.run(Unknown Source)
at java.net.URLClassLoader.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.
项目属性肯定有问题,但我真的不知道去哪里找。
回答by Gaurav Swaroop
I was getting the same error. I was running it using java 8, I switched to Java 7 and it worked.
我遇到了同样的错误。我使用 java 8 运行它,我切换到 Java 7 并且它工作正常。
回答by Odihase
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).
你用的是什么版本的驱动?我在 Java 1.8 和驱动程序版本 2(名为 db2jcc.jar)上遇到了同样的问题。使用驱动程序版本 4(名为 db2jcc4.jar),它可以完美运行。请确保,当使用例如 tomcat 时,只有正确的驱动程序才持久存在于 lib 目录中。如果两个驱动程序都是持久的,它将严格使用旧的(版本 2)。
回答by AngocA
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.
尝试使用来自驱动程序的直接连接,具有相同的类路径和用户。这样就可以保证你的环境配置正确,问题出在Java上。
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.
您甚至可以在 Eclipse 中配置这一行,方法是在“运行...”对话框中传递参数,并使用与运行应用程序相同的类路径。
回答by Augustine Joseph
After I change the DB2 jar to db2jcc4-9.7.jar
it works correctly without any issue.
在我更改 DB2 jar 后,db2jcc4-9.7.jar
它可以正常工作,没有任何问题。
The JDK version that I am using is 1.8.0_131
我使用的 JDK 版本是 1.8.0_131