简体   繁体   English

Java / SQL Server 2008 R2 Express连接问题

[英]Java / SQL Server 2008 R2 Express connection issues

I'm trying to connect to a SQL Server db on my local machine using the following code: 我正在尝试使用以下代码连接到本地计算机上的SQL Server数据库:

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): 尝试连接时收到以下消息(我使用Eclipse作为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. 到目前为止,我已经进入配置管理器并激活并启用了TCP / IP。

I've also gone in and set each IP address to on and active under the Properties > IP Addresses tab. 我还进入了每个属性,并在“属性”>“ IP地址”选项卡下将其设置为活动状态。

A couple of potential issues. 一些潜在的问题。

When I look at the status of the SQL Server Browser it is 'Stopped'. 当我查看SQL Server浏览器的状态时,它处于“已停止”状态。 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. 我正在使用sqljdbc4.jar文件,并将其放在项目的“引用的库”部分中。

Let me know if you have any suggestions please. 如果您有任何建议,请告诉我。

EDIT: 编辑:

I'm using a Windows 7 box. 我正在使用Windows 7盒子。

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)

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. 您没有包括正在使用的JRE版本,但是在错误ID 7105007中记录了1.6.0_29和SQL Server 2008之间存在一个已知问题。Microsoft论坛中也有一个线程建议将其降级为1.6.0_27。

You can also just replace _29's jsse.jar with the one from _27. 您也可以将jsse.jarjsse.jar替换为_27的jsse.jar。 This is mostly useful for Mac OS clients where it's difficult to downgrade. 这对于难以降级的Mac OS客户端最有用。 Target file is Mac HD -> System -> Library -> Java -> JavaVirtualMachines -> 1.6.0.jdk -> Contents -> Classes -> jsse.jar 目标文件是Mac HD -> System -> Library -> Java -> JavaVirtualMachines -> 1.6.0.jdk -> Contents -> Classes -> jsse.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM