日期:2014-05-16 浏览次数:20463 次
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <%@ page import="java.sql.*,java.util.*,java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <title>JSP 事务的操作</title> </head> <body> <% Connection conn=null; Statement stmt=null; ResultSet rs=null; java.util.Date date=new java.util.Date(); out.println(date.getTime()); out.println("<br/>"); try{ conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","zhang","zhang"); conn.setAutoCommit(false);////禁止自动提交,设置回滚点 stmt=conn.createStatement(); String sql="select * from ins order by createtime desc"; stmt.execute(sql); String sqll="update ins set mobile='80080008888' where id>322"; stmt.executeUpdate(sqll);//数据库更新操作 conn.commit(); rs=stmt.executeQuery(sql); while(rs.next()){ out.println(rs.getInt("id")+":"); out.println(rs.getString("createtime")+":"); out.println(rs.getString("username")+":"); out.println(rs.getString("mobile")+":"); out.println("<br/>"); } }catch(Exception e){ e.printStackTrace(); try{ conn.rollback(); //操作不成功则回滚 }catch(Exception e1){ e1.printStackTrace(); } } %> </body> </html>
try { conn = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:SID","username","userpwd"; conn.setAutoCommit(false);//禁止自动提交,设置回滚点 stmt = conn.createStatement(); stmt.executeUpdate(“alter table …”); //数据库更新操作1 stmt.executeUpdate(“insert into table …”); //数据库更新操作2 conn.commit(); //事务提交 }catch(Exception ex) { ex.printStackTrace(); try { conn.rollback(); //操作不成功则回滚 }catch(Exception e) { e.printStackTrace(); } }