1.首先要确保你的mysql是打开的状态,打开idea创建maven工程。
接下来就是在工程pom.xml里面打jar包
jar包打完,在你左侧External Libraries目录下会有相对应的jar包,我这里打的jar包是mysql-connector-java:5.1.47。
2.创建datasource.porperties文件,里面有我们需要的一些配置信息,要记住文件的根目录。
我这里datasource.porperties文件的根目录是
D:\\IdeaProjects\\conf\\datasource.properties
3.进入正题,敲代码,创建一个Dao类。
import java.io.FileReader; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.*; import java.util.*; /** * @Author HanLiang * @Date 2021/12/9 * @Description */ public class Dao { private static Properties prop; public static void init(String path) throws IOException, ClassNotFoundException { prop = new Properties(); prop.load(new FileReader(path)); Class.forName(prop.getProperty("mysql.driver")); } private static Connection con() throws SQLException { return DriverManager.getConnection(prop.getProperty("mysql.url"), prop.getProperty("mysql.username"), prop.getProperty("mysql.password")); } private static PreparedStatement pst(Connection con, String sql, Object... params) throws SQLException { PreparedStatement pst = con.prepareStatement(sql); for (int i = 0; i < params.length; i++) { pst.setObject(i + 1, params[i]); } return pst; } private static void close(AutoCloseable... closes) { for (AutoCloseable close : closes) { if (null != close) { try { close.close(); } catch (Exception e) { e.printStackTrace(); } } } } public static int exeUpdate(String sql, Object... params) {//增删改的方法 int rst = -1; Connection con = null; PreparedStatement pst = null; try { con = con(); pst = pst(con, sql, params); rst = pst.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } return rst; } private static Map<String, Method> parseClass(Class c){ Map<String, Method> map = new HashMap<>(); for (Method method : c.getMethods()) { String name = method.getName(); if (name.startsWith("set")){ name = name.substring(3); name=name.substring(0,1).toLowerCase()+name.substring(1); map.put(name,method); } } return map; } private static <T> T parseObject(Class<T> c,Map<String,Method> map,ResultSet rst) throws IllegalAccessException, InstantiationException, SQLException, InvocationTargetException { T t = c.newInstance(); for (Map.Entry<String, Method> e : map.entrySet()) {//增强for遍历 e.getValue().invoke(t,rst.getObject(e.getKey())); } return t; } //查询的方法 public static<T> List<T> exeQurey(Class<T> c,String sql,Object...params){ List<T> list = new ArrayList<>(); Connection con=null; PreparedStatement pst=null; ResultSet rst=null; try { con=con(); pst=pst(con,sql,params); rst = pst.executeQuery(); Map<String, Method> map = parseClass(c); while (rst.next()) { list.add(parseObject(c,map,rst)); } } catch (Exception e) { e.printStackTrace(); }finally { close(rst,pst,con); } return list; } public static void main(String[] args) throws IOException, ClassNotFoundException { init("D:\\IdeaProjects\\conf\\datasource.properties"); List<Teacher> list = exeQurey(Teacher.class, "select * from teacher"); for (Teacher e : list) { System.out.println(e); } } }
我们在main方法里调用l查询的方法,需要一个Teacher实体类,实体类对应数据库里面的表格,实体类的属性即是表格的字段,表格里一行数据就是实体类的一个对象。我们必须要xian创建一个实体类:
/** * @Author HanLiang * @Date 2021/12/9 * @Description */ public class Teacher { private int tid; private String tname; private String Gender; public Teacher() { } public Teacher(int tid, String tname, String gender) { this.tid = tid; this.tname = tname; Gender = gender; } public int getTid() { return tid; } public void setTid(int tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public String getGender() { return Gender; } public void setGender(String gender) { Gender = gender; } @Override public String toString() { return "Teacher{" + "tid=" + tid + ", tname='" + tname + '\'' + ", Gender='" + Gender + '\'' + '}'; } }