<1>.关闭Connection的自动提交
connection.setAutoCommit(false);
<2>.执行一系列sql语句:执行新sql前,以前的Statement(或PreparedStatemet)须close
Statement sm ;
sm = cn.createStatement(insert into user...);
sm.executeUpdate();
sm.close();
sm = cn.createStatement("insert into corp...);
sm.executeUpdate();
sm.close();
<3>.提交
cn.commit();
<4>.如果发生异常,回滚:
cn.rollback();
5.处理执行结果:
<2>.更新语句,返回数字,表示该更新影响的记录数(0,表示未更新,-1表示更新失败)
<3>.ResultSet的方法:while(re.next())
next(),将游标往后移动一行,如果成功返回true;否则返回false
getInt("id")或getSting("name"),返回当前游标下某个字段的值
6.关闭数据库连接
rs.close();
ps.close(); /stat.close();
con.close();
本文涉及到了JDBC的事务处理,本人觉得WEB开发中,事务处理是一个很重要的概念,请参看本人博文: Java事务处理浅析.
本文出自 “坐看云起时” 博客