Java Application connecting to a DB2 instance – Solution to Sql Exception No Suitable driver found

Ok, so this one bugged me for a while, and it was only a throw away comment on a forum  that gave me the clue.

I have written a simple Java app to connect to a DB2 database to query the data and perform some analytics on that data. However my connection would never work!!!

This was my code


private Connection connection = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;

try
{
res = new Vector();

Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
connection = DriverManager.getConnection("jdbc:db2://"+host+":"+port+"/"+database,user,pass);

statement = connection.createStatement();

preparedStatement = connection.prepareStatement("SELECT * FROM "+table+" order by 1");

resultSet = preparedStatement.executeQuery();

...
}
catch (Exception e)
{
throw e;
}
finally
{
close();
}

This is based on the article I had read from IBM. Further to this article, I had managed to locate db2java.jar from the client installation. However, everytime I executed the code I was getting the error:

Specified connection failed with java.sql.SQLException: No suitable driver found

Searching for this, most articles suggested the connection string was wrong. I knew it wasn’t because I checked and double checked.

Then I read a comment in a forum which said to use jcc.DB2Driver and not app.DB2Driver. I looked in the library db2java.jar and I could not find DB2Driver under jcc! However, I then realised within the driver directory of my DB2 client that there were more Java libraries, and one of these was db2jcc.jar.

I swapped over my libraries and updated my code (note the change to Class.forName):


   private Connection connection = null;
   private Statement statement = null;
   private PreparedStatement preparedStatement = null;
   private ResultSet resultSet = null;

   try
   {
      res = new Vector();

      Class.forName("com.ibm.db2.jcc.DB2Driver");
      connection = DriverManager.getConnection("jdbc:db2://"+host+":"+port+"/"+database,user,pass);

      statement = connection.createStatement();

      preparedStatement = connection.prepareStatement("SELECT * FROM "+table+" order by 1");

      resultSet = preparedStatement.executeQuery();

      ...
   }
   catch (Exception e)
   {
      throw e;
   }
   finally
   {
      close();
   }

This worked perfectly. Just wanted to share it!

Advertisements

Tags: , , , , ,

One Response to “Java Application connecting to a DB2 instance – Solution to Sql Exception No Suitable driver found”

  1. xvcc Says:

    great..thanks a lot u legend

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: