jdbc连接数据库执行sql主要是分更新和查询两种,为了减少冗余,常常抽取通用方法作为一类。(这里用的是mysql数据库)
DAO通用的步骤
1.获取连接
2.输入sql语句
3.创建prepareStatement对象
4.执行sql语句
(1)更新
(2)查询
5.关闭/处理异常
定义两个通用的方法:
(1)更新
public void Update(String sql,Object[] paramValue)
@sql 对于更新的sql操作语句
@paramValue sql语句中占位符对应的值
(2)查询
public <T> List<T> Query(String sql,Object[] paramValue, Class<T> clazz)
@sql 对于更新的sql操作语句
@paramValue sql语句中占位符对应的值
@Class<T> clazz 表示传入某一类型的字节码,就是要封装成这种类型的对象
这样就有了一个通用的dao类,命名为baseDao类,对于我们以后写的dao类,都要继承这个baseDao类。这个baseDao的代码如下:
/**
* 通用的dao类,所有的dao类度继承此类
* @author Administrator
*两个通用的方法:
* (1):更新
* (2):查询
*/
public class baseDao {
private Connection conn =null;
private PreparedStatement pst = null;
private ResultSet rs = null;
//更新的通用方法
//paramValue是sql语句中占位符对应的值
public void Update(String sql,Object[] paramValue){
//获取连接
conn = JdbcUtil.getConnection();
//创建prepareStatement对象
try {
pst = conn.prepareStatement(sql);
//通过参数元数据可获得占位符参数的个数
int count = pst.getParameterMetaData().getParameterCount();
//给占位符赋值
if(paramValue!=null&¶mValue.length>0){
for(int i =0;i<count;i++){
pst.setObject(i+1, paramValue[i]);
}
}
//执行更新
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JdbcUtil.closeAll(conn, pst, rs);
}
}
//查询的通用方法
//Class<T> clazz表示传入某一类型的字节码,就返回该字节码的对象,要封装的对象的类型
public <T> List<T> Query(String sql,Object[] paramValue, Class<T> clazz){
//返回的集合
List<T> list = new ArrayList<T>();
T t=null;
//1.获取连接
conn = JdbcUtil.getConnection();
//2.创建prepareStatement对象
try {
pst = conn.prepareStatement(sql);
//3.通过元数据得到sql语句占位符参数的个数
int count = pst.getParameterMetaData().getParameterCount();
//4.设置每个参数的值
if(paramValue!=null&¶mValue.length>0){
for(int i = 0;i<paramValue.length;i++){
pst.setObject(i+1,paramValue[i]);
}
}
//5.执行查询
rs = pst.executeQuery();
//6.获取结果集元数据
ResultSetMetaData rmd = rs.getMetaData();
//7.通过元数据获取列的个数
int rsCount = rmd.getColumnCount();
//8.遍历结果集
while(rs.next()){
//封装的对象
t = clazz.newInstance();
//遍历每一行每一列
for(int i = 0;i<rsCount;i++){
//通过结果集元数据得到每一列的名称
String columName = rmd.getColumnName(i+1);
//通过每一列的名称获取其对应的值
Object value = rs.getObject(columName);
//9.封装:得到了属性及其对应值,将其封装到对象中去,封装的对象类型就为传入的字节码的对象类型(这里用beanutil组件进行封装)
BeanUtils.copyProperty(t, columName, value);
}
list.add(t);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JdbcUtil.closeAll(conn, pst, rs);
}
return list;
}
}
这里写一个dao类来继承baseDao,获取baseDao的更新和查询功能
public class employeeDao extends baseDao{
//删除(更新)
public void delete(int id){
String sql = "delete from admin where id =?";
Object paramValue[] = {id};
super.Update(sql, paramValue);
}
//查询全部(查询)
public List<employee> findAll(){
String sql="select * from admin";
List<employee> list = super.Query(sql, null, employee.class);
return list;
}
//根据条件查询(查询)
public employee findById(int id){
String sql="select * from employee where id=?";
Object[] paramValue = {id};
List<employee> list = super.Query(sql, paramValue, employee.class);
return (list!=null&&list.size()>0) ? list.get(0) : null;
}
}
最后就可以对功能进行测试,建立employeeDao对象,调用更新或者查询的方法,传入参数就可以测试。
public class employeeTest {
@Test
public void query(){
employeeDao ed = new employeeDao();
//ed.delete(3);
List<employee> list = new ArrayList<employee>();
//employeeDao ed = new employeeDao();
list = ed.findAll();
System.out.println(list);
}
}