准备工作:添加引用
在AndroidManifest.xml文件内增加
<uses-permission android:name="android.permission.INTERNET" />
一、建立连接
//连接数据库,成功返回真,失败返回假
private boolean conSQL() throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
try {
String url = "jdbc:mysql://数据库连接外部地址:3306/数据库名称?autoReconnect=true&useSSL=false";
String name = "用户名";
String password = "密码";
con = DriverManager.getConnection(url, name, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (con != null) {
return true;
} else {
tv_ts1.post(() -> tv_ts1.setText("数据库未连接"));
return false;
}
}
二、查询数据
//查询并返