JDBC 连接失败,错误:与主机的 TCP/IP 连接失败
2022-08-31 10:01:36
我想将Java类文件与SQL Server 2012连接。我已使用 SQL Server 身份验证登录,但在连接时收到错误。
错误:
与主机 127.0.0.1 端口 1433 的 TCP/IP 连接失败。错误:“连接被拒绝:连接。验证连接属性。确保 SQL Server 实例在主机上运行,并在端口上接受 TCP/IP 连接。确保与端口的 TCP 连接未被防火墙阻止。
我的代码:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //1. Register your driver
//2. get the connection object
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=aysha","sa","admin");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;databaseName=aysha","user=sa","password=admin");
//"jdbc:sqlserver://127.0.0.1:1433; Instance=SQL2008;" + "databaseName=MB;user=sa;password=123;";
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=aysha","sa" , "password");
//3. Prepare a statement
Statement stmt = con.createStatement();
//4. Write the query`
String sql = "Select * from employee";
//5. Execute the statement and
ResultSet rs = stmt.executeQuery(sql);
//6. Process the result set
while (rs.next())
{
System.out.println(rs.getInt(1));
}