java - 与主机 localhost 端口 1433 的 TCP/IP 连接失败。错误: "Connection refused: connect

标签 java sql sql-server

我想将Java类文件与SQL Server 2008连接。我已经使用SQL Server身份验证登录:sa,密码: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)

最佳答案

回答这个问题可能有点晚了,但这样做是为了我自己的利益。

通过 JDBC 连接的实际 TCP 端口可以在注册表中找到 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<Instance Name>\MSSQLServer\SuperSocketNetLib\Tcp

使用此值连接到 SQL Server。

例如 "jdbc:sqlserver://localhost\SQLEXPRESS:49922;databaseName=db;user=user;password=secret"

还要确保在服务器配置工具中启用 TCP 连接。

关于java - 与主机 localhost 端口 1433 的 TCP/IP 连接失败。错误: "Connection refused: connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26512721/

相关文章:

java - Android M downloadprovider 下载失败 SecurityException

java - 通过 ActionBarActivity 支持 v7 操作栏

访问最新记录时 SQL 性能缓慢

sql-server - 关于SQL Server Express和localdb的困惑

java - Servlet请求getParameter的性能

java - 如何从另一个类访问数组

mysql - 从性能的角度来看,以下 SQL 是好的还是坏的做法?

php - 如何在之前的 MySQL 操作中获取受影响的行?

SQL 将字符串(日期或文本)转换为日期

sql-server - SQL Server 触发器