[学习笔记]

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

java当中JDBC当中的transaction例子的更多相关文章

  1. java当中JDBC当中请给出一个DataSource的单态模式(SingleTon)HelloWorld例子

    [学习笔记] 2.DataSource的单态模式(SingleTon)程序 咱们还接着上面的例子来说.1万个人要看书.千万确保要只建立一个图书馆.要是一不留神,建了两个或三个图书馆,那可就亏大发了.对 ...

  2. java当中JDBC当中Scrollable和Updatable ResultSet的用法和Helloworld例子

    [学习笔记] 在前面的jdbc的Helloworld程序当中,我们接触了最简单的 Statement.那种Statement的光标只能向前移.意思就是访问完2,只能继续访问3,不能再回过头来访问1.还 ...

  3. java中JDBC当中请给出一个DataSource的HelloWorld例子

    在前面 的jdbc的Helloworld程序当中,我们用DriverManager来获取数据库连接.事实上通过这种方法获取数据库连接,是比较耗费计算机资 源的.当然了,这也是没有办法的事儿.就像我们买 ...

  4. java当中JDBC当中JNDI用来查找dataSource的例子

    [学习笔记] 8.JNDI用来查找dataSource的例子: import javax.naming.InitialContext;import javax.naming.Context; impo ...

  5. java当中JDBC当中请给出一个sql server的helloworld例子

    [学习笔记] 1.sql server的helloworld例子: import java.sql.*; public class JdbcHelloSqlServer {  public stati ...

  6. java当中JDBC当中请给出一个sql server的stored procedure例子

    3.sql server的stored procedure例子: import java.sql.*;public class StoredProc0 {public static void main ...

  7. java当中JDBC当中请给出一个Oracle DataSource and SingleTon例子

    [学习笔记] 6.Oracle DataSource and SingleTon: import oracle.jdbc.pool.OracleDataSource;import java.sql.C ...

  8. java当中JDBC当中请给出一个sql server的dataSource的helloworld例子

     [学习笔记] 4. sql server的dataSource的helloworld: import java.sql.*;import javax.sql.*;import net.sourcef ...

  9. java当中JDBC当中请给出一个SQLServer DataSource and SingleTon例子

    [学习笔记] 5.SQLServer DataSource and SingleTon: import net.sourceforge.jtds.jdbcx.*;import java.sql.*;i ...

随机推荐

  1. CSS Sticky 其实很简单

    为什么要写这篇文章 Sticky 也不是新知识点了,写这篇文章的原因是由于最近在实现效果的过程中,发现我对 Sticky 的理解有偏差,代码执行结果不如预期.决定写篇文章重新学习一次. 什么是 Sti ...

  2. mac安装rust的pyo3模块

    Rust 发布的工具链包括了 stable.beta 和 nightly 三种不同版本. nightly 是最激进的版本,包含了大量(可能不稳定)的新/高级特性.stable 版本目前可能还不支持一些 ...

  3. vim 文本替换讲解

    在VIM中进行文本替换: 1. 替换当前行中的内容: :s/from/to/ (s即substitude) :s/from/to/ : 将当前行中的第一个from,替换成to.如果当前行含有多个 fr ...

  4. iOS开发之如何在用户删除应用后保持一些数据

    在开发过程中我们有时候在用户删除时候保存一些信息在用户下次安装应用时候使用,这个时候我们可以使用剪切版UIPasteboard的FindUIPasteboard和钥匙串keychain的使用 剪切版剪 ...

  5. centos安装jdk1.8的三种方法

    一.手动解压安装包: 1.在user目录下新建java文件夹:   # cd /usr/   # mkdir java   # cd java 2.下载jdk1.8,进入http://www.orac ...

  6. 查看 ssh 攻击 和 攻击成功者

    查看攻击失败记录: grep "Failed password for invalid user admin" /var/log/auth.log 查看攻击成功的记录: grep ...

  7. RK3399 pro 开发记录

    RK3399有三种启动模式:1.Normal模式:2.Loader模式:3.MaskRom模式.      Normal模式是正常的启动过程,各个组件依次加载,直到正常进入系统.      Loade ...

  8. EDAS Serverless & Kubernetes SLB LVS Nginx

    分布式缓存负载均衡的规则处理:虚拟节点对一致性哈希的改进 - yanghuahui - 博客园https://www.cnblogs.com/yanghuahui/p/3755460.html EDA ...

  9. centos6.8安装python3.7.3报错Can't connect to HTTPS URL because the SSL module is not available问题解决

    环境:CentOS release 6.8 (Final) # 直接编译python3.7在使用pip3安装依赖的时候报错: Can't connect to HTTPS URL because th ...

  10. stl中map的四种插入方法总结

    stl中map的四种插入方法总结方法一:pair例:map<int, string> mp;mp.insert(pair<int,string>(1,"aaaaa&q ...