aspose file tools*
The moose likes JDBC and the fly likes JDBC connecting to MS SQL Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC connecting to MS SQL Server" Watch "JDBC connecting to MS SQL Server" New topic
Author

JDBC connecting to MS SQL Server

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2503
    
    8

Hello all

I have developed a JDBC program testing connections to different databases namely MySQL, Oracle, DB2 and SQL Server. All the database JDBC connections works fine except SQL Server. The key issue is with the JDBC jar file for SQL Server.

The whole idea I was doing was to allow my program to run with Java 5 and Java 6 without changing a bit of code. From my understanding SQL Server JDBC uses "sqljdbc.jar" for Java 5 and "sqljdbc4.jar" for Java 6 (JDBC 4 supported). So I use a ClassLoader to load the correct jar based on the Java runtime version. When I run the program, I loaded the correct jar but couldn't connect. I have tried the SQL Server JDBC driver versions 3.0 and 4.0 without success. I'm using SQL Server 2008.

The error I'm getting is UnsupportedClassVersionError. I have installed Java 5,6,7 runtime on my machine and used the following commands to run in different runtime:
Java 5 = "java -version:1.5 -jar myProgram.jar"
Java 6 = "java -version:1.6 -jar myProgram.jar"
Java 7 = "java -jar myProgram.jar"


When I just include only one of the JDBC drivers and test program in the respective Java runtime, it works perfectly fine. Is this a JDBC driver compatibility issue or coding issue?



Any light why is the case?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

One problem I can think of is you have 3 versions of every public class in your environment at once, will that cause problems? (This is an honest question as I have no idea of the answer)
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2503
    
    8

Wendy Gibbons wrote:One problem I can think of is you have 3 versions of every public class in your environment at once, will that cause problems? (This is an honest question as I have no idea of the answer)


Thanks for your feedback Wendy. I doubt having multiple Java platforms is an issue. However, since I'm connecting to SQL Server 2008, I'm not sure if the database engine restricts the jdbc jar file.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

K. Tsang wrote: However, since I'm connecting to SQL Server 2008, I'm not sure if the database engine restricts the jdbc jar file.

The UnsupportedClassVersionError exception indicates that a class could not be loaded by a class loader. I strongly doubt that the database you're connection to would have some direct say over which class file to load.

I think Wendy is right. If you have several versions of a class file on your classpath, the first one is used. If the first one is of a newer version than the JRE you're running, you'll get this error. I don't think class loader will skip class files that have unsupported versions, if there is a supported version further in the path.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2503
    
    8

Thanks Martin for your input.

After thinking about, I have kept things simple by separating the jdbc jar files into individual projects (ie only for java 5 or 6 and not both). This way only one jdbc jar is loaded.

When say using java5 to run the java6 project, it throws the UnsupportClassVersionError as expected.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC connecting to MS SQL Server