如何连接数据库?
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
String connectionString = "jdbc:sqlserver://xxxxx.database.windows.net:1433;database=navili;user=xxxxxx;password=xxxxx;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
PreparedStatement prepsInsertPerson = null;
PreparedStatement prepsUpdateAge = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(connectionString);
/*String selectSql = "SELECT Username, Password FROM dbo.System_Users";
statement = connection.createStatement();
resultSet = statement.executeQuery(selectSql);*/
// Iterate through the result set and print the attributes.
/*while (resultSet.next()) {
System.out.println(resultSet.getString(2) + " "
+ resultSet.getString(3));
}*/
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Close the connections after the data has been handled.
if (prepsInsertPerson != null) try { prepsInsertPerson.close(); } catch(Exception e) {}
if (prepsUpdateAge != null) try { prepsUpdateAge.close(); } catch(Exception e) {}
if (resultSet != null) try { resultSet.close(); } catch(Exception e) {}
if (statement != null) try { statement.close(); } catch(Exception e) {}
if (connection != null) try {
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
connection.close();} catch(Exception e) {}
else{
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
}
}
错误消息
com.example.tao.navi W / System.err: com.microsoft.sqlserver.jdbc.SQLServerException:驱动程序不能 使用安全套接字建立与SQL Server的安全连接 层(SSL)加密。错误:“套接字关闭”。
Azure服务器已允许ip地址
答案 0 :(得分:0)
如果你在Android上,你不想直接连接到SQL服务器。当你拥有像现在这样的连接字符串时,任何人都可以从你的应用程序中获取它并在你的sql server上进行查询。这会将所有数据公开给每个人。您通常在您的Android应用程序和您的SQL Server之间放置一个服务器(Web服务器)。服务器处理安全和业务逻辑以便保存等。