Reputation: 3692
I have the simplest JDBC program to read data from a DB2 (or other configured) instance. When I attempt to run it from Eclipse OR from the command line, the connect fails, throwing a SQLexception
Class.forName(jdbcDriverClassName).newInstance();
The class.forName fails, using com.ibm.db2.jcc.DB2Driver
. I configured Eclipse to point to the same driver that DBVisualizer uses (both as a boot library and for the project). The program uses the same connect string that DBVisualizer uses. DBVisualizer isn't having any trouble.
Ouptut w/full stack (note the FQN for the driver is not null):
V-- !null driver name --V
Attempting to load com.ibm.db2.jcc.DB2Driver
ERROR:java.lang.ExceptionInInitializerError: null
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.ibm.db2.jcc.DB2Driver.class$(DB2Driver.java:58)
at com.ibm.db2.jcc.DB2Driver.<clinit>(DB2Driver.java:61)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.onlineretailer.ecomm.dumporder.DumpOrder.main(DumpOrder.java:79)
Caused by: java.lang.NullPointerException
at com.ibm.db2.jcc.am.ib.i(ib.java:490)
at com.ibm.db2.jcc.am.ib.<clinit>(ib.java:420)
... 7 more
I also tried adding the .newInstance() to the end of the class.forName() and it continues to execute the same behavior.
Thanks to everyone for your time and input!
Upvotes: 2
Views: 2868
Reputation: 1109635
SQLException: No suitable driver found for jdbc:db2:...
This exception has only 2 possible causes:
The JDBC driver is unknown with DriverManager
. I.e. the JDBC driver has not registered itself properly by DriverManager#registerDriver()
.
The JDBC URL is unknown for any of the JDBC drivers registered with DriverManager
. I.e. the Driver#acceptsURL()
hasn't returned true
for any of the registered drivers.
Your JDBC driver class name is perfectly fine. Your JDBC URL is perfectly fine.
A well designed JDBC driver registers itself with DriverManager
in a static {}
initializer block which get executed upon Class#forName()
. However, older versions of the IBM DB2 JDBC driver registers itself in the constructor instead. For those broken drivers you need to call newInstance()
afterwards as well.
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
Update as per your question update, the newInstance()
did the trick. It however leads to a new problem:
Caused by: java.lang.NullPointerException
at com.ibm.db2.jcc.am.ib.i(ib.java:490)
at com.ibm.db2.jcc.am.ib.<clinit>(ib.java:420)
It turns out to be another bug in the DB2 JDBC Driver itself. Upgrade it.
Upvotes: 4
Reputation: 309008
"No suitable driver" usually means that the connection URL isn't correct for the JAR you're using.
Upvotes: 0
Reputation: 240966
You need to load driver too in your jvm , using Class.forName()
You need to load following driver
com.ibm.db2.jcc.DB2Driver
Here is how it fully look like
String databaseURL = "jdbc:derby:net://localhost:1527/sample";
// Load DB2 Driver for JDBC class
Class.forName("com.ibm.db2.jcc.DB2Driver");
// Set user and password properties
Properties properties = new Properties();
properties.put("user", "APP");
properties.put("password", "APP");
properties.put("retreiveMessagesFromServerOnGetMessage", "true");
// Get a connection
Connection conn = DriverManager.getConnection(databaseURL, properties);
Reference
Upvotes: 1