我想连接 Java 类文件与 SQL 服务器2012。我已经使用 SQL 服务器身份验证登录,但是在连接时收到一个错误。
错误:
到主机127.0.0.1、端口1433的 TCP/IP 连接失败。 错误:”连接被拒绝: 连接。请验证连接属性。 确保 SQLServer 实例正在主机上运行,并且 在端口接受 TCP/IP 连接 与端口的连接不会被防火墙阻止。”。
我的代码:
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));
}