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);这种方法删除,但是不行啊?有错,谁能告诉我哪错了?
解决方案 »
- 如何在一个页面不跳转的情况下请求多个SpringMVC的Controller,就像S标签的action实现的那样
- hibernate中日志的问题
- spring中使用quartz任务时,jobDataAsMap问题。
- 多条json格式的字符串类型转换成java对象有问题
- 求大家帮帮忙。看看这个表单怎么样模拟提交(跪求啦!!!)
- 请问Hibernate中这两条语句是否相等???
- java.lang.NumberFormatException: For input string: "201005040119"
- javabean 的getter setter的用法 - 为什么我用set传值传不过去呢 大家请看 代码如下
- Service中使用java反射的问题
- 天津有哪些公司是专搞java的,想了解一下,有知道的帮忙给列一下,谢了!!
- 数据库图片显示到页面
- 紧求
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 看看
String sql="insert into errbills values()";
sm.addBatch(sql);
sql="delete from errbills where bno='124'";
sm.addBatch(sql);
sm.executeBatch();二、使用PreparedStatementString 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是不带参数的
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();
}
}
}
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();
}
}
}