0

我想连接到 sql server express。我下载了这个驱动。我阅读了帮助文件,这是我的代码:

        try 
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://localhost;database=ActorsDb;integratedSecurity=true;";
            Connection con = DriverManager.getConnection(connectionUrl);
            PreparedStatement st = con.prepareStatement("INSERT INTO Actors(FirstName,LastName,Age) VALUES(?,?,?)");
            st.setString(1, "Robert");
            st.setString(2, "de Niro");
            st.setInt(3,45);
            st.executeUpdate();     
            con.close();
        } 
        catch (SQLException | ClassNotFoundException e) 
        {
            e.printStackTrace();
        }   

我得到这个例外: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. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

我关闭了防火墙,但没有任何改变。

  1. 我去了 SQL Server 配置管理器并启用了 TCP/IP

  2. 我去了IP地址,IP1并设置属性

主动:是;启用:是;TCP 动态端口:[空] ; TCP 端口:1433

关于我所缺少的任何提示?谢谢。

4

3 回答 3

0

检查 SQL Server 服务是否正在运行,并检查是否有任何程序(如防病毒)阻止连接。如果是这样,请禁用它们或向该连接添加例外。

于 2013-02-15T18:07:44.220 回答
0

您在 JDBC URL 中使用 localhost,请尝试使用您在 SSCM 中描述的“IP1”的 IP 地址。或者,验证 SSCM 是否配置为侦听 localhost:1433。

于 2013-02-15T18:08:56.483 回答
0

问题解决了。我不得不将 sqljdbc_auth.dll 复制到工作目录中。

于 2013-02-15T22:49:13.427 回答