DAO
public abstract class AbstractDao {
protected abstract Object rowMapper(ResultSet rs)throws SQLException;
public Object find(String sql,Object[] args) {
Object obj=null;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
// 建立连接
// conn=JdbcUtils.getConnection();
conn=JdbcUtilsSing.getInstance().getConnection();//使用单例
ps=conn.prepareStatement(sql);
for(int i=0;i<args.length;i++){
ps.setObject(i+1, args[i]);
}
rs=ps.executeQuery();
if(rs.next()){
obj=rowMapper(rs);
}
} catch (Exception e) {
//对于异常,能处理就处理,不能处理就抛出,不能隐藏
//在不污染接口的情况下通知上一层,给上层选择处理的权力
throw new DaoException(e.getMessage(),e);
}finally{
// 释放资源
JdbcUtils.free(rs, ps, conn);
}
return obj;
}
public void update(String sql,Object[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 建立连接
// conn=JdbcUtils.getConnection();
conn = JdbcUtilsSing.getInstance().getConnection();// 使用单例
ps = conn.prepareStatement(sql);
for(int i=0;i<args.length;i++){
ps.setObject(i+1, args[i]);
}
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
JdbcUtils.free(rs, ps, conn);
}
}
}
------------------------------------------------------------------------
public class UserDaoImpl extends AbstractDao {
public void delete(User user) {
String sql="delete from user where id=?";
Object[] args=new Object[]{user.getId()};
super.update(sql, args);
}
public void update(User user) {
String sql = "update user set name=?,birthday=?,money=? where id=?";
Object[] args = new Object[] { user.getName(), user.getBirthday(),
user.getMoney(), user.getId() };
super.update(sql, args);
}
@Override
protected Object rowMapper(ResultSet rs) throws SQLException {
User user;
user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setMoney(rs.getFloat("money"));
user.setBirthday(rs.getDate("birthday"));
return user;
}
public User finUser(String loginName, String password) {
String sql="select id,name,birthday,money from user where name=?";
Object[] args=new Object[]{loginName};
Object user=super.find(sql,args);
return (User)user;
}
}
------------------------------------------------------------------------------------------------------
策略模式(Strategy):定义一系列的算法,将每个算法封装到具有共同接口的独立类中,以便使得算法可以相互替换,而不影响客户端。
public interface RowMapper {
public Object mapRow(ResultSet rs)throws SQLException;
}
-----------------------------------------------------------------------------------------
public class MyDaoTemplate {
protected Object rowMapper(ResultSet rs) throws SQLException {
return null;
}
public Object find(String sql, Object[] args, RowMapper rowMapper) {
Object obj = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 建立连接
// conn=JdbcUtils.getConnection();
conn = JdbcUtilsSing.getInstance().getConnection();// 使用单例
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
if (rs.next()) {
obj = rowMapper.mapRow(rs);
}
} catch (Exception e) {
// 对于异常,能处理就处理,不能处理就抛出,不能隐藏
// 在不污染接口的情况下通知上一层,给上层选择处理的权力
throw new DaoException(e.getMessage(), e);
} finally {
// 释放资源
JdbcUtils.free(rs, ps, conn);
}
return obj;
}
public void update(String sql, Object[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 建立连接
// conn=JdbcUtils.getConnection();
conn = JdbcUtilsSing.getInstance().getConnection();// 使用单例
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
JdbcUtils.free(rs, ps, conn);
}
}
}
------------------------------------------------------------------------------------------------------
public class UserDaoImpl2{
MyDaoTemplate template=new MyDaoTemplate();
public User findUser(String loginName, String password) {
String sql="select id,name,birthday,money from user where name=?";
Object[] args=new Object[]{loginName};
Object user=this.template.find(sql, args, new UserRowMapper());
return (User) user;
}
public String finderUserName(int id){
String sql="select name from user when id=?";
Object[] args=new Object[]{id};
Object name=this.template.find(sql, args, new RowMapper(){
public Object mapRow(ResultSet rs) throws SQLException{
return rs.getString("name");
}});
return (String)name;
}
class UserRowMapper implements RowMapper{
@Override
public Object mapRow(ResultSet rs) throws SQLException {
User user;
user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setMoney(rs.getFloat("money"));
user.setBirthday(rs.getDate("birthday"));
return user;
}
}
}