错误:与主机的 TCP/IP 连接失败。java.net.ConnectException:连接被拒绝:连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12496045/
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 09:09:04  来源:igfitidea点击:

Error:The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect

javasql-server-2005database-connectiontcp-ip

提问by bsm

I am using

我在用

  • Windows 7
  • Netbeans IDE 7.1.2
  • SQL Server Management Studio Express 2005
  • JDK1.6
  • Windows 7的
  • Netbeans IDE 7.1.2
  • SQL Server Management Studio Express 2005
  • JDK1.6

I am getting the below error while connecting to the database:

连接数据库时出现以下错误:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect

com.microsoft.sqlserver.jdbc.SQLServerException:与主机的 TCP/IP 连接失败。java.net.ConnectException:连接被拒绝:连接

My connection string is:

我的连接字符串是:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
Connection connection = DriverManager.getConnection(  
    "jdbc:sqlserver://127.0.0.1:1433;databaseName=dbcm;" +  
    "user=sa;password=sa");

I've checked below things:

我检查了以下内容:

  1. In SQL Server Configuration Manager, Protocols for SQLEXPRESS : TCP/IP Enabled In fact I made enable remaining 3 too.
  2. SQL Server(SQLExpress) and SQL Browser Service both are running.
  1. 在 SQL Server 配置管理器中,SQLEXPRESS 的协议:启用 TCP/IP 实际上我也启用了剩余的 3 个。
  2. SQL Server(SQLExpress) 和 SQL Browser 服务都在运行。

回答by Vikdor

I followed the steps below to successfully connect to an SQLExpress instance using JDBC:

我按照以下步骤使用 JDBC 成功连接到 SQLExpress 实例:

  1. Configure TCP/IP communication with SQL Express
    1. Open SQL Server Configuration Manager.
    2. Go to SQL Server Network Configuration -> Protocols for SQLEXPRESS
    3. Set the status of TCP/IP protocol to "Enabled" (if it is already not).
    4. Open Properties window for TCP/IP, go to IP Addresses section.
    5. Go to the bottom of this property page and set the TCP Port under IPAllto 1433.
  2. Connect to the SQLExpress instance using Microsoft's JDBC driver for SQL Server
    1. JDBC URL: jdbc:sqlserver://localhost;instance=SQLEXPRESS;databaseName=<your DB>;user=<your User>;password=<your Passwd>
  1. 使用 SQL Express 配置 TCP/IP 通信
    1. 打开 SQL Server 配置管理器。
    2. 转到 SQL Server 网络配置 -> SQLEXPRESS 的协议
    3. 将 TCP/IP 协议的状态设置为“已启用”(如果尚未启用)。
    4. 打开 TCP/IP 的属性窗口,转到 IP 地址部分。
    5. 转到此属性页的底部并将 TCP 端口设置IPAll为 1433。
  2. 使用连接到 SQLExpress 实例 Microsoft's JDBC driver for SQL Server
    1. JDBC 网址: jdbc:sqlserver://localhost;instance=SQLEXPRESS;databaseName=<your DB>;user=<your User>;password=<your Passwd>