4

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.<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$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.

mosquito87
  • 4,270
  • 11
  • 46
  • 77
  • Do your username or password contain non-ASCII characters, by chance? – mustaccio May 22 '14 at 13:09
  • What JRE are you using? It seems that sun.io.UnknownCharacterException is not found, and that might be specific to Sun/Oracle JREs. Have you tried an alternate method, such as instantiating a com.ibm.db2.jcc.DB2SimpleDataSource instance? – nitind May 22 '14 at 14:50
  • I didn't get it working with com.ibm.db2.jcc.DB2SimpleDataSource. Could you provide example code fitting to my case? I'm using JRE 1.4 btw. – mosquito87 May 26 '14 at 06:40

4 Answers4

7

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

Gaurav Swaroop
  • 1,181
  • 11
  • 8
6

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).

Odihase
  • 63
  • 1
  • 6
  • 2
    I had to switch to DB2JCC4 Version 4.19.26 to make it work with AIX+Java8. Got it from IBM: http://www-01.ibm.com/support/docview.wss?uid=swg21363866 – bourbert Nov 05 '15 at 15:58
  • I switched from db2jcc-1.3.1.jar to db2jcc-1.4.2.jar to fix this issue with Java 8. – BigRedBettaFish Jan 23 '17 at 18:44
2

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.

AngocA
  • 7,655
  • 6
  • 39
  • 55
0

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

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Augustine Joseph
  • 2,217
  • 2
  • 16
  • 16