Java 到主机 localhost 端口 1433 的 TCP/IP 连接失败。错误:“连接被拒绝:连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26512721/
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
The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect
提问by Teppei14
I want to connect Java class file with SQL server 2008. I have logged in with SQL server authentication use: sa, pass:123456. But i am receiving error in connectivity.
我想将 Java 类文件与 SQL Server 2008 连接。我已使用 SQL Server 身份验证登录:sa,pass:123456。但我收到连接错误。
static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String dburl = "jdbc:sqlserver://localhost\SQL2008:1433;Database=Java";
static String user = "sa";
static String password = "123456";
public static void update() throws Exception{
String sql = "UPDATE Categories SET Id='COM' WHERE Id='LAP'";
Class.forName(driver);
Connection conn = DriverManager.getConnection(dburl, user, password);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
conn.close();
}
public static void main(String[] args) throws Exception {
Basic.update();
}
Exception in thread "main" 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.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1048)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:829)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:712)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Tutorials.jdbc.Basic.update(Basic.java:48)
at Tutorials.jdbc.Basic.main(Basic.java:72)
回答by Lokesh
Make sure you have Driver jar file and change your dburl to this as mentioned below
确保您有驱动程序 jar 文件并将您的 dburl 更改为如下所述
try this
尝试这个
String dburl ="jdbc:sqlserver://SQL2008:1433;DatabaseName=Java;user=sa;Password=123456";
回答by Aaron C
I think that your dburl may be malformed. Try this url instead.
我认为您的 dburl 可能格式不正确。试试这个网址。
String dburl= "jdbc:sqlserver://localhost\SQL2008:1433;" +
"databaseName=Java;user=sa;password=123456";
回答by Duston
TCP/IP Connection Refused.
TCP/IP 连接被拒绝。
Run
NETSTAT -A
to see a list of ports that are open (they'll sayLISTENING
) If 1433 isn't among them, then SQL server isn't running or isn't listening on that port. Follow Michael Todd's advice from there.If 1433 is on the list, check your local firewall to see if it allows connections to/from localhost and/or allows connections to 1433.
You can try
TELNET 1433
and see if it connects (you should get a blank screen with a cursor blinking at the top left.) (You may have to enable the telnet client in Programs and Features.) If so then you have a problem with your code.
运行
NETSTAT -A
以查看打开的端口列表(他们会说LISTENING
)如果 1433 不在其中,则 SQL Server 未运行或未侦听该端口。从那里遵循迈克尔托德的建议。如果 1433 在列表中,请检查您的本地防火墙以查看它是否允许连接到/来自本地主机和/或允许连接到 1433。
您可以尝试
TELNET 1433
查看它是否连接(您应该看到一个空白屏幕,左上角的光标闪烁。)(您可能必须在“程序和功能”中启用 telnet 客户端。)如果是,那么您的代码有问题.
回答by Teppei14
Thanks you for your help, i'm fix my problem, it's not error on String dburl= "jdbc:sqlserver://localhost\SQL2008:1433;Database:Java"
感谢您的帮助,我正在解决我的问题,这不是 String dburl="jdbc:sqlserver://localhost\SQL2008:1433;Database:Java" 上的错误
it's error port 1433 no connect in SQL server 2008 Where i find the Protocol TCP/IP, if disabled then Enable it Click on TCP/IP, i'm find its properties.
它的错误端口 1433 在 SQL Server 2008 中没有连接 在那里我找到了 TCP/IP 协议,如果禁用,则启用它 单击 TCP/IP,我找到了它的属性。
In this properties Remove All the TCP Dynamic Ports and Add value of 1433 to all TCP Port and restart your SQL Server Services > SQL Server
在此属性中删除所有 TCP 动态端口并将值 1433 添加到所有 TCP 端口并重新启动 SQL Server 服务 > SQL Server
And Its Done...
它完成了......
Thanks all for your help!
感谢你的帮助!
回答by Yogesh Sajanikar
Probably a little late to answer this question, but doing it for my own benefit.
回答这个问题可能有点晚了,但这样做是为了我自己的利益。
The actual TCP port to connect to through JDBC can be found in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<Instance Name>\MSSQLServer\SuperSocketNetLib\Tcp
通过 JDBC 连接的实际 TCP 端口可以在注册表中找到 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<Instance Name>\MSSQLServer\SuperSocketNetLib\Tcp
Use this value to connect to SQL Server.
使用此值连接到 SQL Server。
For example
"jdbc:sqlserver://localhost\SQLEXPRESS:49922;databaseName=db;user=user;password=secret"
例如
"jdbc:sqlserver://localhost\SQLEXPRESS:49922;databaseName=db;user=user;password=secret"
Also ensure that TCP connections are enabled in the server configuration tools.
还要确保在服务器配置工具中启用了 TCP 连接。