[学习笔记]

7.jdbc的transaction例子:

import java.sql.*;

public class MySQlTransaction1 {

public static void main(String[] args) throws SQLException {
/*in my sql: create table Accounts(
                ID int(4) not null,
                NAME varchar(15),
                BALANCE int(4),
                primary key(ID)
            )  type=INNODB;
  insert into Accounts values(1,'wangwu',100);
  insert into Accounts values(3,'zhangsan',300);
  insert into Accounts values(4,'lisi',400);
     */
    Connection con = null;
    Statement s = null;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "1234");
      //s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
/*by default, whenever execute a sql, it will commit automatically,
public void setAutoCommit(boolean autoCommit) throws SQLException
Sets this connection's auto-commit mode to the given state. If a connection is in auto-commit 
mode, then all its SQL statements will be executed and committed as individual transactions. 
Otherwise, its SQL statements are grouped into transactions that are terminated by a call to
 either the method commit or the method rollback. By default, new connections are in
  auto-commit mode.        */

s = con.createStatement();

s.executeUpdate("update ACCOUNTS set BALANCE=508 where ID=3");
      System.out.println("333333");
/*下一步中本来应为where ID=4, 但是却误写成了www ID=4, 所以有错,所以到catch中,但rollback时
 , 却做不成功, 因为是autocommited模式,所以上一句ID=3,就做真改成508了。*/      
      s.executeUpdate("update ACCOUNTS set BALANCE=608 www ID=4");
      System.out.println("444444");

System.out.println("con = " + con);
     }
    catch (Exception e) {
      try{
        con.rollback();
        System.out.println("rollback successfully");
      }catch (Exception ex)
      {
        ex.printStackTrace();
      }
    }
    finally {
      s.close();
      con.close();
      System.out.println("successfully in finally");
    }
  }

}

文章转载自原文:https://blog.csdn.net/qq_43650923/article/details/100653000

最新文章

  1. 常见容易遗漏的html标签
  2. socket - socketserver - start TCP server
  3. Linux课程实践四:ELF文件格式分析
  4. 在GitHub上建立个人主页的方法
  5. 远程登录 - telnet、mstsc等工具的使用及遇到的问题
  6. 转!!JavaBean,List,Map转成json格式
  7. 酷我音乐API
  8. ionic pull to refresh 下拉更新頁面
  9. 到目前为止,Linux下最完整的Samba服务器配置攻略
  10. spring mvc mybatis
  11. 使用ant的jar任务打jar包
  12. 修改session的存储机制
  13. Spring Boot入门(三):使用Scheduled注解实现定时任务
  14. linux下如何查看某软件是否已安装
  15. Java发邮件基础篇
  16. POSIX 线程取消点的 Linux 实现
  17. ubuntu下pyspark的安装
  18. Can you find it?(数组+二分hdu2141)
  19. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)
  20. 119. Pascal's Triangle II (Graph; WFS)

热门文章

  1. vue中如何动态添加readonly属性
  2. 【多线程与并发】Java并发工具类
  3. ubuntu之路——day4(今天主要看了神经网络的概念)
  4. Vue 中Axios 使用
  5. Flink 之 Data Source
  6. vue 自己编写向左滑动的动画 仿transition
  7. 搭建Nuxt项目(搭配Element UI、axios)
  8. SQL Server DATEADD() 当前时间减7小时
  9. markdown2的key
  10. Spring cloud微服务安全实战-3-9API安全机制之审计日志