SQLUser
SQLUser

Reputation: 97

Connecting to remote SQL Server using SQL Driver in Java

Key notes:

I am trying to connect to a remote SQL Server in Java for quite some time now and still have not been able to. I am hoping to connect using my DSN that I have created. The new driver JDK 1.8 uses needs a connection URL which I have created below. Everything in the code below looks correct. But I am still getting the error:

SEVERE: Java Runtime Environment (JRE) version 1.8 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0. java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.8 is not supported by this driver.
Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.

Here is my code:

String connectionUrl = "jdbc:sqlserver://InterfaceDSN" +  
    "databaseName=DB_Local;Trusted_Connection=True;integratedSecurity=true";  

    // Declare the JDBC objects.  
    Connection con = null;  
    Statement stmt = null;  
    ResultSet rs = null;  

    try 
    {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
        con = DriverManager.getConnection(connectionUrl);  

        String SQL = "SELECT TOP 10 * FROM Person.Contact";  
        stmt = con.createStatement();  
        rs = stmt.executeQuery(SQL);

        while (rs.next()) {  
          System.out.println(rs.getString(4) + " " + rs.getString(6));  
        }  
    }catch (Exception e) 
    {  
        e.printStackTrace();  
    }

To conclude: Knowing what I have and what I need, is there another way to connect to the database, I am running out of options here considering I am using JDK 1.8 with SQL Server 2005.

Upvotes: 0

Views: 2072

Answers (2)

jrtapsell
jrtapsell

Reputation: 7021

What is happening

As the message says:

java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.8 is not supported by this driver.
Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.

This is because JDBC4.0 has a lot of changes, meaning you need to use a newer version of the driver. You cannot run the old driver on the new version.

How to fix it

Using just 6.2 or version 6.2.2.jre8 should fix the issue.

Extra info

You shouldn't need the below code after JDBC 4.0 (Java 1.6+)

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  

Upvotes: 1

Anastasios Vlasopoulos
Anastasios Vlasopoulos

Reputation: 1802

Starting with the Microsoft JDBC Driver 4.2 for SQL Server, Sun Java SE Development Kit (JDK) 8.0 and Java

You can read it here where you can find the requirements for the JDBC Driver.

So, you have to update your JDBC driver to v 4.2 at least. This is clear enough.

Upvotes: 0

Related Questions