⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userspersistentbase.java

📁 OA典型例子
💻 JAVA
字号:

/*
 * UsersPersistentBase.java 
 * Generated using xgen and texen from beanpersistentbase.vm
 * Wed Feb 25 10:47:47 CST 2004
 */

package com.sure.oa.orgnization;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DatabaseMetaData;
import java.sql.BatchUpdateException;
import java.math.BigDecimal;
import com.sure.businessmodel.Page;
import java.io.*;
import java.util.*;
import com.sure.util.database.*;

/**
 * 存取人员的属性
 * @author datonghuiren@sohu.com
 */
public abstract class UsersPersistentBase {

  /** class attributes */
  transient protected boolean recordExists = false;
  transient protected boolean handleConcurrency = false;
  transient protected boolean handleAutoIncrement = true;
  protected Users bean;
  
  public UsersPersistentBase(Users bean) { setBean(bean); }
  public UsersPersistentBase(Users bean, boolean recordExists) { setBean(bean); setRecordExists(recordExists); }
  
  
  public Users getBean() { return bean; }
  public void setBean(Users bean) { this.bean = bean; }
  public boolean getRecordExists() { return recordExists; }
  public void setRecordExists(boolean recordExists) { this.recordExists = recordExists; }

  /** 
   * Load this object from a result set 
   * If null fields are present in the table, catch the NullPointerException
   */
  public static Users load(ResultSet rs) throws SQLException {
    Users theBean = new Users ();
    try{ theBean.setUserId ( rs.getInt("userId") ); } catch(NullPointerException e){}
    try{ theBean.setUserName ( rs.getString("userName") ); } catch(NullPointerException e){}
    try{ theBean.setAccount ( rs.getString("account") ); } catch(NullPointerException e){}
    try{ theBean.setPassword ( rs.getString("password") ); } catch(NullPointerException e){}
    try{ theBean.setUnitId ( rs.getString("unitId") ); } catch(NullPointerException e){}
    try{ theBean.setDeptId ( rs.getString("deptId") ); } catch(NullPointerException e){}
    try{ theBean.setCertiFicate ( rs.getString("certiFicate") ); } catch(NullPointerException e){}
    try{ theBean.setUnDefine ( rs.getString("unDefine") ); } catch(NullPointerException e){}
    try{ theBean.setPosition ( rs.getString("position") ); } catch(NullPointerException e){}
    try{ theBean.setSex ( rs.getString("sex") ); } catch(NullPointerException e){}
    try{ theBean.setOfficeTel ( rs.getString("officeTel") ); } catch(NullPointerException e){}
    try{ theBean.setHomeTel ( rs.getString("homeTel") ); } catch(NullPointerException e){}
    try{ theBean.setMobile ( rs.getString("mobile") ); } catch(NullPointerException e){}
    try{ theBean.setEmail ( rs.getString("email") ); } catch(NullPointerException e){}
    try{ theBean.setBirthday ( rs.getTimestamp("birthday") ); } catch(NullPointerException e){}
    try{ theBean.setStatus ( rs.getInt("status") ); } catch(NullPointerException e){}
    try{ theBean.setMemo ( rs.getString("memo") ); } catch(NullPointerException e){}
    return theBean;
  }

  /** 
   * Load a range of records from a connection, according to the where clause.
   * Returns a map of records accessible by a column value. (ie: foreign key)
   */
  public static Map loadByKey(Connection conn, String where, String colname) 
    throws SQLException{
  
    HashMap map = new HashMap();
    Statement s = conn.createStatement();
    String sql = "SELECT userId,userName,account,password,unitId,deptId,certiFicate,unDefine,position,sex,officeTel,homeTel,mobile,email,birthday,status,memo FROM Users " + where;
    ResultSet rs = s.executeQuery( sql );
    while(rs.next()){
      Users theBean = new Users ();
      int col = rs.findColumn(colname);
      String key = rs.getString(col);
      theBean = load(rs);
      map.put(key, theBean);
    }
    rs.close();
    s.close();
    return map;
  }

  /** 
   * Load a range of records from a connection, according to the where clause 
   */
  public static Vector load(Connection conn, String where) throws SQLException{
    Vector beans = new Vector();
    Statement s = conn.createStatement();
    String sql = "SELECT userId,userName,account,password,unitId,deptId,certiFicate,unDefine,position,sex,officeTel,homeTel,mobile,email,birthday,status,memo FROM Users " + where;
    ResultSet rs = s.executeQuery( sql );
    while(rs.next()){
      Users theBean = new Users ();
      theBean = load(rs);
      beans.addElement(theBean);
    }
    rs.close();
    s.close();
    return beans;
  }

  /**
   * Load a range of records using a scrollable resultset and absolute cursor
   */
  public static Vector load(Connection conn, String where,
      int startRow, int rowCount) throws SQLException {
    Vector beans = new Vector();

    String sql = "SELECT userId,userName,account,password,unitId,deptId,certiFicate,unDefine,position,sex,officeTel,homeTel,mobile,email,birthday,status,memo FROM Users ";
    if(where.length() > 0)
      sql = sql + where;
    Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                       ResultSet.CONCUR_READ_ONLY);
    s.setMaxRows(startRow + rowCount - 1);
    ResultSet rs = s.executeQuery( sql );
    rs.absolute(startRow);
    
    do {
      Users theBean = new Users ();
      theBean = load(rs);
      beans.addElement(theBean);
    } while(rs.next());
    rs.close();
    s.close();
    return beans;
  }

  /**
   * Load a Page of records using a scrollable resultset and absolute cursor
   */
  public static Page load(Connection conn, int startRow, int pageSize,
       String where) throws SQLException {
      
    int count = getCount(conn, where);
    if (count == 0) return new Page(pageSize);
    startRow = Page.getValidStart(startRow, count, pageSize);
    Vector beans = load(conn, where, startRow, pageSize);
    return new Page(beans, startRow, count, pageSize);
  }

  /** 
   * Persist the record. Performs a sql update or insert.
   * The primary key will be "set" upon insert.
   * If autocommit is on, turn it off to make a transactional
   * check of the tcn field.
   */
  public void persist(Connection conn) throws SQLException{
    if(recordExists){
      if(handleConcurrency){
        //regenerate source if you want the bean to handle concurrency
      }else{
        update(conn);
      }
    }else{
      if(handleAutoIncrement){
        setPrimaryKey(conn);
      }
      insert(conn);
    }
  }



  /** 
    * Set the primary key field using the MAX value of the column 
    * If the primary key is a string, check for existence and return
    * If the primary key is already set (in a new bean its 0) return
    */
    public void setPrimaryKey(Connection conn) throws SQLException{
            try{
      IDGeneratorManager idGeneratorManager = new IDGeneratorManager();
	  bean.setUserId( idGeneratorManager.setNextValue("Users"));
    }
    catch(SQLException se){
      se.printStackTrace();
    }
    }

  
  /** 
    * Insert a new record 
    */
  public void insert(Connection conn) throws SQLException {
    PreparedStatement pstmt = null;
    pstmt = conn.prepareStatement("INSERT INTO Users ( userId,userName,account,password,unitId,deptId,certiFicate,unDefine,position,sex,officeTel,homeTel,mobile,email,birthday,status,memo ) VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )");
    pstmt.setInt ( 1, bean.getUserId().intValue() );
    pstmt.setString ( 2, bean.getUserName() );
    pstmt.setString ( 3, bean.getAccount() );
    pstmt.setString ( 4, bean.getPassword() );
    pstmt.setString ( 5, bean.getUnitId() );
    pstmt.setString ( 6, bean.getDeptId() );
    pstmt.setString ( 7, bean.getCertiFicate() );
    pstmt.setString ( 8, bean.getUnDefine() );
    pstmt.setString ( 9, bean.getPosition() );
    pstmt.setString ( 10, bean.getSex() );
    pstmt.setString ( 11, bean.getOfficeTel() );
    pstmt.setString ( 12, bean.getHomeTel() );
    pstmt.setString ( 13, bean.getMobile() );
    pstmt.setString ( 14, bean.getEmail() );
    pstmt.setTimestamp ( 15, new java.sql.Timestamp ( bean.getBirthday ().getTime() ) );
    pstmt.setInt ( 16, bean.getStatus().intValue() );
    pstmt.setString ( 17, bean.getMemo() );
    pstmt.executeUpdate();
    pstmt.close();
    recordExists = true;
  }

  /** 
    * Update an existing record 
    * (including the primary key)
    */
  public void update(Connection conn) throws SQLException{
    PreparedStatement pstmt = null;
    pstmt = conn.prepareStatement("UPDATE Users SET " +
             "userId = ?," +
             "userName = ?," +
             "account = ?," +
             "password = ?," +
             "unitId = ?," +
             "deptId = ?," +
             "certiFicate = ?," +
             "unDefine = ?," +
             "position = ?," +
             "sex = ?," +
             "officeTel = ?," +
             "homeTel = ?," +
             "mobile = ?," +
             "email = ?," +
             "birthday = ?," +
             "status = ?," +
             "memo = ? " +	
             "WHERE userId = ?" );
    pstmt.setInt ( 1, bean.getUserId ().intValue() );
    pstmt.setString ( 2, bean.getUserName () );
    pstmt.setString ( 3, bean.getAccount () );
    pstmt.setString ( 4, bean.getPassword () );
    pstmt.setString ( 5, bean.getUnitId () );
    pstmt.setString ( 6, bean.getDeptId () );
    pstmt.setString ( 7, bean.getCertiFicate () );
    pstmt.setString ( 8, bean.getUnDefine () );
    pstmt.setString ( 9, bean.getPosition () );
    pstmt.setString ( 10, bean.getSex () );
    pstmt.setString ( 11, bean.getOfficeTel () );
    pstmt.setString ( 12, bean.getHomeTel () );
    pstmt.setString ( 13, bean.getMobile () );
    pstmt.setString ( 14, bean.getEmail () );
    pstmt.setTimestamp ( 15, new java.sql.Timestamp ( bean.getBirthday ().getTime() ) );
    pstmt.setInt ( 16, bean.getStatus ().intValue() );
    pstmt.setString ( 17, bean.getMemo () );
    pstmt.setInt ( 18, bean.getUserId ().intValue() );
    pstmt.executeUpdate();
    pstmt.close();
  }


  /** 
    * Delete an existing record 
    */
  public static void delete(Connection conn, String where) throws SQLException{
    Statement stmt = conn.createStatement();
    String sql = "DELETE FROM Users " + where;
    stmt.execute(sql);
    stmt.close();
  }

  /** 
    * Delete an existing record using a prepared statement
    */
  public void delete(Connection conn) throws SQLException{
    PreparedStatement pstmt = null;
    pstmt = conn.prepareStatement("DELETE FROM Users WHERE userId = ?");
    pstmt.setInt( 1, bean.getUserId ().intValue() );
    pstmt.executeUpdate();
    pstmt.close();
  }
 
  /** 
    * get total count of a range of records from a connection, according to the where clause 
    */
  protected static int getCount(Connection conn, String where) throws SQLException{
    Statement s = conn.createStatement();
    
    String sql = "SELECT count(userId) FROM Users ";
    if(where.length() > 0) {
      where = where.toLowerCase();
      if (where.indexOf("order") > 0) {
	where = where.substring(0,where.lastIndexOf("order"));
      }
      sql = sql + where;
    }	

    ResultSet rs = s.executeQuery( sql );
    
    int count = 0;
    if( rs.next()){
      count = rs.getInt(1);
    }
    rs.close();
    s.close();
    return count;
  }
 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -