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 ...
随机推荐
- QuartzNet 远程管理持久化job 项目, 源码在Github..希望对大家有所帮助
文章目录 为了方便大家去学习 QuartzNet 与 CrystalQuartz 更多信息请点击链接查看 简介 结构图 为了方便大家去学习 QuartzNet 与 CrystalQuartz 更多信息 ...
- 为什么要监控sql语句,以及如何监控,都有哪几种方式可以监控。
快速阅读 为什么要监控sql语句,以及如何监控,都有哪几种方式可以监控. 我们知道sql server 中有个工具叫sql profile ,可以实时监控sql server中 执行的sql 语句,以 ...
- macbook ios recovery and mount hfs+ journal and revert
sudo fsck.hfsplus -fryd /dev/sdc2 /sbin/fsck_hfs -yprd /dev/partitionName---can not fix b-tree node ...
- SWLU:主核性能采样、调试工具包
http://bbs.nsccwx.cn/topic/262/swlu-主核性能采样-调试工具包
- python 设计模式之适配器模式 Adapter Class/Object Pattern
#写在前面 看完了<妙味>和<华医>,又情不自禁的找小说看,点开了推荐里面随机弹出的<暗恋.橘生淮南>,翻了下里面的评论,有个读者从里面摘了一段自己很喜欢的话出来, ...
- 搭建 Kafka 集群 (v2.12-2.3.0)
服务器:10.20.32.121,10.20.32.122,10.20.32.123 三台服务器都需要安装jdk.配置zookeeper.配置kafka 1.安装配置jdk1.8 [root@loca ...
- git pull There is no tracking information for the current branch.
在高版本的 git下面,也许会看见这样的提示: 解决方案:指定当前工作目录工作分支,跟远程的仓库,分支之间的链接关系. 比如我们设置master对应远程仓库的master分支 git branch - ...
- php5.6.30环境报错Call to undefined function ImageCreate() 编译安装 gd库
php5..30环境报错Call to undefined function ImageCreate() 编译安装 gd库 发现php5..30没有加载gd库 [root@cn_vs_web04:/u ...
- shared_ptr的使用和陷阱
shared_ptr的使用和陷阱 shared_ptr的使用 分配内存 make_shared //make_shared<int>分配一块int类型大小的内存,并值初始化为100 //返 ...
- 安装mysql报错:Can't find messagefile '/usr/share/mysql/english/errmsg.sys'和/usr/bin/mysqladmin: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or
使用yum安装mysql服务端: [root@centos ~]# yum -y install mysql-server Loaded plugins: fastestmirror, securit ...