DAO模式、连接池和DBUtils应用
第一节 工具类封装、DAO模式和自定义连接池
1.1 工具类封装
案例实现:实现emp表的查询、添加、删除、修改
1.1.1 封装DbUtils
由于多个地方都需要使用数据库连接和释放,所以把功能封装到工具类中DbUtils
四个功能:1注册驱动 2 获取连接 3 释放资源 4 执行命令 【5 执行查询】
public class DbUtils {
private static String driver;
private static String url;
private static String user;
private static String password;
static{
try {
driver="com.mysql.jdbc.Driver";
url="jdbc:mysql://localhost:3306/school";
user="root";
password="root";
//加载驱动
Class.forName(driver);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取连接
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,user,password);
}
/**
* 释放资源
* @param rs
* @param stat
* @param conn
*/
public static void release(ResultSet rs,Statement stat,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stat!=null){
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 根据参数执行sql语句 Insert Delete Update
* @param sql
* @param params
* @return
*/
public static int executeUpdate(String sql, Object...params){
Connection conn=null;
PreparedStatement pstat=null;
try {
//1创建连接
conn=getConnection();
//2创建命令对象
pstat=conn.prepareStatement(sql);
//3设置参数
for (int i = 0; i < params.length; i++) {
pstat.setObject(i+1, params[i]);
}
//4执行
int result=pstat.executeUpdate();
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
release(null, pstat, conn);
}
}
}
1.1.2优化DbUtils工具类:把数据库连接信息封装到Properties文件中
Properties properties=new Properties();
InputStream is=DbUtils.class.getClassLoader().getResourceAsStream("database.properties");
properties.load(is);
//初始化参数
driver=properties.getProperty("driver");
url=properties.getProperty("url");
user=properties.getProperty("user");
password=properties.getProperty("password");
1.2 DAO设计模式
DAO(Database Access Object 数据库访问对象)
为了降低耦合性,提出了DAO封装数据库操作的设计模式。
它可以实现业务逻辑与数据库访问相分离。相对来说,数据库是比较稳定的,其中DAO组件依赖于数据库系统,提供数据库访问的接口,隔离了不同的数据库实现。
DAO模式的组成部分
1 DAO接口(主要包 添加 修改 查询 删除方法)
2 DAO实现类
3 实体类 (domain、beans、entity、pojo、model)
PO (VO)(Persistent Object, Value Object)
VO (View Object)
DTO (Data Transfer Object)
--作用:用在数据访问代码和业务逻辑代码之间通过实体类来传输数据
--实体类特征:
◦属性一般使用private修饰
◦提供public修饰的getter/setter方法
◦实体类提供无参构造方法,根据业务提供有参构造
◦实现java.io.Serializable接口,支持序列化机制
4 数据库连接和关闭工具类
设计的包名 :
domain 存放实体类
utils 存放工具类
dao 存放接口
dao.impl 存放实现类
使用DAO设计模式实现emp表的查询、添加、删除、修改
Employee类
/**
* 员工类
* 实体类
*
*/
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 373910607014836778L;
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private double sal;
private double comm;
private int deptno;
public Employee() {
// TODO Auto-generated constructor stub
}
public Employee(int empno, String ename, String job, int mgr, Date hiredate, double sal, double comm, int deptno) {
super();
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 int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}