一月二十九 安卓开发八
继续连接数据库
创建java类
package com.example.myapplication; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class DbOpenHelper { static final String USER="root"; static final String PWD="123456"; static final String CLS="com.mysql.cj.jdbc.Driver"; static final String URL = "jdbc:mysql://192.168.60.241:3306/期末考试?useSSL=false&serverTimezone=UTC"; public static Connection conn; public static Statement stmt; public static PreparedStatement pStmt; public static ResultSet rs; public static void getConnection(){ try{ Class.forName(CLS); conn= DriverManager.getConnection(URL,USER,PWD); }catch (Exception ex){ ex.printStackTrace(); } } public static void closeAll(){ try{ if(rs!=null){ rs.close(); rs=null; } if(stmt!=null){ stmt.close(); stmt=null; } if(pStmt!=null){ pStmt.close(); pStmt=null; } if(conn!=null){ conn.close(); conn=null; } }catch (Exception ex) { ex.printStackTrace(); } } }
创建Userinfo.java类
package com.example.myapplication; import java.io.Serializable; //用户信息类 public class Userinfo implements Serializable{ private int id; private String uname; private String upass; private String creatDt; public Userinfo(){ } public Userinfo(int id, String uname, String upass, String creatDt) { this.id = id; this.uname = uname; this.upass = upass; this.creatDt = creatDt; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } public String getCreatDt() { return creatDt; } public void setCreatDt(String creatDt) { this.creatDt = creatDt; } }
创建UserDo.java类
package com.example.myapplication; import java.util.ArrayList; import java.util.List; public class UserDao extends DbOpenHelper { public List<Userinfo> getAllUserList(){ List<Userinfo> list=new ArrayList<>(); return list; } }