android azure连接

yhxst69z  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(253)

我正在开发一个android应用程序,从azure sql数据库获取数据。
应用程序在尝试获取数据时引发异常(sqlexception)。显示的错误消息是 Network error IOException: SSL handshake aborted: ssl=0x9a56a8e8: I/O error during system call, Broken pipe . 不提取任何数据。
然而,只有在使用安卓10及更低版本时才会发生这种情况,并且在安卓11中工作并成功获取数据。
我已将我的代码张贴在下面:

  1. public class MainActivity extends AppCompatActivity {
  2. public TextView textView;
  3. public Button button;
  4. public Connection con;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. textView = findViewById(R.id.textView);
  10. button = findViewById(R.id.button);
  11. button.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View view) {
  14. CheckLogin checkLogin = new CheckLogin();
  15. checkLogin.execute("");
  16. }
  17. });
  18. }
  19. public class CheckLogin extends AsyncTask<String, String, String> {
  20. String z = "";
  21. Boolean isSuccess = false;
  22. String name1 = "";
  23. @Override
  24. protected void onPreExecute() {
  25. super.onPreExecute();
  26. }
  27. @Override
  28. protected void onPostExecute(String r) {
  29. Toast.makeText(MainActivity.this, r, Toast.LENGTH_LONG).show();
  30. if (isSuccess) {
  31. textView = findViewById(R.id.textView);
  32. textView.setText(name1);
  33. }
  34. }
  35. @Override
  36. protected String doInBackground(String... params) {
  37. try {
  38. con = connectionclass();
  39. if (con == null) {
  40. z = "Check your internet access!";
  41. } else {
  42. //String query = "select * from Users";
  43. Statement stmt = con.createStatement();
  44. //Check what databases are available (check if connecting to correct place
  45. Log.e("Error",stmt.getConnection().getCatalog());
  46. ResultSet rs = stmt.executeQuery("SELECT * FROM Users");
  47. // try while???
  48. if (rs.next()) {
  49. name1 = rs.getString("Name"); // Name is a column in our Users table
  50. z = "query successful";
  51. isSuccess = true;
  52. con.close();
  53. } else {
  54. z = "invalid query";
  55. isSuccess = false;
  56. }
  57. }
  58. } catch (Exception exception) {
  59. isSuccess = false;
  60. z = exception.getMessage();
  61. Log.d("sql error", z);
  62. }
  63. return z;
  64. }
  65. }
  66. @SuppressLint("NewApi")
  67. public Connection connectionclass() {
  68. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  69. StrictMode.setThreadPolicy(policy);
  70. Connection connection = null;
  71. try {
  72. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  73. String hostName = "azuretestappandroid";
  74. String dbName = "AppTestingDatabase";
  75. String user = "Tester@azuretestappandroid";
  76. String userPwd = "XXX";
  77. String connectionURL = String.format("jdbc:jtds:sqlserver://%s.database.windows.net:1433/%s;user=%s;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;ssl=required",hostName,dbName,user,userPwd);
  78. connection = DriverManager.getConnection(connectionURL,user,userPwd);
  79. } catch (SQLException exception) {
  80. Log.e("error here 1: ", exception.getMessage());
  81. } catch (ClassNotFoundException exception) {
  82. Log.e("error here 2: ", exception.getMessage());
  83. } catch (Exception exception) {
  84. Log.e("error here 3: ", exception.getMessage());
  85. }
  86. return connection;
  87. }
  88. }

我确实理解jtds库已经过时,因此如果您对如何向azure sql数据库读写数据有任何建议,我们将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题