我想将Java类文件与SQL Server 2012连接。我已经使用SQL Server身份验证登录。但是我在连接中收到错误。
错误:到主机127.0.0.1的TCP / IP连接,端口1433失败。错误:“连接被拒绝:连接。请验证连接属性。确保主机上正在运行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));
}