// 批量处理:JDBC
@org.junit.Test
public void jdbcBatch(){
session=HibernateUtil.getSession();
tx=session.beginTransaction();
Work work=new Work(){
//匿名内部类、Work实现类
@Override
public void execute(Connection conn) throws SQLException {
sql="update Emp set sal=sal+2";
PreparedStatement pst=conn.prepareStatement(sql);
pst.executeUpdate();
}
};
session.doWork(work);
tx.commit();
HibernateUtil.closeSession();
}
**// 批量处理:HQL**
public void hqlBatch() {
session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
hql = "update Emp set comm=50 where comm=0";
Query query = session.createQuery(hql);
int num = query.executeUpdate();
System.out.println(num);
tx.commit();
HibernateUtil.closeSession();
}

本文介绍使用JDBC和HQL进行批量数据更新的方法。通过示例代码展示了如何利用匿名内部类和预编译语句进行数据库批量操作,以及如何运用HQL执行批量更新任务。

1万+

被折叠的 条评论
为什么被折叠?



