`

策略设计模式_构建公共通用的Dao

阅读更多

核心代码:

Connection的工具类:详见JDBC数据库连接

StuDao的接口:

public interface StuDaointer {
 public void addStu(Stu stu);
 public void delStu(int sid);
 public void updStu(Stu stu);
 public Stu getOneStu(int sid);
 public List getAllStu();
}

 

公共的DAO///*************************************\\\

public abstract class CommDao {

    private ResultSet rs;

    private PreparedStatement pre;

    private Connection conn;

    private SQLDBConnutil connutil=SQLDBConnutil.getSQLDBConnutil();

   

    protected int update(String sql,Object[] obj){

       int n=0;

       conn=connutil.getConnection();

       try {

           pre=conn.prepareStatement(sql);

           if(obj!=null && obj.length>0){

              for(int i=0;i<obj.length;i++){

                  pre.setObject(i+1, obj[i]);

              }

           }

           n=pre.executeUpdate();

       } catch (SQLException e) {

           e.printStackTrace();

           throw new RuntimeException(e.getMessage());

       }finally{

           connutil.free(null, pre, conn);

       }

       return n;

    }

   

    protected Object getone(String sql,Object[] obj,MappingRow mp){

       Object myobj=null;

       conn=connutil.getConnection();

       try {

           pre=conn.prepareStatement(sql);

           if(obj!=null && obj.length>0){

              for(int i=0;i<obj.length;i++){

                  pre.setObject(i+1, obj[i]);

              }

           }

           rs=pre.executeQuery();

           if(rs.next()){

              myobj=mp.MapRow(rs);

           }

       } catch (Exception e) {

           e.printStackTrace();

           throw new RuntimeException(e.getMessage());

       }

       return myobj;

    }

   

    protected List getall(String sql,Object[] obj,MappingRow mp){

       List list=new ArrayList();

       Object myobj=null;

       conn=connutil.getConnection();

       try {

           pre=conn.prepareStatement(sql);

           if(obj!=null && obj.length>0){

              for(int i=0;i<obj.length;i++){

                  pre.setObject(i+1, obj[i]);

              }

           }

           rs=pre.executeQuery();

           while(rs.next()){

              list.add(mp.MapRow(rs));

           }

       } catch (Exception e) {

           e.printStackTrace();

           throw new RuntimeException(e.getMessage());

       }

       return list;

    }

}

//其中MappingRow接口如下:

public interface MappingRow {

    public Object MapRow(ResultSet rs);

}

具体对象的Dao实现如下:

public class StuDaoImple extends CommDao implements StuDaointer{

 public void addStu(Stu stu) {
  String sql="insert into stu values(?,?,?)";
  Object[] obj=new Object[]{stu.getSname(),stu.getSsex(),new java.sql.Date(stu.getSbrith().getTime())};
  super.update(sql, obj);
 }

 public void delStu(int sid) {
  String sql="delete stu where s_id=?";
  Object[] obj=new Object[]{sid};
  super.update(sql, obj);
 }
 //内部类
 public List getAllStu() {
  String sql="select * from stu";
  return super.getall(sql,null, new MappingRow(){

   public Object MapRow(ResultSet rs) {
    Stu stu=new Stu();
    try {
     stu.setSid(rs.getInt("s_id"));
     stu.setSname(rs.getString("s_name"));
     stu.setSsex(rs.getString("s_sex"));
     stu.setSbrith(rs.getDate("s_brith"));
    } catch (SQLException e) {
     e.printStackTrace();
    }
    return stu;
   }
   
  });
 }

 public Stu getOneStu(int sid) {
  String sql="select * from stu where s_id=?";
  Object[] obj=new Object[]{sid};
  return (Stu) super.getone(sql, obj,new MappingRow(){

   public Object MapRow(ResultSet rs) {
    Stu stu=new Stu();
    try {
     stu.setSid(rs.getInt("s_id"));
     stu.setSname(rs.getString("s_name"));
     stu.setSsex(rs.getString("s_sex"));
     stu.setSbrith(rs.getDate("s_brith"));
    } catch (SQLException e) {
     e.printStackTrace();
    }
    return stu;
   }
   
  });
 }

 public void updStu(Stu stu) {
  String sql="update stu set s_name=?,s_sex=?,s_brith=? where s_id=?";
  Object[] obj=new Object[]{stu.getSname(),stu.getSsex(),new java.sql.Date(stu.getSbrith().getTime()),stu.getSid()};
  super.update(sql, obj);
 }
 

 

 

 

2
1
分享到:
评论
1 楼 shiren1118 2009-04-23  
使用模板模式怎么样?

相关推荐

Global site tag (gtag.js) - Google Analytics