• Java中MySQL事务处理举例


    实例(以sql语句中的insert语句为例)

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    /**
     * 事务的基本用法
     *
     */
    public class Test 
    {
        public static void main(String[] args) 
        {
            Connection conn = null;
            PreparedStatement ps1 = null;
            PreparedStatement ps2 = null;
    
            try
            {
                //加载驱动类
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","123456");
                
                conn.setAutoCommit(false); //JDBC中默认是true,我们改成false,然后在后面手动提交
                
                ps1 = conn.prepareStatement("insert into t_user (username,pwd) values (?,?)");//?是占位符
                ps1.setObject(1, "张三");
                ps1.setObject(2, "666666");
                ps1.execute();
                System.out.println("插入一个用户张三");
                
                try 
                {
                    Thread.sleep(3000);
                } 
                catch (InterruptedException e) 
                {
                    e.printStackTrace();
                }
                
                ps2 = conn.prepareStatement("insert into t_user (username,pwd) values (?,?)");
                ps2.setObject(1, "李四");
                ps2.setObject(2, "123456");
                ps2.execute();            
                System.out.println("插入一个用户李四");
                
                conn.commit();//提交事务
             }
             catch (ClassNotFoundException e)
             {
                e.printStackTrace();
                try 
                {
                    conn.rollback();//某一条数据添加失败时,回滚
                } 
                catch (SQLException e1) 
                {
                    e1.printStackTrace();
                }
            } 
            catch (SQLException e) 
            {
                e.printStackTrace();
            }
            finally
            {
                try 
                {
                    if(ps1!=null)
                    {
                        ps1.close();
                    }
                } 
                catch (SQLException e) 
                {
                    e.printStackTrace();
                }
                try 
                {
                    if(conn!=null)
                    {
                        conn.close();
                    }
                } 
                catch (SQLException e) 
                {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    MAXSCRIPT 连数据库(转)
    Docker安装部署ELK教程 (Elasticsearch+Kibana+Logstash+Filebeat)
    ArrayList知识点
    HashMap知识点
    使用docker 安装nacos
    记录docker 安装sonarqube和安装的一些坑
    sql优化
    Centos7下安装Docker
    使用docker安装gitlab
    docker安装jenkins
  • 原文地址:https://www.cnblogs.com/sungong1987/p/6559315.html
Copyright © 2020-2023  润新知