http://www.ads4cn.com/newsbar/refferer.asp?elflovebobo>看新闻,赚现金!无须点击广告,不必投入资金!

解决方案 »

  1.   

    这段是批量更新的sample:
    调用update方法就可以了,下面是update里使用到的queryActionByFind和updateAction方法,这样更新一个和多个都可以了,加分吧朋友!!!
    public void update(){
    String[] columnNames=EmployeeDetailTable.getColumnNames();
    Object[] values={"null","a00X","null","null","null","null","null","null","null","null"};
    Hashtable msgDetails=new Hashtable();
    for(int i=0;i<columnNames.length;i++){
    msgDetails.put(columnNames[i],values[i]);
    }
    Iterator it=EmployeeDetailTable.queryActionByFind(
    "from EmployeeDetail e where e.employeeid='a000'");
    EmployeeDetailTable.updateAction(it,msgDetails);
    }public static Iterator queryActionByFind(String HQL){
    Session session=HibernateUtility.currentSession();
    Iterator i=null;
    try {
    i = session.find(HQL).iterator();
    } catch (HibernateException e) {
    e.printStackTrace();
    }
    return i;
    }public static void updateAction(Iterator it,Hashtable msgDetails){
    EmployeeDetail employeeDetail=null;
    Session session=HibernateUtility.currentSession();
    String[] columnNames=getColumnNames();
    while(it.hasNext()){
    employeeDetail=(EmployeeDetail)it.next();
    for(int i=0;i<columnNames.length;i++){
    //Hashtable中不支持null,只能用"null"代替
    if(!msgDetails.get(columnNames[i]).toString().equals("null")){
    switch(i){
    case 0:
    employeeDetail.setSysid((Integer)msgDetails.get(columnNames[i]));
    break;
    case 1:
    employeeDetail.setEmployeeid((String)msgDetails.get(columnNames[i]));
    break;
    case 2:
    employeeDetail.setName((String)msgDetails.get(columnNames[i]));
    break;
    case 3:
    employeeDetail.setGender((String)msgDetails.get(columnNames[i]));
    break;
    case 4:
    employeeDetail.setAge((Integer)msgDetails.get(columnNames[i]));
    break;
    case 5:
    employeeDetail.setAddress((String)msgDetails.get(columnNames[i]));
    break;
    case 6:
    employeeDetail.setZipcode((String)msgDetails.get(columnNames[i]));
    break;
    case 7:
    employeeDetail.setMobile((String)msgDetails.get(columnNames[i]));
    break;
    case 8:
    employeeDetail.setUnitphone((String)msgDetails.get(columnNames[i]));
    break;
    case 9:
    employeeDetail.setEmail((String)msgDetails.get(columnNames[i]));
    break;
    default:
    }
    }
    }
    }
    try {
    session.flush();
    session.evict(employeeDetail);
    HibernateUtility.closeSession();
    } catch (HibernateException e) {
    e.printStackTrace();
    }
    }
      

  2.   

    补充一下上面的EmployeeDetailTable.getColumnNames()方法是从自己写的xml文件里得到employeedetail表的列名,省得自己一个个去写了,xml原型如下,当然你也可以用final static String[]定死
    <?xml version="1.0" encoding="UTF-8"?>
    <Table name="EmployeeDetail">
    <Column name="sysid" type="INT" isNull="NOT NULL"/>
    <Column name="employeeid" type="CHAR(20)" isNull="NOT NULL"/>
    <Column name="name" type="CHAR(20)" isNull="NOT NULL"/>
    <Column name="gender" type="CHAR(1)" isNull="NOT NULL">
    <ValueOptional>M</ValueOptional>
    <ValueOptional>F</ValueOptional>
    </Column>
    <Column name="age" type="SMALLINT" isNull="NOT NULL"/>
    <Column name="address" type="CHAR(100)" isNull="NULL"/>
    <Column name="zipcode" type="CHAR(20)" isNull="NULL"/>
    <Column name="mobile" type="CHAR(20)" isNull="NULL"/>
    <Column name="unitphone" type="CHAR(20)" isNull="NULL"/>
    <Column name="email" type="CHAR(50)" isNull="NULL"/>
    </Table>