//先创建一个包名字为com.mchange.v2.log 必须为这个名 //写个Emp实体类 用来暂时存储用户输入的数据 package com.mchange.v2.log; public class Emp { private int id; private String name; private int age; public Emp() { } public Emp(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Emp{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
//写一个工具类DBUtils 里面写一个方法getDateSource() 用来获取数据源 package com.mchange.v2.log; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; public class DBUtils { public static DataSource getDateSource(){ DataSource ds = new ComboPooledDataSource(); return ds; } }
//写一个员工数据访问类 用来访问数据库 进行增删改查 package com.mchange.v2.log; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import java.sql.SQLException; import java.util.List; public class EmpDaoImpl { private QueryRunner qr = new QueryRunner(DBUtils.getDateSource()); public List<Emp> getAllEmp(){ String sql="select * from emp1"; List<Emp> list=null; try { list= qr.query(sql, new BeanListHandler<>(Emp.class)); } catch (SQLException throwables) { throwables.printStackTrace(); } return list; } public int addEmp(Emp emp){ String sql="insert into emp1 values(?,?,?)"; int rows=0; try { rows=qr.update(sql,emp.getId(),emp.getName(),emp.getAge()); } catch (SQLException throwables) { throwables.printStackTrace(); }if (rows>0){ System.out.println("添加成功!"); EmpDaoImpl empDao=new EmpDaoImpl(); for (Emp e:empDao.getAllEmp()) { System.out.println(e); } }else { System.out.println("添加失败!"); } return rows; } public int deleteEmp(int id){ String sql="delete from emp1 where id=?"; int rows=0; try { rows = qr.update(sql, id); } catch (SQLException throwables) { throwables.printStackTrace(); } if (rows>0){ System.out.println("删除成功!"); EmpDaoImpl empDao=new EmpDaoImpl(); for (Emp e:empDao.getAllEmp()) { System.out.println(e); } }else { System.out.println("删除失败!"); } return rows; } public int updataEmp(Emp emp){ String sql="update emp1 set name=?,age=? where id=?"; int rows=0; try { rows = qr.update(sql, emp.getName(), emp.getAge(), emp.getId()); } catch (SQLException throwables) { throwables.printStackTrace(); } if (rows>0){ System.out.println("修改成功!"); EmpDaoImpl empDao=new EmpDaoImpl(); for (Emp e:empDao.getAllEmp()) { System.out.println(e); } }else { System.out.println("修改失败!"); } return rows; } }
//写一个降低日志输出等级类 package com.mchange.v2.log; import java.util.logging.Level; public class AmendLogLevel { public static void amendInfoLevel(Level level){ MLevel.INFO.level=level; } }
//写个测试类 测试对员工数据进行增删改查 package com.mchange.v2.log; import java.util.logging.Level; public class Test { static { AmendLogLevel.amendInfoLevel(Level.ALL); } public static void main(String[] args) { EmpDaoImpl empDao=new EmpDaoImpl(); empDao.addEmp(new Emp(6,"daming",24)); //empDao.deleteEmp(4); //empDao.updataEmp(new Emp(6,"xiaoming",20)); //empDao.getAllEmp(); //empDao.deleteEmp(5); } }
说明:此方法在网上某位大佬学到的。。。