日志报错:NetworkSecurityConfig: No Network Security Config specified, using platform default
public static Connection getConnection(String dbName) throws SQLException {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver"); //加载驱动
String ip = "jdbc:mysql://rm-bp1t9js01f765a35k2o.mysql.rds.aliyuncs.com?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false";
conn =(Connection) DriverManager.getConnection(
"jdbc:mysql://" + ip + ":3306/" + dbName,
"zp_x", "Zpx008102");
MainActivity.conn_on=1;//用于向主函数传参,判断连接是否成功
}catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
MainActivity.conn_on=2;//用于向主函数传参,判断连接是否成功
}
return conn;//返回Connection型变量conn用于后续连接
}
```java
final Handler handler1 = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
switch (conn_on)//根据返回值判断网络连接是否成功
{
case 1:conn.setText("网络连接成功");conn.setBackgroundColor(Color.GREEN);break;
case 2:conn.setText("网络连接失败");break;
}
return false;
}
});
new Thread(new Runnable() {
@Override
public void run() {
Message msg = new Message();
try {
connect.getConnection("person");//执行连接测试
} catch (SQLException e) {
e.printStackTrace();
}
handler1.sendMessage(msg);//跳转到handler1
}
}).start();
```