6

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.

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.

4

5 回答 5

9
  1. 确保您的 SQL Server Browser 服务处于活动状态。
  2. 转到 AllProgram->SQL Server ->configuraton tools->SQL Server Configuration Manager->SQL Server Network Configuration-> 选择您的服务器实例并右键单击 TCP/IP,然后转到 IPAddresses。将所有 IP 的端口号更改/放置为 1433。还要确保 IP 地址处于活动状态并已启用。保存并重新启动服务器。
于 2017-04-14T11:38:32.557 回答
6

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

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

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

于 2013-08-02T16:18:24.523 回答
5

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

jdbc:jtds:sqlserver://<yourDBServerIPAddress>\SQLEXPRESS:1433;databaseName=AdventureWorks;user=sa;password=*****;
于 2013-08-02T20:33:26.343 回答
1

打开配置管理器:开始 -> Microsoft SQL Server -> 配置工具 -> SQL Server 配置管理器 启用 TCP/IP:从左侧树中选择: SQL Server Network Configuration-> Protocol for SQLEXPRESS-> TCP/IP 右键单击并启用它。双击 TCP/IP 并单击“IP 地址”选项卡添加 TCP/IP 端口:将 TCP 端口值输入 1433,然后单击应用重新启动 SQL Server:从左侧树中,选择:SQL Server Services -> SQL Server ( SQLEXPRESS) -> 右键单击​​并重新启动。

于 2020-10-28T13:30:13.390 回答
0

解决方案 1:使用mssql-jdbc-9.4.0.jre8.jar而不是使用 jtds 驱动程序

代替:

Class.forName("net.sourceforge.jtds.jdbc.Driver");
con = DriverManager.getConnection("jdbc:jtds:sqlserver://<IP>:port/<DatabaseName>",userid,password);

和:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
DriverManager.getConnection("jdbc:jtds:sqlserver://<IP>:port/<DatabaseName>",userid,password);

解决方案2:

在连接字符串中添加ssl=request参数

con = DriverManager.getConnection("jdbc:jtds:sqlserver://<IP>:port/<DatabaseName>;ssl=request",userid,password);
于 2021-12-16T12:44:37.710 回答