zoukankan      html  css  js  c++  java
  • SQL事务

    JDBC实现事务(transaction)

    1.事务的开启connection.setAutoCommit(false);

    2.中间语句是事务的执行语句

    3.事务的提交connection.commit();

    package JDBCTest;
    
    import JDBCTest.utils.JdbcUtils;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class TransactionTest {
        public static void main(String[] args) {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
    
            try {
                connection = JdbcUtils.getConnection ();
                //关闭数据库的自动提交,会自动开启事务
                connection.setAutoCommit (false);//开启事务
                String sql1 = "update account set money=money-400 where name='A'";
                preparedStatement = connection.prepareStatement (sql1);
                preparedStatement.executeUpdate ();
                String sql2 = "update account set money=money+400 where name='B'";
                preparedStatement = connection.prepareStatement (sql2);
                preparedStatement.executeUpdate ();
    
                connection.commit ();//提交事务
                System.out.println ("成功!");
            } catch (SQLException e) {
                e.printStackTrace ();
            } finally {
                JdbcUtils.release (connection,preparedStatement,null);
            }
        }
    }
    
    create table account
    (
        name  varchar(10) null,
        money int         null,
        id    int auto_increment
            primary key
    );
    
  • 相关阅读:
    url编码
    客户端安全-xss-1类型介绍
    阿里云扩容教程
    jquery获取和设置表单数据
    uMlet建模工具
    phpstorm的调试工具xdebug
    服务器如何处理http请求
    http基础实战
    协程
    Goroutine(协程)为何能处理大并发?
  • 原文地址:https://www.cnblogs.com/li33/p/12814074.html
Copyright © 2011-2022 走看看