Results 1 to 7 of 7
  1. #1
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Question JDK 7 connecting to Remote MS SQL 2005 Server

    Hello,

    I'm attempting to connect to a Remote (outside of my network) Microsoft SQL Server 2005 using JDK 7 SE. I have downloaded the Microsoft JDBC 4.0 driver and added it to my classpath.

    When I execute my connection and test query -- I get a "SEVERE" error that states JRE 1.7 is not supported by this driver and to use the sqljdbc4.jar class library which does support JDBC 4.0 instead. Problem is, I thought I was doing this already...

    Can someone point me in the right direction?

    My test code has the connection built in two different ways, because my first try wasn't working as expected... I'm open to whatever works... thanks!

    Here's my test code:

    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import com.microsoft.sqlserver.jdbc.*;
    
    public class ConnectMSSQL {
    	static Connection con;
    	static Statement statement;
    	static ResultSet result;
    	
    	// DEBUG ONLY //
    	public static void main (String[] args) {
    //		openConnection("169.0.0.0", "9999", "DB", "user", "pass");
    //		String response = sendStatement("SELECT * FROM table WHERE num='7205560'");
    //		System.out.println("Response: " + response);
    		testConnection("169.0.0.0", 9999, "DB", "user", "pass");
    		testStatement("SELECT * FROM table WHERE num='7205560'");
    		closeConnection();
    	}
    	// DEBUG ONLY //
    	public static void testConnection(String serverIP, int port, String databaseName, String user, String password) {
    		con = null;
    		try {
    			SQLServerDataSource ds = new SQLServerDataSource();
    			ds.setUser(user);
    			ds.setPassword(password);
    			ds.setServerName(serverIP);
    			ds.setPortNumber(port);
    			ds.setDatabaseName(databaseName);
    			con = ds.getConnection();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	public static void testStatement(String sql) {
    		try {
    			statement = con.createStatement();
    			result = statement.executeQuery(sql);
    			while (result.next()) {
    				System.out.println("Result:" + result.getString(0));
    			}
    		} catch (SQLException ex) {
    			ex.printStackTrace();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public static void openConnection(String serverIP, String port, String databaseName, String user, String password) {
    		try {
    			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    			String connectionUrl = "jdbc:sqlserver://" + serverIP + ":" + port + ";" +
    					"databaseName=" + databaseName + ";" + "user=" + user + ";" + 
    					"password=" + password + ";";
    			con = DriverManager.getConnection(connectionUrl);
    		} catch (Exception e) {
    			System.out.println("");
    			System.out.println(" ~~ SQL CONNECTION ERROR ~~ ");
    			System.out.println("");
    			e.printStackTrace();
    		}
    	}
    	public static void closeConnection() {
    		try {
    			con.close();
    		} catch (SQLException e) {
    			System.out.println("");
    			System.out.println(" ~~ CLOSE SQL CONNECTION ERROR ~~ ");
    			System.out.println("");
    			e.printStackTrace();
    		}
    	}
    	public static String sendStatement(String sql) {
    		String response = "";
    		try {
    			statement = con.createStatement();
    			statement.execute(sql);
    			result = statement.getResultSet();
    			if (result.next()) {
    				response = result.getString(0);
    			}
    		} catch (SQLException e) {
    			System.out.println("");
    			System.out.println(" ~~ SQL STATEMENT ERROR ~~ ");
    			System.out.println("");
    			e.printStackTrace();
    		}
    		return response;
    	}
    }
    EDIT: Forgot to add the exception!

    Java Code:
    Aug 29, 2012 11:10:27 AM com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
    SEVERE: Java Runtime Environment (JRE) version 1.7 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.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
    	at com.microsoft.sqlserver.jdbc.SQLServerConnection.<init>(SQLServerConnection.java:304)
    	at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(SQLServerDataSource.java:620)
    	at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnection(SQLServerDataSource.java:57)
    	at jdbGetOrders.ConnectMSSQL.testConnection(ConnectMSSQL.java:34)
    	at jdbGetOrders.ConnectMSSQL.main(ConnectMSSQL.java:20)
    I don't really think the problem is with using Java 7... that seems to be a ridiculous problem if so... Java 7 as far as I know didn't remove anything, just added stuff... right? -- Also, the rest of my rather large (by my standards) project is written using Java 7, so backtracking to Java 6 isn't really an option... please help!
    Last edited by SnakeDoc; 08-29-2012 at 08:26 PM.

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: JDK 7 connecting to Remote MS SQL 2005 Server

    I think you are right, it is not the fault of Java 7. Have you tried the sqljdbc4.jar as advised in the error?
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: JDK 7 connecting to Remote MS SQL 2005 Server

    I thought I already was by importing that into my classpath and explicitly calling the

    Java Code:
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    before I added the jar files downloaded from Microsoft into my classpath, I got an error when I tried to call that driver... now I dont, however I get the exception listed above...

    So i thought/think I already am using the sdljdbc4.jar ... ???

    EDIT: I'm seeing in a few places that Microsoft wants you to use JDK EE ... is that really necessary? Wouldn't I be getting a different Exception thrown if that were the case? Also, if it is necessary, how can I get EE safely without effecting all of the other projects I currently am working on (including the rest of this current project) which had all been written using SE of java7.
    Last edited by SnakeDoc; 08-29-2012 at 08:53 PM.

  4. #4
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: JDK 7 connecting to Remote MS SQL 2005 Server

    I just took a look at the downloaded/unzipped Driver file i got from Microsoft and they include two source files as example ways to make a connection... and surprisingly both look pretty much identical to the two ways I have in my original code posted above...

    Here's Microsoft's example Code:

    Example Connection 1:

    Java Code:
    //=====================================================================
    //
    //  File:    connectURL.java      
    //  Summary: This Microsoft JDBC Driver for SQL Server sample application
    //	     demonstrates how to connect to a SQL Server database by using
    //	     a connection URL. It also demonstrates how to retrieve data 
    //	     from a SQL Server database by using an SQL statement.
    //
    //---------------------------------------------------------------------
    //
    //  This file is part of the Microsoft JDBC Driver for SQL Server Code Samples.
    //  Copyright (C) Microsoft Corporation.  All rights reserved.
    //
    //  This source code is intended only as a supplement to Microsoft
    //  Development Tools and/or on-line documentation.  See these other
    //  materials for detailed information regarding Microsoft code samples.
    //
    //  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
    //  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
    //  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    //  PARTICULAR PURPOSE.
    //
    //===================================================================== 
    
    import java.sql.*;
    
    public class connectURL {
    
    	public static void main(String[] args) {
    		
    		// Create a variable for the connection string.
    		String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
    			"databaseName=AdventureWorks;integratedSecurity=true;";
    
    		// Declare the JDBC objects.
    		Connection con = null;
    		Statement stmt = null;
    		ResultSet rs = null;
    		
            	try {
            		// Establish the connection.
            		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                		con = DriverManager.getConnection(connectionUrl);
                
                		// Create and execute an SQL statement that returns some data.
                		String SQL = "SELECT TOP 10 * FROM Person.Contact";
                		stmt = con.createStatement();
                		rs = stmt.executeQuery(SQL);
                
                		// Iterate through the data in the result set and display it.
                		while (rs.next()) {
                			System.out.println(rs.getString(4) + " " + rs.getString(6));
                		}
            	}
            
    		// Handle any errors that may have occurred.
    		catch (Exception e) {
    			e.printStackTrace();
    		}
    
    		finally {
    			if (rs != null) try { rs.close(); } catch(Exception e) {}
    	    		if (stmt != null) try { stmt.close(); } catch(Exception e) {}
    	    		if (con != null) try { con.close(); } catch(Exception e) {}
    		}
    	}
    }
    Example Connection 2:

    Java Code:
    //=====================================================================
    //
    //  File:    connectDS.java      
    //  Summary: This Microsoft JDBC Driver for SQL Server sample application
    //	     demonstrates how to connect to a SQL Server database by 
    //	     using a data source object. It also demonstrates how to 
    //	     retrieve data from a SQL Server database by using a stored 
    //	     procedure.
    //
    //---------------------------------------------------------------------
    //
    //  This file is part of the Microsoft JDBC Driver for SQL Server Code Samples.
    //  Copyright (C) Microsoft Corporation.  All rights reserved.
    //
    //  This source code is intended only as a supplement to Microsoft
    //  Development Tools and/or on-line documentation.  See these other
    //  materials for detailed information regarding Microsoft code samples.
    //
    //  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
    //  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
    //  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    //  PARTICULAR PURPOSE.
    //
    //===================================================================== 
    
    import java.sql.*;
    import com.microsoft.sqlserver.jdbc.*;
    
    public class connectDS {
    
    	public static void main(String[] args) {
    		
    		// Declare the JDBC objects.
    		Connection con = null;
    		CallableStatement cstmt = null;
    		ResultSet rs = null;
    		
    		try {
    			// Establish the connection. 
    			SQLServerDataSource ds = new SQLServerDataSource();
    			ds.setIntegratedSecurity(true);
    			ds.setServerName("localhost");
    			ds.setPortNumber(1433); 
    			ds.setDatabaseName("AdventureWorks");
    			con = ds.getConnection();
    			 
    	        	// Execute a stored procedure that returns some data.
                		cstmt = con.prepareCall("{call dbo.uspGetEmployeeManagers(?)}");
                		cstmt.setInt(1, 50);
                		rs = cstmt.executeQuery();
    
    	        	// Iterate through the data in the result set and display it.
    	        	while (rs.next()) {
    	            		System.out.println("EMPLOYEE: " + rs.getString("LastName") + 
    	            			", " + rs.getString("FirstName"));
    	            		System.out.println("MANAGER: " + rs.getString("ManagerLastName") + 
    	            			", " + rs.getString("ManagerFirstName"));
    	            		System.out.println();
    	        	}
    	        }
    	        
    		// Handle any errors that may have occurred.
    	    	catch (Exception e) {
    	    		e.printStackTrace();
    	    	}
    
    	   	finally {
    	    		if (rs != null) try { rs.close(); } catch(Exception e) {}
    	    		if (cstmt != null) try { cstmt.close(); } catch(Exception e) {}
    	    		if (con != null) try { con.close(); } catch(Exception e) {}
    	    	}
    	}
    }

  5. #5
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: JDK 7 connecting to Remote MS SQL 2005 Server

    SOLVED!

    I think at least! lol!

    I was including both the sqljdbc.jar AND sqljdbc4.jar that came in MS's zip file in my classpath! The sqljdbc.jar was overriding the sqljdbc4.jar stuff, throwing the error!!! Removing the non-4 version seems to have fixed this...

    Thanks for your help!

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,932
    Rep Power
    19

    Default Re: JDK 7 connecting to Remote MS SQL 2005 Server

    I was going to say, have you accidentally stuck both jar files onto the classpath.
    The class loader generally works in alphabetical order so will take the older (non-4) jar first.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: JDK 7 connecting to Remote MS SQL 2005 Server

    Thanks Tolls,

    This was exactly what was happening! This was my first foray with direct database access as apposed to using odbc... and the README that came with the zip driver download just said "copy the jar to your classpath", etc... so I wrongly assumed it was both jars that were needed (like some inheritance thing or something). Wrong!

    Thanks again!

Similar Threads

  1. Replies: 0
    Last Post: 09-20-2011, 04:42 PM
  2. connecting to remote SQL server
    By mahasarathi in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-04-2009, 06:11 AM
  3. Replies: 0
    Last Post: 12-16-2008, 08:32 AM
  4. Replies: 4
    Last Post: 05-21-2008, 06:55 AM
  5. Java connecting to sql server 2005
    By pelegk2 in forum JDBC
    Replies: 0
    Last Post: 04-05-2008, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •