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 ...
随机推荐
- SpringMVC的处理器全局异常处理类
SpringMVC的处理器全局异常处理类 package com.huawei.utils; import org.springframework.web.servlet.HandlerExcepti ...
- mint-ui 做省市选择组件
省市的数据是动态的,其实不是动态的更好搞 <!-- 省市选择 --> <mt-popup v-model="popupVisible" position=&quo ...
- el-select定义初始值并且可以修改
[](https://img2018.cnblogs.com/blog/1338470/201811/1338470-20181112152013318-1731627947.png <el-f ...
- docker本地仓库&镜像
镜像的命名规则: 1.[冷数据]/[base镜像]例如:ansible,centos 2. lastest{最新的意思} 不是真的(随便命名) 3. [image name]=[repository ...
- OSPF外部实验详解
- Hutool工具类之HttpUtil使用Https
关于Hutool工具类之HttpUtil如何使用可以参考官方文档Hutool之HttpUtil 其实使用Http和Https使用的方式是一样的. 建议大家可以看看HttpUtil的源码,感觉设计的挺不 ...
- css中的浮动与定位
传送门:https://www.cnblogs.com/junwuyao/p/7435257.html
- How to Hack Unity Games using Mono Injection Tutorial
https://guidedhacking.com/threads/how-to-hack-unity-games-using-mono-injection-tutorial.11674/ Unity ...
- heatmap.js 参数说明
blur:每个点都是两个圆组成的,分别为内圆和外圆:外圆越大,看起来这个点越模糊,内圆部分比较清晰:外圆的颜色比较固定且与内圆颜色不同,内圆的颜色由value确定:blur决定外圆与内圆的占比大小 ...
- linux下如何制作initramfs镜像?
1. 准备文件 加入已经准备好了所有文件在/home/initrd-base目录下 2. 在内核中指定/home/initramfs-base目录 General setup -> (/home ...