Java/SQL Server 2008 R2 Express 连接问题

标签 java sql-server sql-server-2008 jdbc

我正在尝试使用以下代码连接到本地计算机上的 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) {}
            }
        }
}

当我尝试连接时收到以下消息(我使用 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.".

到目前为止,我已进入配置管理器并激活并启用了 TCP/IP。

我还进入并在“属性”>“IP 地址”选项卡下将每个 IP 地址设置为打开并处于 Activity 状态。

一些潜在的问题。

当我查看 SQL Server 浏览器的状态时,它是“已停止”。我不确定这是否是一个问题,如果是,如何解决它。

我正在使用 sqljdbc4.jar 文件,并将其放在项目的“引用库”部分中。

如果您有任何建议,请告诉我。

编辑:

我使用的是 Windows 7 盒子。

更新:

>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)

最佳答案

您没有包含您正在使用的 JRE 版本,但 1.6.0_29 和 SQL Server 2008 之间存在一个已知问题,记录在 Bug ID 7105007 中。还有一个thread Microsoft 论坛中也建议降级到 1.6.0_27。

您也可以将 _29 的 jsse.jar 替换为 _27 中的。这对于难以降级的 Mac OS 客户端非常有用。目标文件为 Mac HD -> System -> Library -> Java -> JavaVirtualMachines -> 1.6.0.jdk -> Contents -> Classes -> jsse.jar

关于Java/SQL Server 2008 R2 Express 连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9012315/

相关文章:

java - JUnit 测试方法失败

java - 通过自定义注解进行代码注入(inject)

java - URL.openStream 生成错误 (javax.net.ssl.SSLException)

sql-server - 使用 select 的 SQL 插入可以返回多个身份吗?

java - 通用可比较合并排序算法的 Stackoverflow 错误

sql - 触发引发错误、阻止删除和审核尝试

sql-server - SQL Server 分页使用限制和偏移量以及非唯一排序列

SQL 内连接来自不同数据库的两个表并更新其中之一

SQL Server : SELECT from multiple lines if values exist

sql-server-2008 - 在 SQL Server Management Studio 中使用 setvar 转义空格字符