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();
}
}
}
}
}

最新文章

  1. python-list tuple dict set
  2. mavan 命令行创建项目
  3. 学习ASP.NET MVC(一)——我的第一个ASP.NET MVC应用程序
  4. Java 创建文件夹和文件
  5. [转]N种内核注入DLL的思路及实现
  6. HDU 4604 Deque(最长上升子序)
  7. 关于Eclipse插件之IWorkbench IWorkbenchWindow IWorkbenchPage |WorkbenchPart......等的总结
  8. C++ static(施工中)
  9. set和replace方法的区别
  10. 提高C#编程水平的50个要点 你掌握了多少呢?
  11. IIS8中使用OpenSSL来创建CA并且签发SSL证书
  12. 最牛分布式消息系统:Kafka
  13. 201521123070 《JAVA程序设计》第9周学习总结
  14. Max Sum of Max-K-sub-sequence hdu3415
  15. [LeetCode] BFS解决的题目
  16. Kubenetes 核心概念理解
  17. python发送邮件(在邮件中显示HTMLTestRunner生成的报告)
  18. 小甲鱼python第二讲课后习题
  19. SpringBoot+MyBatis多数据源使用分页插件PageHelper
  20. 20181223 python 使用Beautiful Soup

热门文章

  1. 利用VTK和PyQt5对医学体数据进行渲染并展示
  2. Linux screen命令详解
  3. Json序列化与反序列化导致多线程运行速度和单线程运行速度一致问题
  4. vue - Vue路由
  5. 第一个Python程序 | 机选彩票号码+爬取最新开奖号码
  6. [java并发编程]基于信号量semaphore实现限流器
  7. @Inherited 原注解功能介绍
  8. React简单教程-4-事件和hook
  9. 7. Docker CI、CD
  10. 1. Docker的中央仓库安装设置及镜像的操作