transaction1.jsp

来自「JSP网络编程」· JSP 代码 · 共 44 行

JSP
44
字号
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<head>
<title>事务处理程序</title>
</head>
<body>
<center>
<table border="1" width="700">
  <tr>
    <td width="100%" colspan="2" align="center">事务处理程序</td>
  </tr>
  <tr>
    <td width="100%" colspan="2">
<%//事务处理
 String sqlString1="update userTable set user_age=30 where user_true_name like '邓%'";
 String sqlString2="update userTable set user_age=40 where user_true_name like '张%'";
 Connection con;
 Statement sql;
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
 con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDatabse","sa","6599996");
 try 
   {  //设置不会自动提交
      con.setAutoCommit(false);
      sql=con.createStatement();
      sql.executeUpdate(sqlString1);
      sql.executeUpdate(sqlString2);
      //提交事务
      con.commit();
      out.print("事务提交成功,执行两条SQL语句。");
      con.close();
    }
 catch(SQLException e) 
    { //操作不成功,回滚事务
      con.rollback();
      out.print("事务操作不成功,事务已回滚!");
    }
%>
    </td>
  </tr>
  </table>
</center>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?