Wednesday, December 30, 2009

java.sql.SQLException: No suitable driver

Since I've been re-building my pc since a blue screen of death last week, I got to spend a frustrating morning trying to figure out why i was getting the following exception in my Java app when making a connection to a 2005 SQL Server using the Microsoft jdbc drive SQLJDBC4 sqljsbc4.jar


java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at com.medrisk.collections.server.DataServiceImpl.MRConn(DataServiceImpl.java:61)
at com.medrisk.collections.server.DataServiceImpl.getCaseWorkerList(DataServiceImpl.java:149)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)

A lot of postings out there on news groups all reprimand the poor developer who hits this message that either is class path is wrong, the jar is not in the path, or the connection URL is wrong. Developers howl that this is not the case... any they may be right! The matter-of-fact responses out there insist that this error only comes from the class path or url, but a hint on the Microsoft doc hints at another problem - your version of java.

First Step for you is to read that doc!

Here is what i had to do to fix this problem on my pc running eclipse 3.5

Open a command prompt and type

 java -version.

This will be your first clue, but also try typing:

C:\windows\system32\java -version

Note if the responses are different. We're trying to get version 6 of java installed and everything pointing to it.

Here was my response before i fixed it


C:\Documents and Settings\bsautner>java -version
java version "1.5.0_15"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
Java HotSpot(TM) Client VM (build 1.5.0_15-b04, mixed mode)


C:\Documents and Settings\bsautner>C:\windows\system32\java -version
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)



turns out there was a java install on my pc when it was given back to me after the crash.

According to the Microsoft doc, you need to Java Version 6 or higher to use sqljdbc4.jar.

Download the latest version of the Java JDK to your pc and install it. As of this posting, here is where i got it:

http://java.sun.com/javase/downloads/index.jsp

Try running the java command above but from the install directory like this:

C:\Program Files\Java\jre6\bin\java -version

note if you got a higher version than before. You may now have the right version to use sqljdbc4.jar

Next, open eclipse and tell it to use that version of java by selecting the Window Menu - then Preferences. Expand Java And click on Installed JREs. Click Add and browse to C:\Program Files\Java\jre6.  Click finish and make sure jre6 is checked.

I'm hopeing that since you found this blog, you already have the jar files and connection classes, and i hope this solved your problem.

More samples and resources on using sqljdbc are available here

http://msdn.microsoft.com/en-us/library/aa342339.aspx

3 comments:

  1. I am getting the following error.
    05/24/2010 03:28:41 java.sql.SQLException: No suitable driver
    05/24/2010 03:28:41 java.sql.DriverManager.getConnection threw an exception
    05/24/2010 03:28:44 java.sql.SQLException: No suitable driver
    05/24/2010 03:28:44 java.sql.DriverManager.getConnection threw an exception
    05/24/2010 03:28:44 java.lang.NoClassDefFoundError: com/provider/jdbc/sqlserver/SQLServerDriver
    05/24/2010 03:28:44 CJavaClassHandle: Not initialized
    05/24/2010 03:28:44 User-specified driver class failed to load

    I am getting this on Solaris machine.

    Can you please help to resolve this.

    ReplyDelete
  2. pummy, did the solution i posted above not work for you?

    ReplyDelete
  3. Latest reviews and news about android phones, free droid app and android mobile

    android mobile

    ReplyDelete