<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<%
Connection con=null;
PreparedStatement st =null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=pubs;user=sa;password=";
con = DriverManager.getConnection(url);
String sql="insert into authors (au_id,au_lname,au_fname,phone,contract) values (?,?,?,?,?)";
st=con.prepareStatement(sql);
con.setAutoCommit(false); //关闭JDBC的自动失误提交功能
st.setString(1,"333-33-4455");
st.setString(2,"Tomcat");
st.setString(3,"Apache");
st.setString(4,"111 222-3333");
st.setInt(5,1);
st.executeUpdate();
st.setString(1,"2A2-33-4455");
st.setString(2,"Rose");
st.setString(3,"Bill");
st.setString(4,"333 222-3333");
st.setInt(5,1);
st.executeUpdate();
con.commit(); //提交事务
out.println("成功加入记录,请用查询分析器验证");
}
catch(Exception e)
{
out.println("出错:"+e);
out.println("<br>回滚全部事务");
con.rollback(); //回滚所有的事务
}
finally
{
if(st != null )
{st.close();}
if(con != null )
{
con.setAutoCommit(true);
con.close();
}
}
%>
</body>
</html>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<%@page import="javax.sql.*"%>
<body>
<%
Connection con=null;
PreparedStatement st =null;
int counter=0; //定义一个计数器 用于识别应该会滚到哪一个保存点
Savepoint p=null,p0=null,p1=null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=pubs;user=sa;password=";
con = DriverManager.getConnection(url);
String sql="insert into authors (au_id,au_lname,au_fname,phone,contract) values (?,?,?,?,?)";
st=con.prepareStatement(sql);
con.setAutoCommit(false);
p0=con.setSavepoint("1"); //保存断点
counter=1; //将断点标记为1
st.setString(1,"333-33-4455");
st.setString(2,"Tomcat");
st.setString(3,"Apache");
st.setString(4,"111 222-3333");
st.setInt(5,1);
st.executeUpdate();
p1=con.setSavepoint("2");
counter=2;
st.setString(1,"2A2-33-4455");
st.setString(2,"Rose");
st.setString(3,"Bill");
st.setString(4,"333 222-3333");
st.setInt(5,1);
st.executeUpdate();
con.commit();
out.println("成功加入记录,请用查询分析器验证");
}
catch(SQLException e)
{
out.println("出错:"+e);
out.println("<br>回滚部分事务,请用查询分析器验证");
switch(counter) //根据标示识别断点
{
case 1:
con.rollback(p0);
break;
case 2: con.rollback(p1);
break;
default:con.rollback();
}
}
finally
{
if(st != null )
{st.close();}
if(con != null )
{
con.setAutoCommit(true);
con.close();
}
}
%>
</body>
</html>
原文地址:http://blog.csdn.net/xlgen157387/article/details/39431317