public boolean insertOneCommentForOneArticleTestBatch(Comment comment,String author){
boolean flag=true;
DataBaseConnection dbc;
Connection con=null;
PreparedStatement pstmt;
dbc=new DataBaseConnection();
con=dbc.getConnection();
/******特别是这儿开始的几句 ***********************/ String sql="insert into comment(pdate,comment) value(now(),?)"; String sql2="update title set 评论数目=评论数目+1 where author=? ";
try { pstmt=con.prepareStatement(sql);
pstmt.setString(1, comment.getComment());
pstmt.executeUpdate(); pstmt=con.prepareStatement(sql2);
pstmt.setString(1, author);
pstmt.executeUpdate();
pstmt.close();
/*********************************************************************/
} catch (SQLException e) {
e.printStackTrace();
System.out.println("insertOneCommentForOneArticle(Comment comment)出错");
}finally{
dbc.close();
//实际上关的是con,
}
return flag;
}
public static void main(String agrs[]){
Comment comment=new Comment();
comment.setComment("TestBatch");
CommentDao cd=new CommentDao();
cd.insertOneCommentForOneArticleTestBatch(comment,"f");
}
boolean flag=true;
DataBaseConnection dbc;
Connection con=null;
PreparedStatement pstmt;
dbc=new DataBaseConnection();
con=dbc.getConnection();
/******特别是这儿开始的几句 ***********************/ String sql="insert into comment(pdate,comment) value(now(),?)"; String sql2="update title set 评论数目=评论数目+1 where author=? ";
try { pstmt=con.prepareStatement(sql);
pstmt.setString(1, comment.getComment());
pstmt.executeUpdate(); pstmt=con.prepareStatement(sql2);
pstmt.setString(1, author);
pstmt.executeUpdate();
pstmt.close();
/*********************************************************************/
} catch (SQLException e) {
e.printStackTrace();
System.out.println("insertOneCommentForOneArticle(Comment comment)出错");
}finally{
dbc.close();
//实际上关的是con,
}
return flag;
}
public static void main(String agrs[]){
Comment comment=new Comment();
comment.setComment("TestBatch");
CommentDao cd=new CommentDao();
cd.insertOneCommentForOneArticleTestBatch(comment,"f");
}
解决方案 »
- 这样的有嵌套查询SQL使用HQL该如何表达
- 我自己写了个快速排序法,但是没排成功,大家帮忙看看哪里错了?
- java servlet 分页功能
- 求救,jdbc查询时报异常~急!!!
- servlet与servlet之间如何相互调用?
- hibernate 用SQLQuery 调用存储过程的参数问题
- 可以实现这样的功能吗?写一触发期,无限期监控一个表的数据,如果某条记录的插入时间和当前时间大于几小时就把该记录插入另一表~~谢谢大
- 一个对象中包含BufferedReader成员,是不是就不能在Socket通讯中传递这个对象了?
- 郁闷一周的问题了!(关于webServices)
- struts 中WEB.XML 和CONFIG.XML的区别
- struts-config 数据源配置问题
- struts1 的 actionform 为null问题
如果需要事务控制,在前面加上conn.setAutoCommit(false);
后面conn.commit();
catch 里面 加上 conn.rollback();
finally 里面加上 conn.setAutoCommit(true);
PreparedStatement pstmt=null这里不设置初始值的话pstmt.close()应该会编译不过去,直接出错吧。
可是我认为根据executeUpdate()的字面意思,在第一句pstmt.executeUpdate();执行的时候,就已经执行前面准备好的一句pstmt=con.prepareStatement(sql)了啊,这时setAutoCommit(FALSE)还有意义么?我也知道,这里的PreparedStatement是可以改成我已经理解了的Statement的,所以,我认为要setAutoCommit(FALSE)产生作用,就只能用Statement的addBatch方法进行批处理,大家说说看,对么,