TCP/IP 连接到主机本地主机,端口 1433 出现故障错误,需要帮助
2022-09-03 08:38:13
我得到的完整错误:
与主机本地主机端口 1433 的 TCP/IP 连接失败。错误:“连接超时。验证连接属性。确保 SQL Server 实例在主机上运行,并在端口上接受 TCP/IP 连接。确保与端口的 TCP 连接未被防火墙阻止。
我已经使用端口1433检查了TCP / IP是否已启用,并且TCP动态端口为空。我禁用了窗口防火墙。
这是我的代码:
import java.sql.*;
public class DBConnect {
public static void main(String[] args) {
// TODO Auto-generated method stub
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=TestDB1;instance=SQLSERVER;encrypt=true;TrustServerCertificate=true;";
String user = "sa";
String pass = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myConn = DriverManager.getConnection(dbURL, user, pass);
try {
Statement myStmt = myConn.createStatement();
try {
ResultSet myRs = myStmt.executeQuery("Select * from Login");
while (myRs.next())
{
System.out.println(myRs.getString("Username"));
System.out.println(myRs.getString("Password"));
}
}
catch (Exception e)
{
System.out.println("Error with query");
}
}
catch (Exception e)
{
System.out.println("Error connecting to database");
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}