- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章详解Java的JDBC API中事务的提交和回滚由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
如果JDBC连接是在自动提交模式下,它在默认情况下,那么每个SQL语句都是在其完成时提交到数据库.
这可能是对简单的应用程序,但有三个原因,你可能想关闭自动提交和管理自己的事务:
若要控制事务,以及何时更改应用到数据库。它把单个SQL语句或一组SQL语句作为一个逻辑单元,而且如果任何语句失败,整个事务失败.
若要启用,而不是JDBC驱动程序默认使用auto-commit模式手动事务支持,使用Connection对象的的setAutoCommit()方法。如果传递一个布尔值false到setAutoCommit(),关闭自动提交。可以传递一个布尔值true将其重新打开.
例如,如果有一个名为conn Connection对象,以下代码来关闭自动提交:
1
|
conn.setAutoCommit(
false
);
|
提交和回滚 一旦已经完成了变化,要提交更改,然后调用commit(在连接对象)方法,如下所示:
1
|
conn.commit( );
|
否则回滚更新对数据库所做的使用命名连接conn,使用下面的代码:
1
|
conn.rollback( );
|
下面的例子演示了如何使用一个提交和回滚对象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
try
{
//Assume a valid connection object conn
conn.setAutoCommit(
false
);
Statement stmt = conn.createStatement();
String SQL =
"INSERT INTO Employees "
+
"VALUES (106, 20, 'Rita', 'Tez')"
;
stmt.executeUpdate(SQL);
//Submit a malformed SQL statement that breaks
String SQL =
"INSERTED IN Employees "
+
"VALUES (107, 22, 'Sita', 'Singh')"
;
stmt.executeUpdate(SQL);
// If there is no error.
conn.commit();
}
catch
(SQLException se){
// If there is any error.
conn.rollback();
}
|
在这种情况下没有上述INSERT语句会成功,一切都将被回滚.
事务提交和回滚示例 以下是使用事务提交和回滚描述的例子.
基于对环境和数据库安装在前面的章节中做此示例代码已学习过.
复制过去下面的例子中JDBCExample.java,编译并运行,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
//STEP 1. Import required packages
import
java.sql.*;
public
class
JDBCExample {
// JDBC driver name and database URL
static
final
String JDBC_DRIVER =
"com.mysql.jdbc.Driver"
;
static
final
String DB_URL =
"jdbc:mysql://localhost/EMP"
;
// Database credentials
static
final
String USER =
"username"
;
static
final
String PASS =
"password"
;
public
static
void
main(String[] args) {
Connection conn =
null
;
Statement stmt =
null
;
try
{
//STEP 2: Register JDBC driver
Class.forName(
"com.mysql.jdbc.Driver"
);
//STEP 3: Open a connection
System.out.println(
"Connecting to database..."
);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Set auto commit as false.
conn.setAutoCommit(
false
);
//STEP 5: Execute a query to create statment with
// required arguments for RS example.
System.out.println(
"Creating statement..."
);
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//STEP 6: INSERT a row into Employees table
System.out.println(
"Inserting one row...."
);
String SQL =
"INSERT INTO Employees "
+
"VALUES (106, 20, 'Rita', 'Tez')"
;
stmt.executeUpdate(SQL);
//STEP 7: INSERT one more row into Employees table
SQL =
"INSERT INTO Employees "
+
"VALUES (107, 22, 'Sita', 'Singh')"
;
stmt.executeUpdate(SQL);
//STEP 8: Commit data here.
System.out.println(
"Commiting data here...."
);
conn.commit();
//STEP 9: Now list all the available records.
String sql =
"SELECT id, first, last, age FROM Employees"
;
ResultSet rs = stmt.executeQuery(sql);
System.out.println(
"List result set for reference...."
);
printRs(rs);
//STEP 10: Clean-up environment
rs.close();
stmt.close();
conn.close();
}
catch
(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
// If there is an error then rollback the changes.
System.out.println(
"Rolling back data here...."
);
try
{
if
(conn!=
null
)
conn.rollback();
}
catch
(SQLException se2){
se2.printStackTrace();
}
//end try
}
catch
(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
finally
{
//finally block used to close resources
try
{
if
(stmt!=
null
)
stmt.close();
}
catch
(SQLException se2){
}
// nothing we can do
try
{
if
(conn!=
null
)
conn.close();
}
catch
(SQLException se){
se.printStackTrace();
}
//end finally try
}
//end try
System.out.println(
"Goodbye!"
);
}
//end main
public
static
void
printRs(ResultSet rs)
throws
SQLException{
//Ensure we start with first row
rs.beforeFirst();
while
(rs.next()){
//Retrieve by column name
int
id = rs.getInt(
"id"
);
int
age = rs.getInt(
"age"
);
String first = rs.getString(
"first"
);
String last = rs.getString(
"last"
);
//Display values
System.out.print(
"ID: "
+ id);
System.out.print(
", Age: "
+ age);
System.out.print(
", First: "
+ first);
System.out.println(
", Last: "
+ last);
}
System.out.println();
}
//end printRs()
}
//end JDBCExample
|
现在让我们来编译上面的例子如下:
1
|
C:>javac JDBCExample.java
|
当运行JDBCExample,它会产生以下结果:
1
|
C:>java JDBCExample
|
1
2
3
4
5
6
7
8
9
10
11
12
|
Connecting to database...
Creating statement...
Inserting one row....
Commiting data here....
List result set for reference....
ID: 100, Age: 18, First: Zara, Last: Ali
ID: 101, Age: 25, First: Mahnaz, Last: Fatma
ID: 102, Age: 30, First: Zaid, Last: Khan
ID: 103, Age: 28, First: Sumit, Last: Mittal
ID: 106, Age: 20, First: Rita, Last: Tez
ID: 107, Age: 22, First: Sita, Last: Singh
Goodbye!
|
最后此篇关于详解Java的JDBC API中事务的提交和回滚的文章就讲到这里了,如果你想了解更多关于详解Java的JDBC API中事务的提交和回滚的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!