This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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:
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.
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.