好的,我有这个代码:
package com.andrewxd.banksystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Interface
{
public static void main(String[] args)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1443;user=Andrew;password=andrei23;database=BankSystem");
System.out.println("test");
Statement sta = conn.createStatement();
String Sql = "select * from Clients";
ResultSet rs = sta.executeQuery(Sql);
System.out.println(rs.next());
} catch (Exception e){
e.printStackTrace();
}
}
}
但是它给了我这个错误有人可以帮助我吗?:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1443 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
我尝试过搜索,但我不太了解。
据我了解,端口不正确,但我如何找到正确的 IP/端口?
最佳答案
1) 打开 SQL Server 配置管理器并检查网络配置协议(protocol)下的 TCP/IP 是否已启用。
2) 在“连接”下的 SQL 服务器属性下,检查是否允许“允许远程连接到此服务器”。
3) 检查是否可以通过 SSMS 连接并查询数据库。
4) 在 SQL Server 配置管理器中检查 SQL Server Browser 服务是否正在运行。 (默认情况下未启用此功能,也未将其设置为默认情况下自动启动)。
5)如果所有这些都已设置,那么我将检查防火墙。
(对于任何可能遇到此问题的人,解决方案是允许 SQL Server 和 Windows 身份验证)
关于Java 连接到 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21833415/