zoukankan      html  css  js  c++  java
  • JDBC事务机制

    package com.jdbc.test;
    
    import java.sql.*;
    
    /**
     * 数据库的引擎必须是innodb
     */
    
    public class Demo02 {
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        Statement statement = null;
    
        public static void main(String[] args) throws SQLException, ClassNotFoundException {
            Demo02 demo02 = new Demo02();
            demo02.connect();
        }
    
        public void connect() throws ClassNotFoundException, SQLException {
    
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdjk?useSSL=false", "root", "kkk");
    
            // 关闭默认提交
            connection.setAutoCommit(false);
            statement = (Statement) connection.createStatement();
    
            try {
                String sql = "insert into jdjk_java(name,age) values('alex',10)";
                statement.execute(sql);
                System.out.println("第一条数据插入完成");
                Thread.sleep(3000);
                String sql2 = "insert into jdjk_java(name,age) values('alex',10)";
                statement.execute(sql2);
                System.out.println("第二条数据也插入完成");
                
                connection.commit();
                statement.close();
                connection.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.out.println("第二条数据出错,滚动成功");
                connection.rollback();
            } finally {
                // 关闭连接
                statement.close();
                connection.close();
            }
        }
    }
    

      

  • 相关阅读:
    支付宝小程序InputItem清除icon不显示
    win11 激活 wi7 win11 魔兽争霸切换 后无法 回到游戏界面 处理办法
    HJ10 字符个数统计
    iOS 15系统导航栏适配
    HJ4 字符串分隔
    HJ7 取近似值
    [iOS]隐藏导航栏3种方式
    HJ3 明明的随机数
    HJ8 合并表记录
    HJ5 进制转换
  • 原文地址:https://www.cnblogs.com/leigepython/p/10006564.html
Copyright © 2011-2022 走看看