这是我从Android应用程序连接到SqlServer的代码
private Connection CONN(String _DB, String _server)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String connectionUrl = "jdbc:sqlserver://AhmedPc:1433; Instance=SQL2008;" +
"databaseName=admin1;integratedSecurity=true;";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
String SQL = "SELECT TOP 10 * FROM Person.Contact";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next())
{
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
return con;
}
我收到此错误
当我使用IP地址42.xx.xx.xx时,我得到这个::::
com.microsoft.sqlserver.jdbc.SQLServerException:与主机的TCP / IP连接失败。 java.net.ConnectException:无法连接到/(端口1433):连接失败:ETIMEDOUT(连接超时)
当我使用127.0.0.1时,我得到这个:::::
com.microsoft.sqlserver.jdbc.SQLServerException:与主机的TCP / IP连接失败。 java.net.ConnectException:无法连接到/(端口1433):连接失败:ECONNREFUSED(拒绝连接)