Friday, November 11, 2011

java.sql.SQLException: No suitable driver found

If you get below mentioned error

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:db2://localhost:3306/Test
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)


There could be two possible reasons.

1. Correct driver is not loaded, If this is the case make sure you pass correct Driver for getConnection() method.

2. URL passed is not properly constructed, If DriverManager do not understands the URL sent it throws above error.

In my case this was the issue, It was working well in my local machine but when I deploy the code on my production server (linux server) it used to throw above error. It was working on local machine because IBM client was installed on it.

You can verify on your local machine, probably on below mentioned path
C:\Program Files\IBM\SQLLIB\java

I was using App JDBC Driver :

Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
con=DriverManager.getConnection("jdbc:db2:Test","uername","passowrd");

Jar file : db2java.jar

Here local machine is able to understand alias "Test" because IBM client was installed as mentioned above. But server was not able to understand the alias hence it was throwing above error.

I solved the problem by using DB2 Universal Driver

Class.forName("com.ibm.db2.jcc.DB2Driver");
con=DriverManager.getConnection("jdbc:db2://hostname:port/Schema","username","password");

Jar file : db2jcc.jar


To download Jar files : Click here


Aslo there is one more solution for this.

Possibly there could be IBMdb2 installed on your server / local machine but while running program it is not able get the driver.

Solution is load the profile, in a .bat or .sh file as shown below.


#!/bin/ksh -x
. /apps/IBMdb2/db2v8/sqllib/db2profile 

java -classpath db2java.jar:. TestDB2

Here TestDB2 is .class file of JAVA program.