0

I have a JAVA Class that's trying to connect to a MS SQL database. Now, when I am trying to connect to the database I got an error "No suitable driver found for....."

Why is this happening?

Here is the code.

DB db = bew DB();
db.dbConnect();

...

class DB
{
    public void dbConnect() {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String connectionUrl = "jdbc:sqlserver://web.address.com;" +
               "databaseName=testdb;user=user.name;password=*****;";

            Connection con = DriverManager.getConnection(connectionUrl);
        }
        catch (Exception e)
        {
            e.printStackTrace();                
        }
    }
}

I used sqljdbc.jar as my driver and I added it on my classpath.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
RJ.
  • 77
  • 3
  • 10

1 Answers1

4

SQLException: No suitable driver found

This exception means that the JDBC URL which is been used to get the connection isn't supported by any of the JDBC drivers loaded as far. So, this exception can have at least three possible causes:

  • The right driver wasn't loaded at all.
  • Loading the driver failed with an exception which was suppressed which in turn caused that the code continues to run and tries to get the connection instead of aborting.
  • The JDBC URL is plain wrong.

Let's look what you've as far:

  • You're using a Microsoft SQL Server database.
  • You've loaded the Sun JDBC-ODBC bridge driver.
  • You're using a JDBC URL which is specific to the SQL Server JDBC driver and it looks correct.

Ah right, you've loaded the wrong JDBC driver!

You need the Microsoft SQL Server JDBC driver, or at least the jTDS JDBC driver, which is actually a 3rd party driver which also supports SQL Server, but is been told to be much better than Microsoft's own JDBC driver. You can find in the documentation which driver class name you need to specify to load the driver. For the Microsoft one, it's the following:

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • I have actually downloaded my driver in the site you've provide "Microsoft SQL Server JDBC driver". I'm wondering why the driver name is different and not "com.microsoft.jdbc.sqlserver.SQLServerDriver". I'm very confuse now.. :( – RJ. Sep 06 '11 at 05:48
  • Perhaps you were copypasting the wrong code snippets without reading documentation? The JDBC-ODBC bridge driver only works for MS Access or any other DB which can create a DSN. – BalusC Sep 06 '11 at 12:34