手动提交模式,怎样用一个preparedStatement往两个结构不同的表里写大量记录?

krack2002 2003-12-12 01:16:03
因为在手动提交模式,总不允许我定义多个preparedStatement。
...全文
146 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
web_spider 2003-12-12
  • 打赏
  • 举报
回复
一个连接只能同时有一个事务,想用事务的化,可能要用不同的连接(并行但浪费);
要不就串行处理。
我给的代码是自动提交的,不带事务
krack2002 2003-12-12
  • 打赏
  • 举报
回复
to CoolAbu(绿茶的白开水(★★★★)) :
(1)为了事务处理
(2)connection.commit()有
CoolAbu 2003-12-12
  • 打赏
  • 举报
回复
为什么要用手动模式?

最后你有connection.commit()吗?
krack2002 2003-12-12
  • 打赏
  • 举报
回复
to web_spider:在手动模式(connection.setAutoCommit(false))时,定义多个preparedStatement,Tomcat4.1.18出错:
[Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned
connection while in manual transaction mode.
bobZ 2003-12-12
  • 打赏
  • 举报
回复
up
web_spider 2003-12-12
  • 打赏
  • 举报
回复
while循环中应该有类似于下面的语句(添加语句到批):
batchEmps.setInt(1, activityID);
batchEmps.addBatch();
web_spider 2003-12-12
  • 打赏
  • 举报
回复
肯定要定义多个preparedStatement。并且要用batch的方式来插入数据
下面是一段相关的代码:
PreparedStatement batchEmps = null;
PreparedStatement batchProdScrap = null;
PreparedStatement batchCutDownScrap = null;
try {

batchEmps = conn.prepareStatement(sqlEmps);
batchProdScrap = conn.prepareStatement(sqlProdScrap);
batchCutDownScrap = conn.prepareStatement(sqlCutDownScrap);
while(){
//根据需要加入数据
............................
}

batchEmps.executeBatch();
batchProdScrap.executeBatch();
batchCutDownScrap.executeBatch();
}catch (SQLException ex2) {
println("catch:", ex2);
isOK = false;
}
finally {
try {
if (batchEmps != null) {
batchEmps.close();
}
if (batchProdScrap != null) {
batchProdScrap.close();
}
if (batchCutDownScrap != null) {
batchCutDownScrap.close();
}
}
catch (SQLException ex3) {
println("fianlly:", ex3);
}
return isOK;
}

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧