第二阶段--DBUtils工具类和Druid连接池的使用
2020-01-14 14:15
274 查看
在项目下的lib目录中导入三个jar包:connector,druid,dbutils
配置文件druid放在src目录下
//配置文件druid.properties #连接设置 driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mydb1?useSSL=true&characterEncoding=utf8 username=root password=root #<!-- 初始化连接 --> initialSize=10 #最大连接数量 maxActive=50 #<!-- 最小空闲连接 --> minIdle=5 #<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 --> maxWait=5000 //utils包 public class DataSourceUtils { //声明连接池对象 private static DruidDataSource dataSource; static { try { //实例化配置对象 Properties properties=new Properties(); //加载配置文件内容,该配置文件应该放在src下面,而不是项目下面 InputStream is = DataSourceUtils.class.getClassLoader().getResourceAsStream("druid.properties"); properties.load(is); dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } //静态方法通过类名调用该方法获得连接池,并同时与数据库获得连接 public static DataSource getDataSource(){ return dataSource; } } //domain实体类 public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Float sal; private Float comm; private Integer deptno; public Emp() { } public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Float sal, Float comm, Integer deptno) { this.empno = empno; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; this.deptno = deptno; } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Float getSal() { return sal; } public void setSal(Float sal) { this.sal = sal; } public Float getComm() { return comm; } public void setComm(Float comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } @Override public String toString() { return "Emp{" + "empno=" + empno + ", ename='" + ename + '\'' + ", job='" + job + '\'' + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + '}'; } } //dao包下的接口和实现类 public interface EmpDao { List<Emp> findAll(); Emp findByEmpno(Integer empno); void add(Emp e); void update(Emp e); void delete(Integer empno); } public class EmpDaoImpl implements EmpDao { @Override public List<Emp> findAll() { QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); try { //返回List集合结果集 return qr.query("select * from emp",new BeanListHandler<Emp>(Emp.class)); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("查询失败",e); } } @Override public Emp findByEmpno(Integer empno) { QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); try { return qr.query("select * from emp where empno=?",new BeanHandler<Emp>(Emp.class),empno); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("查询失败",e); } } @Override public void add(Emp e) { //创建查询执行器,可以调用update方法,并指定连接池 QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); Object[] params={e.getEmpno(),e.getEname(),e.getJob(),e.getMgr(),e.getHiredate(),e.getSal(),e.getComm(),e.getDeptno()}; //广义上的update包括增、删、改操作 try { qr.update("insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values(?,?,?,?,?,?,?,?)",params); } catch (SQLException ex) { ex.printStackTrace(); throw new RuntimeException("添加失败",ex); } } @Override public void update(Emp e) { //创建查询执行器,可以调用update方法,并指定连接池 QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); Object[] params={e.getEname(),e.getJob(),e.getMgr(),e.getHiredate(),e.getSal(),e.getComm(),e.getDeptno(),e.getEmpno()}; //广义上的update包括增、删、改操作 try { qr.update("update emp set ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=? where empno=?",params); } catch (SQLException ex) { ex.printStackTrace(); throw new RuntimeException("更新失败",ex); } } @Override public void delete(Integer empno) { //创建查询执行器,可以调用update方法,并指定连接池 QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); //广义上的update包括增、删、改操作 try { qr.update("delete from emp where empno=?",empno); } catch (SQLException ex) { ex.printStackTrace(); throw new RuntimeException("删除失败",ex); } } } //测试类 public class EmpDaoImplTest { @Test public void testAdd(){ EmpDao empDao=new EmpDaoImpl(); Emp e=new Emp(11,"小明","java",0,new Date(),new Float(2000),new Float(5000),100); empDao.add(e); System.out.println("添加成功"); } @Test public void testUpdate(){ EmpDao empDao=new EmpDaoImpl(); Emp e=new Emp(11,"李白","搬砖",1,new Date(),new Float(30000),new Float(5000),100); empDao.update(e); System.out.println("更新成功"); } @Test public void testDelete(){ EmpDao empDao=new EmpDaoImpl(); empDao.delete(11); System.out.println("删除成功"); } @Test public void testFindAll(){ EmpDao empDao=new EmpDaoImpl(); List<Emp> all = empDao.findAll(); for (Emp emp : all) { System.out.println(emp.toString()); } } @Test public void testFindByEmpno(){ EmpDao empDao=new EmpDaoImpl(); Emp e = empDao.findByEmpno(7782); System.out.println(e.toString()); } }
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 使用DButils、c3p0连接池、Druid连接池操作MYSQL数据库
- Druid连接池与DbUtils整合工具类
- c3p0连接池结合dbutils工具类操作数据库的简单使用。
- Spring Boot下Druid连接池的使用配置分析
- spring boot 学习(四)Druid连接池的使用配置
- 连接池和使用dbutils
- Mybatis框架的连接池的使用工具类
- 使用druid连接池的超时回收机制排查连接泄露问题
- spring jdbc --注:c3p0数据库连接池或druid连接池使用配置整理
- Druid使用起步—在javaWeb项目中配置监控 连接池
- Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
- JavaWeb之DButils(二)使用DBCP实现数据库的连接池
- SpringBoot实践之---Druid连接池的使用和监控配置
- 使用druid连接池的超时回收机制排查连接泄露问题
- DBCP连接池及其工具类的使用
- JDBC的C3P0连接池和DBUtils工具类
- JDBC相关(三):DButils工具类的使用详解
- 使用druid连接池,配置sql防火墙发现的sql注入问题
- DRUID连接池的使用
- Java Druid大数据连接池的实例应用 和工具类