Tuesday, 14 June 2011

DB2 java.sql.SQLException: No suitable driver

Problem:
The following line of code throws the exception below:

url = "jdbc:db2//" + server + ":" + port + "/" + database;
Class.forName("com.ibm.db2.jcc.DB2Driver");
connection = DriverManager.getConnection(url, username, password);

java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:559)
at java.sql.DriverManager.getConnection(DriverManager.java:189)

Resolution:
1-After investigation, I realized this is not caused by missing JARs (as I already added them all) but because the url is not properly formatted. The correct one should be:
url = "jdbc:db2://" + server + ":" + port + "/" + database;

2-The same problem happens too if you omit the line of code below:
Class.forName("com.ibm.db2.jcc.DB2Driver");

So whenever you face this exception, check the syntax of the url connecton and make sure you are loading the appropriate class for your driver before establishing the connection.

For more information, check the following link http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.apdv.java.doc/doc/tjvjcccn.htm

1 comment: