jdbc連結資料庫去log(c3p0日誌)版
阿新 • • 發佈:2021-11-13
//先建立一個包名字為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() { returnage; } 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); } }
說明:此方法在網上某位大佬學到的。。。