我正在开发一个android应用程序,从azure sql数据库获取数据。
应用程序在尝试获取数据时引发异常(sqlexception)。显示的错误消息是 Network error IOException: SSL handshake aborted: ssl=0x9a56a8e8: I/O error during system call, Broken pipe
. 不提取任何数据。
然而,只有在使用安卓10及更低版本时才会发生这种情况,并且在安卓11中工作并成功获取数据。
我已将我的代码张贴在下面:
public class MainActivity extends AppCompatActivity {
public TextView textView;
public Button button;
public Connection con;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckLogin checkLogin = new CheckLogin();
checkLogin.execute("");
}
});
}
public class CheckLogin extends AsyncTask<String, String, String> {
String z = "";
Boolean isSuccess = false;
String name1 = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String r) {
Toast.makeText(MainActivity.this, r, Toast.LENGTH_LONG).show();
if (isSuccess) {
textView = findViewById(R.id.textView);
textView.setText(name1);
}
}
@Override
protected String doInBackground(String... params) {
try {
con = connectionclass();
if (con == null) {
z = "Check your internet access!";
} else {
//String query = "select * from Users";
Statement stmt = con.createStatement();
//Check what databases are available (check if connecting to correct place
Log.e("Error",stmt.getConnection().getCatalog());
ResultSet rs = stmt.executeQuery("SELECT * FROM Users");
// try while???
if (rs.next()) {
name1 = rs.getString("Name"); // Name is a column in our Users table
z = "query successful";
isSuccess = true;
con.close();
} else {
z = "invalid query";
isSuccess = false;
}
}
} catch (Exception exception) {
isSuccess = false;
z = exception.getMessage();
Log.d("sql error", z);
}
return z;
}
}
@SuppressLint("NewApi")
public Connection connectionclass() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String hostName = "azuretestappandroid";
String dbName = "AppTestingDatabase";
String user = "Tester@azuretestappandroid";
String userPwd = "XXX";
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);
connection = DriverManager.getConnection(connectionURL,user,userPwd);
} catch (SQLException exception) {
Log.e("error here 1: ", exception.getMessage());
} catch (ClassNotFoundException exception) {
Log.e("error here 2: ", exception.getMessage());
} catch (Exception exception) {
Log.e("error here 3: ", exception.getMessage());
}
return connection;
}
}
我确实理解jtds库已经过时,因此如果您对如何向azure sql数据库读写数据有任何建议,我们将不胜感激。
暂无答案!
目前还没有任何答案,快来回答吧!