Wednesday, April 29, 2009

Java: Connecting to MS SQL server with JDBC and JRE 1.6

So,
I have this little Java project which requires that I connect to SQL Server 2005. Something I have done many times. Download the JDBC drivers from MS and copy them into the lib directory and do your shindig in the connection to get going.
But, this time things have changed and the installation instructions are worthless at best.

Here is what I did:
  1. Downloaded the driver files from MS for JDBC 2.0
  2. Unzipped to directory
  3. Moved jar files (there are two files sqljdbc.jar and sqljdbc4.jar) to my lib directory
  4. coded my connector to use Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
  5. Did some query code

And presto, nothing works as expected. Instead I get an exception with this in the stack trace:

"The JDBC Driver version 2.0 does not support JRE 1.4. You must upgrade JRE 1.4 to JRE 5.0 or later when using the JDBC Driver version 2.0. In some cases, you might need to recompile your application because it might not be compatible with JDK 5.0 or later. For more information, see the documentation on Sun Microsystems Web site"

Complete bogusness as neither Microsoft or Sun contain much info about how to solve this. So, head scratching starts followed by experimentation.

Come to find out I made a mistake by copying both jar files packaged in the driver to lib. They contain similar classes and the sqljdbc.jar files classes' are used if both are copied. However, the sqljdbc.jar file classes are not ment to be used with jre/jvm 1.6+ . Why noone mention this in the provided install doc is a different question.

Solution:

a) Remove the not needed jar file (sqljdbc.jar) from lib directory or

b) specifically declare to use only the right one (sqljdbc4.jar)

Microsoft, please help us measly developers wasting time and state this in basic terms in your install doc.

There you have it.

Cheers,

No comments: