Reputation: 28139
I'm trying to connect to a SQL Server db on my local machine using the following code:
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
import java.sql.*;
public class JDBConn {
//public class connectURL {
public static void main(String[] args) {
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
// Create a variable for the connection string.
//String connectionUrl = "jdbc:sqlserver://localhost;database=optRes1;integratedSecurity=true;";
//con = DriverManager.getConnection(connectionUrl);
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost";
con = DriverManager.getConnection(connectionUrl);
Statement s = con.createStatement();
ResultSet r = s.executeQuery("SELECT * FROM some_table_name");
while (r.next()) {
System.out.println(r.getString(1));
}
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT * from [optRes1].[dbo].[unEmpDat]";
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) {}
}
}
}
I'm getting the following message when I try to connect (I'm using Eclipse as an IDE):
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
So far, I've gone into the Configuration Mgr and activated and enabled TCP/IP.
I've also gone in and set each IP address to on and active under the Properties > IP Addresses tab.
A couple of potential issues.
When I look at the status of the SQL Server Browser it is 'Stopped'. I'm not sure if this is an issue and if so how to fix it.
I'm using the sqljdbc4.jar
file and have it in the Referenced Libraries section of the project.
Let me know if you have any suggestions please.
EDIT:
I'm using a Windows 7 box.
UPDATE:
>java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)
Upvotes: 0
Views: 2609
Reputation: 6726
You didn't include which JRE version you are using, but there is a known issue between 1.6.0_29 and SQL Server 2008 documented in Bug ID 7105007 There's also a thread in the Microsoft forums too which suggests downgrading to 1.6.0_27.
You can also just replace _29's jsse.jar
with the one from _27. This is mostly useful for Mac OS clients where it's difficult to downgrade. Target file is Mac HD -> System -> Library -> Java -> JavaVirtualMachines -> 1.6.0.jdk -> Contents -> Classes -> jsse.jar
Upvotes: 1