📄 usecommitrollback.jsp
字号:
<%-- useCommitRollback.jsp程序代码 --%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.SQLException" %>
<%
Connection conn=null;
Statement stmt=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:datasource_suit","sa","710318");
conn.setAutoCommit(false); //禁止自动提交事务
stmt=conn.createStatement();
//构造第1条SQL插入语句
String strSQL1="insert into t_userInfo";
strSQL1+="(username,password,realname,sex,age,region,email,education,marriage,regdate) ";
strSQL1+="values('ly','12345678','翻译的人',0,'22-35岁','武汉','where@555.com','本科',0,'2004/05/05')";
//构造第2条SQL插入语句
String strSQL2="insert into t_userInfo";
strSQL2+="(username,password,realname,sex,age,region,email,education,marriage,regdate) ";
strSQL2+="values('ln','12345678','吴学究',0,'22-35岁','北京','ln@555.com','硕士',0,'2004/01/017')";
//构造第3条SQL修改语句
String strSQL3="update t_userInfo set password='key12345' where username='ly'";
//执行以上3条SQL语句
stmt.executeUpdate(strSQL1);
stmt.executeUpdate(strSQL2);
stmt.executeUpdate(strSQL3);
//提交事务
conn.commit();
out.println("数据更新成功");
}catch(Exception e){
out.println("更新失败,数据被回滚.");
try{
conn.rollback(); //回滚数据
}catch(Exception e2){
out.println("数据回滚失败,请检查数据库。");
}
}finally{
try{
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}catch(SQLException sqle){}
}
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -