错误:与主机的 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
Error:The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
提问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:
我检查了以下内容:
- In SQL Server Configuration Manager, Protocols for SQLEXPRESS : TCP/IP Enabled In fact I made enable remaining 3 too.
- SQL Server(SQLExpress) and SQL Browser Service both are running.
- 在 SQL Server 配置管理器中,SQLEXPRESS 的协议:启用 TCP/IP 实际上我也启用了剩余的 3 个。
- SQL Server(SQLExpress) 和 SQL Browser 服务都在运行。
回答by Vikdor
I followed the steps below to successfully connect to an SQLExpress instance using JDBC:
我按照以下步骤使用 JDBC 成功连接到 SQLExpress 实例:
- Configure TCP/IP communication with SQL Express
- Open SQL Server Configuration Manager.
- Go to SQL Server Network Configuration -> Protocols for SQLEXPRESS
- Set the status of TCP/IP protocol to "Enabled" (if it is already not).
- Open Properties window for TCP/IP, go to IP Addresses section.
- Go to the bottom of this property page and set the TCP Port under
IPAll
to 1433.
- Connect to the SQLExpress instance using
Microsoft's JDBC driver for SQL Server
- JDBC URL:
jdbc:sqlserver://localhost;instance=SQLEXPRESS;databaseName=<your DB>;user=<your User>;password=<your Passwd>
- JDBC URL:
- 使用 SQL Express 配置 TCP/IP 通信
- 打开 SQL Server 配置管理器。
- 转到 SQL Server 网络配置 -> SQLEXPRESS 的协议
- 将 TCP/IP 协议的状态设置为“已启用”(如果尚未启用)。
- 打开 TCP/IP 的属性窗口,转到 IP 地址部分。
- 转到此属性页的底部并将 TCP 端口设置
IPAll
为 1433。
- 使用连接到 SQLExpress 实例
Microsoft's JDBC driver for SQL Server
- JDBC 网址:
jdbc:sqlserver://localhost;instance=SQLEXPRESS;databaseName=<your DB>;user=<your User>;password=<your Passwd>
- JDBC 网址: