jdbc 10:jdbc事务
2024-08-29 07:00:55
jdbc连接mysql,涉及到的事务问题
package com.examples.jdbc.o10_jdbc事务;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ResourceBundle;
/**
* jdbc事务演示:单机转帐
*/
public class Test {
public static void main(String[] args) {
//countUnsafe();
countSafe();
}
/**
* 单机转帐:开启事务
*/
private static void countSafe() {
//资源绑定器绑定配置属性文件
ResourceBundle resourceBundle = ResourceBundle.getBundle("config/jdbc");
String driver = resourceBundle.getString("driver");
String url = resourceBundle.getString("url");
String userName = resourceBundle.getString("userName");
String passWord = resourceBundle.getString("passWord");
//2个资源文件
Connection connection = null;
PreparedStatement preparedStatement = null;
//jdbc6步骤
try {
//1.
Class.forName(driver);
//2.
connection = DriverManager.getConnection(url, userName, passWord);
connection.setAutoCommit(false); //开启事务
//3.
String sql = "update tb_count set money = ? where count = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setDouble(1, 10000);
preparedStatement.setInt(2, 111);
int num = preparedStatement.executeUpdate();
//String s = null;
//s.toString();
preparedStatement.setDouble(1, 10000);
preparedStatement.setInt(2, 222);
num += preparedStatement.executeUpdate();
System.out.println(num == 2 ? "转帐成功" : "转帐失败");
connection.commit(); //提交事务
} catch (ClassNotFoundException | SQLException e) {
//事务回滚
if(connection != null){
try {
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
e.printStackTrace();
}finally {
//6.
if(preparedStatement != null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* 单机转帐:未开启事务
*/
private static void countUnsafe() {
//资源绑定器绑定配置属性文件
ResourceBundle resourceBundle = ResourceBundle.getBundle("config/jdbc");
String driver = resourceBundle.getString("driver");
String url = resourceBundle.getString("url");
String userName = resourceBundle.getString("userName");
String passWord = resourceBundle.getString("passWord");
//2个资源文件
Connection connection = null;
PreparedStatement preparedStatement = null;
//jdbc6步骤
try {
//1.
Class.forName(driver);
//2.
connection = DriverManager.getConnection(url, userName, passWord);
//3.
String sql = "update tb_count set money = ? where count = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setDouble(1, 10000);
preparedStatement.setInt(2, 111);
int num = preparedStatement.executeUpdate();
String s = null;
s.toString();
preparedStatement.setDouble(1, 10000);
preparedStatement.setInt(2, 222);
num += preparedStatement.executeUpdate();
System.out.println(num == 2 ? "转帐成功" : "转帐失败");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//6.
if(preparedStatement != null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
最新文章
- python-list tuple dict set
- mavan 命令行创建项目
- 学习ASP.NET MVC(一)——我的第一个ASP.NET MVC应用程序
- Java 创建文件夹和文件
- [转]N种内核注入DLL的思路及实现
- HDU 4604 Deque(最长上升子序)
- 关于Eclipse插件之IWorkbench IWorkbenchWindow IWorkbenchPage |WorkbenchPart......等的总结
- C++ static(施工中)
- set和replace方法的区别
- 提高C#编程水平的50个要点 你掌握了多少呢?
- IIS8中使用OpenSSL来创建CA并且签发SSL证书
- 最牛分布式消息系统:Kafka
- 201521123070 《JAVA程序设计》第9周学习总结
- Max Sum of Max-K-sub-sequence hdu3415
- [LeetCode] BFS解决的题目
- Kubenetes 核心概念理解
- python发送邮件(在邮件中显示HTMLTestRunner生成的报告)
- 小甲鱼python第二讲课后习题
- SpringBoot+MyBatis多数据源使用分页插件PageHelper
- 20181223 python 使用Beautiful Soup