这里,我有一个工具类HibernateUtil,里面有一个方法public static void executeUpdate(String hql,String[] parameters){
Session s=null;
Transaction ts=null;

try {
s=openSession();
ts=s.beginTransaction();
Query query=s.createQuery(hql);

if(parameters!=null && parameters.length>0){
for(int i=0;i<parameters.length;i++){
query.setString(i,parameters[i] );
}
}
query.executeUpdate();
ts.commit();

} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally{

if(s!=null && s.isOpen()){
s.close();
}
}

}
那么我在main函数里怎么调用呢?
目标是更改Student表中的,计算机系学生的年龄全加1;

解决方案 »

  1.   

    非高手,路过插个嘴,楼主,如果你是想测试这个工具类能不能使用的话,建议你使用junit来进行测试,还有,你这个现在不能再main中调用吗?有什么问题么?
      

  2.   

    你是要问hql和parameters这两个参数需要传什么值么?
      

  3.   

    public static void mian(String[] args) {
        executeUpdate(hql,parameters);
    }
      

  4.   


    再写上对应的hql,应该就可以了吧。不过,要确保数据库连接那方面没问题。
      

  5.   

    由于executeUpdate()是static的,所以在main方法中可以直接使用,
    public static void mian(String[] args) {
         executeUpdate(hql,parameters);
     }
    不过至于传入的参数parameters是个数组,一般都是传如要修改的对象,修改一下executeUpdate()吧。