JDBC 连接失败,错误:到主机的 TCP/IP 连接失败 - JDBC connection failed, error: TCP/IP connection to host failed

问题:

I want to connect Java class file with SQL server 2012. I have logged in with SQL server authentication, but I am receiving an error when connecting.我想将 Java 类文件与 SQL Server 2012 连接。我已使用 SQL Server 身份验证登录,但在连接时收到错误消息。

Error:错误:

The TCP/IP connection to the host 127.0.0.1, port 1433 has failed.与主机 127.0.0.1、端口 1433 的 TCP/IP 连接失败。 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.".错误:“连接被拒绝:连接。验证连接属性。确保 SQL Server 实例正在主机上运行并在端口接受 TCP/IP 连接。确保到端口的 TCP 连接未被防火墙阻止.”。

My code:我的代码:

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));
}

解决方案:

参考: https://stackoom.com/en/question/1H3b6
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章