jdbc的addBatch问题,快来帮忙啊,200分

huosidun0302 2010-06-29 11:17:24

public void deleteCourseInfo(String courseId) throws Exception {
Connection con = pStrategy.getDBConnection();
con.setAutoCommit(false);
try {
Statement stmt = con.createStatement();
Map tb = AllCourseInfoTable.getTable();
Iterator it = tb.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
String tableName = (String)entry.getKey();
String propertyName = (String)entry.getValue();
String sql = "delete from " + tableName + " where " + propertyName + " = " + courseId;
con.prepareStatement(sql);
stmt.addBatch(sql);
}
stmt.executeBatch();
con.commit();
if (stmt != null) {
stmt.close();
stmt = null;
}
}catch(Exception e){
try {
con.rollback();
} catch (SQLException e1) {
throw new Exception();
}
throw new Exception();
}finally{
try {
con.close();
} catch (SQLException ex) {
throw new Exception();
}
}
}

这段代码我想完成批量删除的工作,就是delete from table where id = ?有N条这样的语句,我想用上面的stmt.addBatch(sql);这种方法删除,但是不行啊?有错,谁能告诉我哪错了?
...全文
802 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
nanyang421 2010-06-29
  • 打赏
  • 举报
回复
学习了
风过无痕1110 2010-06-29
  • 打赏
  • 举报
回复
注释掉一行:
public void deleteCourseInfo(String courseId) throws Exception {
Connection con = pStrategy.getDBConnection();
con.setAutoCommit(false);
try {
Statement stmt = con.createStatement();
Map tb = AllCourseInfoTable.getTable();
Iterator it = tb.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
String tableName = (String)entry.getKey();
String propertyName = (String)entry.getValue();
String sql = "delete from " + tableName + " where " + propertyName + " = " + courseId;
//con.prepareStatement(sql);
stmt.addBatch(sql);
}
stmt.executeBatch();
con.commit();
if (stmt != null) {
stmt.close();
stmt = null;
}
}catch(Exception e){
try {
con.rollback();
} catch (SQLException e1) {
throw new Exception();
}
throw new Exception();
}finally{
try {
con.close();
} catch (SQLException ex) {
throw new Exception();
}
}
}

道光2008 2010-06-29
  • 打赏
  • 举报
回复
有两种方法可以进行批处理 第一种是用Statement 另外一种是PreparedStatement

一、使用Statement



Statement sm=con.createStatement();
String sql="insert into errbills values()";
sm.addBatch(sql);
sql="delete from errbills where bno='124'";
sm.addBatch(sql);
sm.executeBatch();

二、使用PreparedStatement

String sql="delete from errblls where bno=?";
PreparedStatement ps=con.prepareStatement(sql);
for(int i=0;i<10;i++) {
ps.setString(i);
ps.addBatch();
}
ps.executeBatch();

Statement的addBatch(String s)是带参数的,PreparedStatement是不带参数的
qq330106852 2010-06-29
  • 打赏
  • 举报
回复
//批量删除分类对象
public void deleteBat(Integer[] catNo){
try {
Connection con=DBUtil.getInstance().getCon();
String sql="delete from cat where catno=?";
con.setAutoCommit(false);
PreparedStatement ps=con.prepareStatement(sql);
for (Integer in : catNo) {
ps.setInt(1, in);
ps.addBatch();
}
int[] result=ps.executeBatch();
con.commit();
for (int i : result) {
System.out.println(i);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}


把Statement改成prepareStatement 看看
Pbulic 2010-06-29
  • 打赏
  • 举报
回复
addBatch好像不是删除的吧
Pbulic 2010-06-29
  • 打赏
  • 举报
回复
异常贴贴
newLife_bj 2010-06-29
  • 打赏
  • 举报
回复
接分学习
BearKin 2010-06-29
  • 打赏
  • 举报
回复
。。。我咋感觉你用的方式有些不对啊。。 等俺回去翻书先。。
BearKin 2010-06-29
  • 打赏
  • 举报
回复
异常贴贴
nlwangxin 2010-06-29
  • 打赏
  • 举报
回复
//修改过的,试试看行不行
public void deleteCourseInfo(String courseId) throws Exception {
Connection con = pStrategy.getDBConnection();
con.setAutoCommit(false);
try {
Statement stmt = con.createStatement();
Map tb = AllCourseInfoTable.getTable();
Iterator it = tb.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
String tableName = (String)entry.getKey();
String propertyName = (String)entry.getValue();
String sql = "delete from " + tableName + " where " + propertyName + " = " + courseId;
stmt.addBatch(sql);
}
stmt.executeBatch();
con.commit();
if (stmt != null) {
stmt.close();
stmt = null;
}
}catch(Exception e){
try {
con.rollback();
} catch (SQLException e1) {
throw new Exception();
}
throw new Exception();
}finally{
try {
con.close();
} catch (SQLException ex) {
throw new Exception();
}
}
}
zw61911169 2010-06-29
  • 打赏
  • 举报
回复
你这一句con.prepareStatement(sql);要的有什么用啊。。。。
水中影子 2010-06-29
  • 打赏
  • 举报
回复
路过,学习,个人很少使用batch
往何 2010-06-29
  • 打赏
  • 举报
回复
mark

67,518

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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