JDBC的批量操作Batch
2011-10-19 17:33
204 查看
语句队列,用链表存储是为了保证先到的语句能先执行
* 加到当前批次
*
* @param conn
* 数据库连接
* @param sql
* sql语句
* @param callback
* 绑定变量回调接口
* @throws SinoException
*/
public void addToBatch(Connection conn, String sql, Callback callback) throws SinoException {
PreparedStatement ps = null;
try {
if (currentSql != null && sql.hashCode() == currentSql.hashCode()
&& sql.length() == currentSql.length()) {
ps = (PreparedStatement) statementList.getLast();
} else {
conn = SysUtility.check(conn);
ps = SQLExecUtils.getProxy(conn.prepareStatement(sql));
statementList.addLast(ps);
currentSql = sql;
}
callback.doIn(ps);
ps.addBatch();
} catch (SQLException e) {
throw SinoException.getSinoException(e);
}
LogUtil.printLog("add to batch :" + sql, LogUtil.DEBUG);
size++;
}
public boolean executeBatch() throws SinoException {
if (isEmpty()) {
return true;
}
long start = System.currentTimeMillis();
try {
for (int i = 0; i < statementList.size(); i++) {
PreparedStatement ps = (PreparedStatement)statementList.get(i);
ps.executeBatch();
isNullUpdated(ps.getUpdateCount());
}
} catch (SQLException e) {
throw SinoException.getSinoException(e);
} finally {
clear();
}
LogUtil.printLog("executeBatch cost " + (System.currentTimeMillis() - start) + " ms!",
LogUtil.DEBUG);
return isNullUpdated();
}
* 加到当前批次
*
* @param conn
* 数据库连接
* @param sql
* sql语句
* @param callback
* 绑定变量回调接口
* @throws SinoException
*/
public void addToBatch(Connection conn, String sql, Callback callback) throws SinoException {
PreparedStatement ps = null;
try {
if (currentSql != null && sql.hashCode() == currentSql.hashCode()
&& sql.length() == currentSql.length()) {
ps = (PreparedStatement) statementList.getLast();
} else {
conn = SysUtility.check(conn);
ps = SQLExecUtils.getProxy(conn.prepareStatement(sql));
statementList.addLast(ps);
currentSql = sql;
}
callback.doIn(ps);
ps.addBatch();
} catch (SQLException e) {
throw SinoException.getSinoException(e);
}
LogUtil.printLog("add to batch :" + sql, LogUtil.DEBUG);
size++;
}
public boolean executeBatch() throws SinoException {
if (isEmpty()) {
return true;
}
long start = System.currentTimeMillis();
try {
for (int i = 0; i < statementList.size(); i++) {
PreparedStatement ps = (PreparedStatement)statementList.get(i);
ps.executeBatch();
isNullUpdated(ps.getUpdateCount());
}
} catch (SQLException e) {
throw SinoException.getSinoException(e);
} finally {
clear();
}
LogUtil.printLog("executeBatch cost " + (System.currentTimeMillis() - start) + " ms!",
LogUtil.DEBUG);
return isNullUpdated();
}
相关文章推荐
- hibernate 批量操作 hibernate.jdbc.batch_size hibernate.jdbc.fetch_size
- JDBC-批量操作的行数限制
- 用友nc65 uap开发 nc.vo.bd.meta.BatchOperateVO(批量操作的包装VO)转aggvo
- Java JDBC 进行批量跟新操作
- 聊聊jdbc的batch操作
- oracle事务处理及实例演示jdbc操作批量删除
- 使用管道(PipeLine)和批量(Batch)操作
- 【Hibernate框架】批量操作Batch总结
- 【Hibernate框架】批量操作Batch总结
- JDBC的批量插入操作
- 初学JDBC,获取插入记录的主键、执行批量操作
- jdbc批量执行SQL insert 操作
- springmvc封装jdbc的批量更新BatchPreparedStatementSetter问题
- jdbc的批量操作使用和注意事项
- Hibernate 抓取策略fetch-2 (批量抓取batch-size以及hibernate.jdbc.fetch_size、hibernate.jdbc.batch_size)
- JDBC 批量操作
- Spring4-JdbcDaoSupport-insertBatch-执行批量添加操作
- JDBC的批量插入操作
- Spring 对JDBC操作(实现增删改查,批量添加功能)
- Spring JDBC Framework详解——批量JDBC操作、ORM映射