结合ValueOject 模式,代理模式,门面模式,建议你看看j2ee 设计模式

解决方案 »

  1.   

    我用的就是ValueObject模式,代码如下:public class FlightSCHDBean implements EntityBean {
        EntityContext entityContext;
        long flightID;
        FlightSCHDData flightSCHDData;
     
        public void ejbLoad() {      
            FlightSCHDPK pk = (FlightSCHDPK)entityContext.getPrimaryKey();
            setFlightID(pk.flightID);
            try {
                flightSCHDData = selectByPrimaryKey(new FlightSCHDPK(getFlightID()));
                setFlightSCHDData(flightSCHDData);
            }
            catch (SQLException se) {
                logger.error(se.getErrorCode()+se.getMessage());
            }
        }    public void setFlightSCHDData(FlightSCHDData flightSCHDData) {
            this.flightSCHDData = flightSCHDData;
        }
        public FlightSCHDData getFlightSCHDData() {
            return flightSCHDData;
        }    /////////////////////////////////////// SQL methods
        private FlightSCHDData selectByPrimaryKey(FlightSCHDPK pk) throws SQLException {
            Connection con = null;
            PreparedStatement prepStmt =null;
            FlightSCHDData plandata=null;
            try{
            String selectStatement = "select* from table "         
            con = DBConnectionFactory.getConnection();
            prepStmt = con.prepareStatement(selectStatement);
            prepStmt.setLong(1, pk.flightID);
            ResultSet rs = prepStmt.executeQuery();        if (rs != null && rs.next()) {
                plandata = createFlightSCHDData(rs);
            }
            if(rs!=null)
                rs.close();
            }
            catch(SQLException se){
                DBConnectionFactory.closeStatement(prepStmt);
                DBConnectionFactory.releaseConnection(con);
                throw se;
            }
            finally{
                DBConnectionFactory.closeStatement(prepStmt);
                DBConnectionFactory.releaseConnection(con);
            }
            return plandata;
        }   public java.util.Collection ejbFindByQueryStr(String query) throws FinderException {      String selectStatement = "select FLIGHT_ID from " + TableName.FLTOPRSCHD +"  Q  "+ query;
          Connection con = null;
          Statement stmt = null;
          ArrayList a = new ArrayList();
          try {
              con = DBConnectionFactory.getConnection();
              stmt = con.createStatement();
              ResultSet rs = stmt.executeQuery(selectStatement);          while (rs != null && rs.next()) {
                  FlightSCHDPK pk = new FlightSCHDPK(rs.getLong(1));
                  a.add(pk);
              }
              if(rs !=null) rs.close();
              logger.debug("found " + a.size() + " QueryStr: " + query);
          }
          catch (SQLException se) {
              DBConnectionFactory.closeStatement(stmt);
              DBConnectionFactory.releaseConnection(con);
            throw new FinderException(ErrorCodeConvert.getErrorCode(Integer.toString(se.getErrorCode())));
          }
          finally{
              DBConnectionFactory.closeStatement(stmt);
              DBConnectionFactory.releaseConnection(con);
          }
          return a;
        }
     
    }