• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC connecting to MS SQL Server

 
K. Tsang
Bartender
Posts: 3404
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3404
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3404
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic