jdbc 10:jdbc事务
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事务的更多相关文章
- Java数据库连接——JDBC调用存储过程,事务管理和高级应用
一.JDBC常用的API深入详解及存储过程的调用 相关链接:Jdbc调用存储过程 1.存储过程(Stored Procedure)的介绍 我们常用的操作数据库语言SQL语句在执行的时候需要先编译,然后 ...
- JDBC&&c3p0、事务、批处理、多线程 于一体的经典秘方QueryRunner
目录: 基础篇_功能各自回顾 JDBC基础代码回顾(使用JdbcUtils工具简化) c3p0数据库连接池的使用(使用JdbcUtils工具简化) 大数据的插入(使用c3p0+JdbcUtils工具简 ...
- 一、DAO设计模式 二、DAO设计模式的优化 三、JDBC中的事务,连接池的使用
一.DAO设计模式概述###<1>概念 DAO,Data Access Object ,用于访问数据库的对象. 位于业务逻辑和数据持久化层之间,实现对数据持久化层的访问 ...
- JAVA JDBC(存储过程和事务管理)
1.什么是存储过程 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程 ...
- 创建JDBC模板简化代码、JDBC应用的事务管理以及连接池的作用
一.创建JDBC模板简化代码 一个简单的查询.要做这么一大堆事情,并且还要处理异常,我们不防来梳理一下: 1.获取connection 2.获取statement 3.获取resultset 4 ...
- JDBC基础:JDBC快速入门,JDBC工具类,SQL注入攻击,JDBC管理事务
JDBC基础 重难点梳理 一.JDBC快速入门 1.jdbc的概念 JDBC(Java DataBase Connectivity:java数据库连接)是一种用于执行SQL语句的Java API,可以 ...
- JAVAEE——spring03:spring整合JDBC和aop事务
一.spring整合JDBC 1.spring提供了很多模板整合Dao技术 2.spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术. JDBCTemplate => JDBC模 ...
- java框架之Spring(3)-JDBC模板使用&事务管理
下面内容使用到的 jar 包下载 JDBC模板使用 入门 1.导包,如要导入 Spring 的基本开发包.数据库驱动包.Spring 提供的 JDBC 模板包,如下: 2.测试: @Test publ ...
- JDBC编程之事务的使用教程
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5868750.html 关于事务的理论知识.ACID特性等等,网上太多了,在此不一一重复.本文主要着重 事务 ...
随机推荐
- 用浏览器快速开启Docker的体验之旅
互联网科技发展创造了很多奇迹,比如我今天要提到的 docker 技术就是其一.我很早就关注它(在2015年写过这方面的博客),那会儿还只是一个开源项目,现在已经是一个行业事实标准了,它推动了云原生的变 ...
- 老生常谈系列之Aop--JDK动态代理的底层实现原理
老生常谈系列之Aop--JDK动态代理的底层实现原理 前言 在Aop系列里面有两篇文章,分别是老生常谈系列之Aop--Spring Aop原理浅析和老生常谈系列之Aop--Spring Aop源码解析 ...
- 异步加载数据——turn.js
var tostore = GetQueryString("tostore"); var photo_id = GetQueryString("photo_id" ...
- 论文解读(SAGPool)《Self-Attention Graph Pooling》
论文信息 论文标题:Self-Attention Graph Pooling论文作者:Junhyun Lee, Inyeop Lee, Jaewoo Kang论文来源:2019, ICML论文地址:d ...
- C语言函数调用栈
C语言函数调用栈 栈溢出(stack overflow)是最常见的二进制漏洞,在介绍栈溢出之前,我们首先需要了解函数调用栈. 函数调用栈是一块连续的用来保存函数运行状态的内存区域,调用函数(calle ...
- SRIO RapidIO (SRIO)协议介绍(-)
1 导读 1.1 与PCIe的差异 典型的PCIe结构定义了一个以单个中央处理器为核心的计算机系统,比如我们常见的工控机.PXIe机箱控制器.服务器内的IO设备.从系统架构来看,这个结构 ...
- 干掉RedisHelper,请这样用分布式缓存
前言 我们在项目中使用Redis时通常是写一个单例模式的RedisHelper静态类,暴露一些常用的Get.Set等操作,在需要使用地方直接RedisHelper.StringGet(xx,xx)就可 ...
- 817. Linked List Components - LeetCode
Question 817. Linked List Components Solution 题目大意:给一个链表和该链表元素组成的一个子数组,求子数组在链表中组成多少个片段,每个片段中可有多个连续的元 ...
- redis高可用、redis集群、redis缓存优化
今日内容概要 redis高可用 redis集群 redis缓存优化 内容详细 1.redis高可用 # 主从复制存在的问题: 1 主从复制,主节点发生故障,需要做故障转移,可以手动转移:让其中一个sl ...
- 2021.02.27【NOIP提高B组】模拟 总结
T1 欧拉筛质数时若 \(i\) 是质数且没有被用过就顺便用于计算结果,复杂度 \(O(n)\) #include<bits/stdc++.h> using namespace std; ...