您的位置:首页 > 产品设计 > UI/UE

第二阶段--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());
}
}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
Strickld 发布了21 篇原创文章 · 获赞 0 · 访问量 673 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: