将SQLAzure与Android连接

将SQLAzure与Android连接,android,sql-server,azure,Android,Sql Server,Azure,如何连接数据库? 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

如何连接数据库?

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 Server已允许ip地址

如果您使用的是android,则不希望直接连接到sql Server。当您拥有像现在这样的连接字符串时,任何人都可以从您的应用程序中获取它,并在sql server上进行查询。这将向所有人公开您的所有数据。通常在android应用程序和sql server之间放置一个服务器(web服务器)。服务器处理保存等的安全和业务逻辑。

如果您使用的是android,则不希望直接连接到sql server。当您拥有像现在这样的连接字符串时,任何人都可以从您的应用程序中获取它,并在sql server上进行查询。这将向所有人公开您的所有数据。通常在android应用程序和sql server之间放置一个服务器(web服务器)。服务器处理用于保存等的安全和业务逻辑