java当中JDBC当中的transaction例子
[学习笔记]
7.jdbc的transaction例子:
import java.sql.*;
public class MySQlTransaction1 {
public static void main(String[] args) throws SQLException {
/*in my sql: create table Accounts(
ID int(4) not null,
NAME varchar(15),
BALANCE int(4),
primary key(ID)
) type=INNODB;
insert into Accounts values(1,'wangwu',100);
insert into Accounts values(3,'zhangsan',300);
insert into Accounts values(4,'lisi',400);
*/
Connection con = null;
Statement s = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "1234");
//s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
/*by default, whenever execute a sql, it will commit automatically,
public void setAutoCommit(boolean autoCommit) throws SQLException
Sets this connection's auto-commit mode to the given state. If a connection is in auto-commit
mode, then all its SQL statements will be executed and committed as individual transactions.
Otherwise, its SQL statements are grouped into transactions that are terminated by a call to
either the method commit or the method rollback. By default, new connections are in
auto-commit mode. */
s = con.createStatement();
s.executeUpdate("update ACCOUNTS set BALANCE=508 where ID=3");
System.out.println("333333");
/*下一步中本来应为where ID=4, 但是却误写成了www ID=4, 所以有错,所以到catch中,但rollback时
, 却做不成功, 因为是autocommited模式,所以上一句ID=3,就做真改成508了。*/
s.executeUpdate("update ACCOUNTS set BALANCE=608 www ID=4");
System.out.println("444444");
System.out.println("con = " + con);
}
catch (Exception e) {
try{
con.rollback();
System.out.println("rollback successfully");
}catch (Exception ex)
{
ex.printStackTrace();
}
}
finally {
s.close();
con.close();
System.out.println("successfully in finally");
}
}
}
文章转载自原文:https://blog.csdn.net/qq_43650923/article/details/100653000
java当中JDBC当中的transaction例子的更多相关文章
- java当中JDBC当中请给出一个DataSource的单态模式(SingleTon)HelloWorld例子
[学习笔记] 2.DataSource的单态模式(SingleTon)程序 咱们还接着上面的例子来说.1万个人要看书.千万确保要只建立一个图书馆.要是一不留神,建了两个或三个图书馆,那可就亏大发了.对 ...
- java当中JDBC当中Scrollable和Updatable ResultSet的用法和Helloworld例子
[学习笔记] 在前面的jdbc的Helloworld程序当中,我们接触了最简单的 Statement.那种Statement的光标只能向前移.意思就是访问完2,只能继续访问3,不能再回过头来访问1.还 ...
- java中JDBC当中请给出一个DataSource的HelloWorld例子
在前面 的jdbc的Helloworld程序当中,我们用DriverManager来获取数据库连接.事实上通过这种方法获取数据库连接,是比较耗费计算机资 源的.当然了,这也是没有办法的事儿.就像我们买 ...
- java当中JDBC当中JNDI用来查找dataSource的例子
[学习笔记] 8.JNDI用来查找dataSource的例子: import javax.naming.InitialContext;import javax.naming.Context; impo ...
- java当中JDBC当中请给出一个sql server的helloworld例子
[学习笔记] 1.sql server的helloworld例子: import java.sql.*; public class JdbcHelloSqlServer { public stati ...
- java当中JDBC当中请给出一个sql server的stored procedure例子
3.sql server的stored procedure例子: import java.sql.*;public class StoredProc0 {public static void main ...
- java当中JDBC当中请给出一个Oracle DataSource and SingleTon例子
[学习笔记] 6.Oracle DataSource and SingleTon: import oracle.jdbc.pool.OracleDataSource;import java.sql.C ...
- java当中JDBC当中请给出一个sql server的dataSource的helloworld例子
[学习笔记] 4. sql server的dataSource的helloworld: import java.sql.*;import javax.sql.*;import net.sourcef ...
- java当中JDBC当中请给出一个SQLServer DataSource and SingleTon例子
[学习笔记] 5.SQLServer DataSource and SingleTon: import net.sourceforge.jtds.jdbcx.*;import java.sql.*;i ...
随机推荐
- 记一次vue+vuex+vue-router+axios+elementUI开发(一)
记录自己的vue开发之旅,方便之后查询 一.开发环境 1.安装node.js 自带npm https://nodejs.org/en/ 2. 全局安装vue-cli脚手架 npm install v ...
- kubernetes 1.14安装部署EFK日志收集系统
简单介绍: EFK 组合插件是k8s项目的一个日志解决方案,它包括三个组件:Elasticsearch, Fluentd, Kibana.相对于ELK这样的架构,k8s官方推行了EFK,可能Fluen ...
- JVM常量的含义与反编译助记符详解
1.定义一个常量 public class MyTest2 { public static void main(String[] args) { System.out.println(MyParent ...
- c++ 函数后面加一个冒号的含义
转载自:https://zhidao.baidu.com/question/2010930169328038188.html 冒号后面跟的是赋值,这种写法是C++的特性. A( int aa, int ...
- MiniUI官方表单验证示例
原文地址:http://www.miniui.com/docs/tutorial/validator.html 表单验证 参考示例: 验证规则 表单验证 表单验证:文本提示 表 ...
- 办公室上网慢原因排查 有人下载东西 设置行为管理 宽带分配设置独立下行宽带 TL-WVR1200L
办公室上网慢原因排查 有人下载东西 设置行为管理 宽带分配设置独立下行宽带
- 021 CSS高级特性
一:元素的显示与影藏 1.比较常见的单词 dispaly,visibility,overflow 2.display案例 如果影藏了,这个元素就看不见了,然后也不保留位置 <!DOCTYPE h ...
- 服务器端实时推送技术之SseEmitter的用法
这是SpringMVC提供的一种技术,可以实现服务端向客户端实时推送数据.用法非常简单,只需要在Controller提供一个接口,创建并返回SseEmitter对象,发送数据可以在另一个接口调用其se ...
- Python爬取所有城市的aqi值
# -*- coding: utf-8 -*- # author:baoshan import requests from bs4 import BeautifulSoup def get_city_ ...
- mtcnn论文学习
Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks 使用多任务级联卷积网络连接人脸检 ...