数据向数据库中重复插入两次

编写博客系统的时候,用户添加博文,单击“提交”按钮后,数据库中插入了两条同样的数据,如下图:

 

涉及的代码如下:

 

代码
1 int result = 0;
2 try {
3 Connection conn = ds.getConnection();
4 String insertBlog = "insert into blog(title,content,category_id,createdtime) values(?,?,?,now())";
5 PreparedStatement pstmt = conn.prepareStatement(insertBlog);
6 pstmt.setString(1, title);
7 pstmt.setString(2, content);
8 pstmt.setInt(3, Integer.parseInt(categoryId));
9 result = pstmt.executeUpdate();
10 pstmt.executeUpdate();
11 } catch (SQLException e) {
12 e.printStackTrace();
13 }

检查出错原因,以上代码中第9行和第10行都执行了executeUpdate()方法,即向数据库提交了两次同样的命令。

删除第10行代码即可。

 

******************************************************************************************

posted @ 2010-10-03 10:48  Mr.chenz  阅读(850)  评论(0编辑  收藏  举报