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

jdbc 10:jdbc事务的更多相关文章

  1. Java数据库连接——JDBC调用存储过程,事务管理和高级应用

    一.JDBC常用的API深入详解及存储过程的调用 相关链接:Jdbc调用存储过程 1.存储过程(Stored Procedure)的介绍 我们常用的操作数据库语言SQL语句在执行的时候需要先编译,然后 ...

  2. JDBC&&c3p0、事务、批处理、多线程 于一体的经典秘方QueryRunner

    目录: 基础篇_功能各自回顾 JDBC基础代码回顾(使用JdbcUtils工具简化) c3p0数据库连接池的使用(使用JdbcUtils工具简化) 大数据的插入(使用c3p0+JdbcUtils工具简 ...

  3. 一、DAO设计模式 二、DAO设计模式的优化 三、JDBC中的事务,连接池的使用

    一.DAO设计模式概述###<1>概念 DAO,Data Access Object ,用于访问数据库的对象. 位于业务逻辑和数据持久化层之间,实现对数据持久化层的访问![](1.png) ...

  4. JAVA JDBC(存储过程和事务管理)

    1.什么是存储过程 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程 ...

  5. 创建JDBC模板简化代码、JDBC应用的事务管理以及连接池的作用

    一.创建JDBC模板简化代码 一个简单的查询.要做这么一大堆事情,并且还要处理异常,我们不防来梳理一下: 1.获取connection  2.获取statement  3.获取resultset  4 ...

  6. JDBC基础:JDBC快速入门,JDBC工具类,SQL注入攻击,JDBC管理事务

    JDBC基础 重难点梳理 一.JDBC快速入门 1.jdbc的概念 JDBC(Java DataBase Connectivity:java数据库连接)是一种用于执行SQL语句的Java API,可以 ...

  7. JAVAEE——spring03:spring整合JDBC和aop事务

    一.spring整合JDBC 1.spring提供了很多模板整合Dao技术 2.spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术. JDBCTemplate => JDBC模 ...

  8. java框架之Spring(3)-JDBC模板使用&事务管理

    下面内容使用到的 jar 包下载 JDBC模板使用 入门 1.导包,如要导入 Spring 的基本开发包.数据库驱动包.Spring 提供的 JDBC 模板包,如下: 2.测试: @Test publ ...

  9. JDBC编程之事务的使用教程

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5868750.html  关于事务的理论知识.ACID特性等等,网上太多了,在此不一一重复.本文主要着重  事务 ...

随机推荐

  1. iptables系列教程(二)| iptables语法规则

    一个执着于技术的公众号 iptables 命令基本语法 " iptables [-t table] command [链名] [条件匹配] [-j 目标动作] 以下是对 iptables 命 ...

  2. 项目文章|DNA(羟)甲基化研究揭示铁离子依赖表观调控促进狼疮致病性T细胞分化|易基因

    易基因(羟)甲基化DNA免疫共沉淀测序(h)MeDIP-seq研究成果见刊<Journal of Clinical Investigation> 2022年5月2日,中南大学湘雅二医院赵明 ...

  3. 创建进程,join方法,进程对象相关属性和方法,僵尸进程和孤儿进程,守护进程,互斥锁

    创建进程 在python中提供了一个multiprocessing模块可以帮助我们使用多进程解决问题.在multiprocessing 模块中有一个类Process. from multiproces ...

  4. Nginx报错收集

    在安装完成ngixn之后,访问页面显示空白,报错信息里面有这一条报错信息: tailf /usr/local/nginx/logs/error.log 2018/10/26 10:58:00 [err ...

  5. 用了Scrum越来越累?这三点帮你走出困境

    摘要:你有没有一种感觉,团队用了Scrum之后,工作任务越来越多,加班越来越严重?有?好兄弟,这篇文章正好能帮你~ 本文分享自华为云社区<用了Scrum越来越累?这三点帮你走出困境>,作者 ...

  6. 767. Reorganize String - LeetCode

    Question 767. Reorganize String Solution 题目大意: 给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串. 思路: 首先找 ...

  7. 706. Design HashMap - LeetCode

    Question 706. Design HashMap Solution 题目大意:构造一个hashmap 思路:讨个巧,只要求key是int,哈希函数选择f(x)=x,规定key最大为100000 ...

  8. 136. Single Number - LeetCode

    Question 136. Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int single ...

  9. 一文带你看懂Java中的Lock锁底层AQS到底是如何实现的

    前言 相信大家对Java中的Lock锁应该不会陌生,比如ReentrantLock,锁主要是用来解决解决多线程运行访问共享资源时的线程安全问题.那你是不是很好奇,这些Lock锁api是如何实现的呢?本 ...

  10. js 定时器 Timer

    1 /* Timer 定时器 2 3 parameter: 4 func: Function; //定时器运行时的回调; 默认 null 5 speed: Number; //延迟多少毫秒执行一次 f ...