java 如何修复它:com.microsoft.sqlserver.jdbc.SQLServerException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14900675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:46:48  来源:igfitidea点击:

How to fix it: com.microsoft.sqlserver.jdbc.SQLServerException?

javasql-server

提问by Hyman Willson

I want to connect to sql server express. I downloaded thisdriver. I read the help file and this is my code:

我想连接到 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();
        }   

I get this exception: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.".

我得到这个例外: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.".

I turned off firewall but nothing changed.

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

  1. I went to SQL Server Configuration Manager and i enabled TCP/IP

  2. I went to IP Adresses, IP1 and set the properties

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

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

Active:Yes ; Enabled:Yes ; TCP Dynamic Ports:[empty] ; TCP Port:1433

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

Any tips of what i am missing? Thanks.

我缺少什么的任何提示?谢谢。

采纳答案by Hyman Willson

Problem solved. I had to copy sqljdbc_auth.dll in the working directory.

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

回答by Amith Koujalgi

Check if the SQL Server service is running and check if any programs like anti-virus is blocking the connections. If so, disable them or add exception to that connection.

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

回答by ty733420

You're using localhost in your JDBC URL, try instead using the IP address for "IP1" that you describe in SSCM. Or, verify SSCM is configured to listen on localhost:1433.

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