news 2026/8/7 14:25:51

JavaWeb-JDBC事务回滚

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
JavaWeb-JDBC事务回滚

什么是JDBC?

Java连接数据库!

需要jar包支持:

  • java.sql
  • javax.sql
  • mysql-connter-java……连接驱动(必须要导入)

实验-Mysql数据库

MySQL数据插入表中数据

CREATETABLEusers(`id`INTPRIMARYKEY,`name`VARCHAR(40),`password`VARCHAR(40),`email`VARCHAR(2222),`birthday`DATE);INSERTINTOusers(`id`,`name`,`password`,`email`,`birthday`)VALUES(1,'admin','123456','admin@gmail.com','1998-06-18');INSERTINTOusers(`id`,`name`,`password`,`email`,`birthday`)VALUES(2,'system','000000','system@gmail.com','1999-06-18');INSERTINTOusers(`id`,`name`,`password`,`email`,`birthday`)VALUES(3,'sa','00000000','sa@gmail.com','1992-06-18');INSERTINTOusers(`id`,`name`,`password`,`email`,`birthday`)VALUES(4,'web','0123456789','web@gmail.com','1991-06-18');SELECT*FROMusers;

导入数据库依赖

<dependencies><!--MySQL驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency></dependencies>

idea连接数据库

jdbc连接七部曲

  1. 获取配置信息&解决中文乱码
  2. 加载驱动

显式加载:

* Class.forName("..."):这是最经典的方式,在代码中显式地加载驱动类。例如,Class.forName("com.mysql.cj.jdbc.Driver")。 * 优点:兼容老版本,建议写、兼容性强。 * 缺点:需要手动编写代码,在一些情况下(如 JDBC 4.0 之后),可能显得冗余。

隐示加载:

spl模式

* SPI ,全称为 Service Provider Interface,是一种服务发现机制。 * 它通过在ClassPath路径下的META-INF/services文件夹查找文件,自动加载文件里所定义的类。 * 这一机制为很多框架扩展提供了可能,比如在Dubbo、JDBC中都使用到了SPI机制。

jdbc4.0后 可以自动扫描jar包下面的这个文件

  1. 连接数据库,代表数据库
  2. 向数据库发送SQL的对象 Statement 用它来做(CRUD)增删改查
  3. 编写SQL
  4. 执行查询SQL语句
  5. 关闭连接,释放资源

增删改查

//受影响的行数,CRUD都是用executeUpdate即可inti=preparedStatement.executeUpdate();

完整代码

publicstaticvoidmain(String[]args)throwsClassNotFoundException,SQLException{//1.获取配置信息// serverTimezone=UTC 解决中文乱码Stringurl="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8";Stringuser="root";Stringpassword="123456";// 2.加载驱动// Class.forName("com.mysql.jdbc.Driver");// 3.连接数据库,代表数据库Connectionconnection=DriverManager.getConnection(url,user,password);// 4.向数据库发送SQL的对象,Statement :用它来做(CRUD)增删改查Statementstatement=connection.createStatement();// 5.编写SQLStringsql="select * from users;";// 6.执行查询SQL语句,返回一个ResultSet对象ResultSetresultSet=statement.executeQuery(sql);while(resultSet.next()){// 如果还有数据System.out.println("id="+resultSet.getObject("id"));System.out.println("name="+resultSet.getObject("name"));System.out.println("password="+resultSet.getObject("password"));System.out.println("email="+resultSet.getObject("email"));System.out.println("birthday="+resultSet.getObject("birthday"));}// 7.关闭连接,释放资源(一定要做)先开后关statement.close();connection.close();resultSet.close();}}

预编译SQL

更加安全

publicclassTestJdbc2{publicstaticvoidmain(String[]args)throwsClassNotFoundException,SQLException{// 1.配置信息Stringurl="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8";Stringuser="root";Stringpassword="123456";// 2.加载驱动Class.forName("com.mysql.jdbc.Driver");// 3.连接数据库Connectionconnection=DriverManager.getConnection(url,user,password);// 4.编写SQLStringsql="insert into users(`id`,`name`,`email`,`password`,`birthday`) values (?,?,?,?,?);";// 5.预编译PreparedStatementpreparedStatement=connection.prepareStatement(sql);//设置?的值preparedStatement.setInt(1,5);//给第一个占位符,赋值id为5preparedStatement.setString(2,"test");//给第二个占位符,赋值name为testpreparedStatement.setString(3,"test@gmail.com");//给第三个占位符,赋值email为test@gmail.compreparedStatement.setString(4,"123456");//给第四个占位符,赋值password为123456preparedStatement.setDate(5,newDate(newjava.util.Date().getTime()));//给第五个占位符,赋值birthday为当前时间// 5. 执行SQLinti=preparedStatement.executeUpdate();//增删改查的判断if(i>0){System.out.println("插入成功");}else{System.out.println("插入失败");}// 6.关闭连接,释放资源connection.close();preparedStatement.close();}}

事务

要么都成功,要么都失败

ACID原则(原子性、一致性、隔离性、持久性):保证数据的安全。

  • 开启事务
  • 事务提交 commit()
  • 事务回滚 rollback()
  • 关闭事务

转账:

A:1000

B:1000

A(900) --100–>B(1100)

如果服务器崩溃了怎么办?我们不应该让这100块钱凭空的消失

Junit单元测试

测试数据经常使用,不需要new类什么的

导入依赖

pom.xml

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency>

publicclassTestJdbc3{@Testpublicvoidtest(){System.out.println("test");}}

添加@Test注解

可以看见运行键不再是灰色了,可以直接输出

@Test注解只有在方法上有效,只要加了这个注解的方法,就可以直接运行!

事务

常用通常有以下:

  • start transaction ;
  • rollback ;
  • commit ;
starttransaction;#开启事务updatejdbc.accountsetmoney=money-100whereid='1';# update jdbc.account set money = money+ 100 where id = '1';rollback;# 回滚事务,如果中间崩溃了怎么样,就会回滚,不进行提交commit;# 提交事务

可以选中这两行执行

就会只执行这两行

回滚

一定要添加这一行,这个引擎支持回滚

ALTERTABLEjdbc.accountENGINE=InnoDB;

案例代码:

publicclassTestJdbc3{@Testpublicvoidtest(){//配置信息Stringurl="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8";Stringuser="root";Stringpassword="123456";Connectionconnection=null;try{// 加载驱动Class.forName("com.mysql.jdbc.Driver");// 连接数据库connection=DriverManager.getConnection(url,user,password);// 检查自动提交状态System.out.println("自动提交状态: "+connection.getAutoCommit());// 开启事务connection.setAutoCommit(false);System.out.println("事务已开启");// 执行第一条SQLStringsql="update jdbc.account set money = money - 100 where id = 2;";PreparedStatementpstmt1=connection.prepareStatement(sql);intcount1=pstmt1.executeUpdate();System.out.println("第一条SQL影响行数: "+count1);pstmt1.close();// 检查当前数据状态checkAccountBalance(connection);// 制造错误 - 这会触发回滚inti=1/0;// 执行第二条SQLStringsql1="update jdbc.account set money = money + 100 where id = 3;";PreparedStatementpstmt2=connection.prepareStatement(sql1);intcount2=pstmt2.executeUpdate();System.out.println("第二条SQL影响行数: "+count2);pstmt2.close();// 提交事务connection.commit();System.out.println("事务提交成功!");}catch(Exceptione){System.out.println("捕获到异常: "+e.getMessage());// 发生异常时回滚事务if(connection!=null){try{connection.rollback();System.out.println("事务已回滚!所有操作已撤销");// 回滚后再次检查数据状态checkAccountBalance(connection);}catch(SQLExceptionex){System.out.println("回滚失败: "+ex.getMessage());ex.printStackTrace();}}e.printStackTrace();}finally{// 恢复自动提交并关闭连接if(connection!=null){try{connection.setAutoCommit(true);// 恢复自动提交connection.close();System.out.println("连接已关闭");}catch(SQLExceptione){e.printStackTrace();}}}}// 辅助方法:检查账户余额privatevoidcheckAccountBalance(Connectionconn)throwsSQLException{StringcheckSql="SELECT id, money FROM jdbc.account WHERE id IN (2, 3)";PreparedStatementpstmt=conn.prepareStatement(checkSql);ResultSetrs=pstmt.executeQuery();System.out.println("当前账户余额:");while(rs.next()){System.out.println("账户 "+rs.getInt("id")+": "+rs.getDouble("money"));}rs.close();pstmt.close();}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/7 1:38:07

西安最新 955 公司名单

大家好&#xff0c;我是菜哥&#xff01;最近有粉丝群里面有人聊西安&#xff0c;有人私信问我&#xff1a;"在西安&#xff0c;除了互联网大厂&#xff0c;还有哪些公司既能拿到不错的薪资&#xff0c;又不用天天加班到深夜&#xff1f;"这个问题问到点子上了&#…

作者头像 李华
网站建设 2026/8/7 23:01:42

微信不小心违规被封,好友辅助验证流程怎样?

大家好&#xff0c;我是明哥。有时候我们发的信息太多&#xff0c;微信会认为涉嫌营销&#xff0c;导致我们的微信被封几天&#xff0c;但几天解封的时候&#xff0c;会需要你做一个好友验证。那么如何去操作呢&#xff0c;以及需要注意哪些事情&#xff1f;当你微信正在聊天&a…

作者头像 李华
网站建设 2026/8/7 15:34:39

这款小工具,彻底治好了我的Mac文件管理焦虑

作为一名长期和Mac打交道的内容创作者&#xff0c;我每天都要在无数层文件夹中穿梭&#xff0c;重复着“点击-打开-返回-再打开”的繁琐操作。直到我遇见了 Default Folder X&#xff0c;它就像给我的访达&#xff08;Finder&#xff09;装上了“任督二脉”&#xff0c;效率提升…

作者头像 李华
网站建设 2026/8/7 15:36:36

智能助手性能评估:5大关键维度与实战指南

智能助手性能评估&#xff1a;5大关键维度与实战指南 【免费下载链接】agents-course This repository contains the Hugging Face Agents Course. 项目地址: https://gitcode.com/GitHub_Trending/ag/agents-course 面对市场上琳琅满目的AI助手产品&#xff0c;你是否…

作者头像 李华
网站建设 2026/8/7 22:56:58

从零开始搭建个人AI助手:Anything-LLM + Ollama下载配置全记录

从零开始搭建个人AI助手&#xff1a;Anything-LLM Ollama下载配置全记录 在一台普通的笔记本上&#xff0c;运行一个能理解你所有文档、记住你工作习惯、还能用自然语言和你对话的AI助手——这听起来像是科幻电影里的场景。但今天&#xff0c;这一切已经可以轻松实现。 不需要…

作者头像 李华