Java 无法连接到数据库网络错误IOException:连接被拒绝:连接

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

Could not connect to the database Network error IOException: Connection refused: connect

javasql-server

提问by Allen

I am running a SQL Server Express 10.50.4000.0 on my machine. I have enabled on the TCP/IP and made sure the port is set to 1433. I have added an exception in my firewall for 1433. The server service is running. I have tried localhost and the computer name and both give the same error. When i try localhost\SQLEXPRESS or [USER-PC]\SQLEXPRESS I received an error saying it could not find the database.

我在我的机器上运行 SQL Server Express 10.50.4000.0。我已启用 TCP/IP 并确保端口设置为 1433。我在防火墙中为 1433 添加了一个例外。服务器服务正在运行。我试过 localhost 和计算机名称,都给出了同样的错误。当我尝试 localhost\SQLEXPRESS 或 [USER-PC]\SQLEXPRESS 时,我收到一条错误消息,说它找不到数据库。

String driverName = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driverName);

String serverName = "localhost";
String instanceName = "ALLEN-PC\SQLEXPRESS";
String serverPort = "1433";
String database  = serverName +":" + serverPort+"/"+instanceName;
String url = "jdbc:jtds:sqlserver://" + database;
String username = "sa";
String password = "password";

connection = DriverManager.getConnection(url, username, password);

I receive the error:

我收到错误:

Could not connect to the database Network error IOException: Connection refused: connect

with no additional context to investigate.

没有额外的背景可以调查。

采纳答案by Adam Haines

Your connection string needs to be in this format. Found here: http://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx

您的连接字符串需要采用这种格式。在这里找到:http: //msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx

jdbc:jtds:sqlserver://<yourDBServerIPAddress>\SQLEXPRESS:1433;databaseName=AdventureWorks;user=sa;password=*****;

回答by Kalaiarasan Manimaran

There is no database server listening on localhost:1433

没有数据库服务器监听 localhost:1433

Check that the SQL Server Browser Service is running

检查 SQL Server Browser 服务是否正在运行

So maybe using the ip address instead of localhost helps.

所以也许使用 ip 地址而不是 localhost 会有所帮助。

回答by vsriram92

  1. Make Sure that your SQL Server Browser service is active.
  2. Goto AllProgram->SQL Server ->configuraton tools->SQL Server Configuration Manager->SQL Server Network Configuration-> Select your server instance and right click on TCP/IP then goto IPAddresses. Change/Place port number for all IP's as 1433. Also make sure that the IP Address is Active and Enabled. Save it and restart the server.
  1. 确保您的 SQL Server Browser 服务处于活动状态。
  2. 转到 AllProgram->SQL Server -> 配置工具->SQL Server 配置管理器->SQL Server 网络配置-> 选择您的服务器实例并右键单击 TCP/IP,然后转到 IPAddresses。将所有 IP 的端口号更改/放置为 1433。还要确保 IP 地址处于活动状态并已启用。保存并重新启动服务器。