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特性等等,网上太多了,在此不一一重复.本文主要着重 事务 ...
随机推荐
- [题解] [AGC024F] Simple Subsequence Problem
题目大意 有一个 01 串集合 \(S\),其中每个串的长度都不超过 \(N\),你要求出 \(S\) 中至少是 \(K\) 个串的子序列的最长串,如果有多解,输出字典序最小的那组解. 由于 \(S\ ...
- 基于casbin的RBAC权限实践
五一假期疫情封在家也没事做,就想来优化一下一个前端容器小项目 之前的TODOlist里面有一项是权限这块时隔2年了还一直没有动手 迟迟没搞主要还是我太懒了,哈哈 其实我一直想要找一个轻量级的权限通用方 ...
- 1.2 Linux是什么,有哪些特点?
与大家熟知的 Windows 操作系统软件一样,Linux 也是一个操作系统软件,其 logo 是一只企鹅(如图 1 所示).与 Windows 不同之处在于,Linux 是一套开放源代码程序的.可以 ...
- 『现学现忘』Git基础 — 20、Git中忽略文件补充
目录 1.忽略文件常遇到的问题 2.忽略文件配置优先级 3.忽略已跟踪文件的改动(本机使用) 4.autocrlf和safecrlf参数说明 (1)提出问题 (2)autocrlf说明 (3)safe ...
- CEPH-5:ceph集群基本概念与管理
ceph集群基本概念与管理 ceph集群基本概念 ceph集群整体结构图 名称 作用 osd 全称Object Storage Device,主要功能是存储数据.复制数据.平衡数据.恢复数据等.每个O ...
- PHP代码审计之SQL注入
代码审计之SQL注入 SQL注入攻击(SQLInjection),是攻击者在表单中提交精心构造的sql语句,改变原来的sql语句,如果web程序没有对提交的数据经过检查,那么就会造成sql注入攻击. ...
- Python-100-Days-master-第二周笔记
python100day学习第二周 # 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值 list1 = [1, 3, 5, 7, 100] for index, elem in ...
- HAVING,多表查询思路,可视化软件navicat,多表查询练习题,
HAVING "where"是一个约束声明,在查询数据库的结果返回之前对数据库中的查询条件进行约束,即在结果返回之 前起作用,且"where"后面不能写&quo ...
- 好客租房53-context的使用
app组件要传递给child组件 该如何处理 更好的姿势 跨组件传递数据 Provider 用来提供数据 Consumer用来消费数据 1调用React.createContext() 创建provi ...
- python操作MySQL与MySQL补充
目录 python操作MySQL 基本使用 SQL注入问题 二次确认 视图 触发器 事务 存储过程 函数 流程控制 索引 练习 python操作MySQL python中支持操作MySQL的模块很多, ...