android连接mysql
导入Mysql的jar,和rxjava
implementation "io.reactivex.rxjava2:rxjava:2.2.10"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
创建DBOpenHelper
package com.example.connectmysql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBOpenHelper { private static String diver = "com.mysql.jdbc.Driver"; //加入utf-8是为了后面往表中输入中文,表中不会出现乱码的情况 private static String url = "jdbc:mysql://ip:port/your db's name?characterEncoding=utf-8"; private static String user = "user";//用户名 private static String password = "password";//密码 /* * 连接数据库 * */ public static Connection getConn(){ Connection conn = null; try { Class.forName(diver); conn = (Connection) DriverManager.getConnection(url,user,password);//获取连接 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } }
创建bean类
package com.example.connectmysql; public class StudentBean { String name; int id; int age; String major; public String getName() { return name; } public int getId() { return id; } public int getAge() { return age; } public String getMajor() { return major; } public void setName(String name) { this.name = name; } public void setId(int id) { this.id = id; } public void setAge(int age) { this.age = age; } public void setMajor(String major) { this.major = major; } }
在MainActivity中加入
private void executeAsyncTask(int i) { //Observable#create方法创建一个异步任务 Observable.create(new ObservableOnSubscribe<Integer>() { @Override public void subscribe(@NonNull ObservableEmitter<Integer> e) throws Exception { new Thread(new Runnable() { @Override public void run() { Connection conn =(Connection) DBOpenHelper.getConn(); Statement st = null; try {st = (Statement) conn.createStatement();} catch (SQLException ex) {ex.printStackTrace();} switch (i){ case 1: String sql = "select * from student"; try { ResultSet rs = st.executeQuery(sql); while (rs.next()){ studentBean=new StudentBean(); studentBean.setName(rs.getString("name")); studentBean.setId(rs.getInt("id")); studentBean.setAge(rs.getInt("age")); studentBean.setMajor(rs.getString("major")); e.onNext(0); } st.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } break; case 2: String sqlInsert = "insert into student (id,name,age,major) values (?,?, ?,?)"; try { PreparedStatement ps = conn.prepareStatement(sqlInsert); ps.setInt(1,202121103); ps.setString(2,"names"); ps.setInt(3, 19); ps.setString(4,"cs"); ps.executeUpdate(); st.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); Log.e("-->",e.getMessage()); } e.onComplete(); break; case 3: String sqlDele = "delete from student where id = ?"; try { PreparedStatement ps = conn.prepareStatement(sqlDele); ps.setInt(1,202121101); ps.executeUpdate(); st.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); Log.e("-->",e.getMessage()); } e.onComplete(); break; case 4: String sqlUpdate = "update student set age = ? where id = ?"; try { PreparedStatement ps = conn.prepareStatement(sqlUpdate); ps.setInt(1,22); ps.setInt(2,202120100); ps.executeUpdate(); st.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); Log.e("-->",e.getMessage()); } e.onComplete(); break; } } }).start(); } }) .subscribe(new Observer<Integer>() { @Override public void onSubscribe(@NonNull Disposable d) { } @Override public void onNext(@NonNull Integer integer) { Log.e("-->",studentBean.getName()+"\n" +studentBean.getId()+"\n" +studentBean.getAge()+"\n" +studentBean.getMajor()); } @Override public void onError(@NonNull Throwable e) { } @Override public void onComplete() { Log.e("-->","COMP"); } }); }
直接用mysql语句即可
GitHub地址 下载前给star