代碼中反復(fù)開關(guān)自動提交沒有必要. 其他方面寫得還是很不錯的.清晰.
摘自 http://wangqinqin.iteye.com/blog/547277
摘自 http://wangqinqin.iteye.com/blog/547277
PreparedStatement:
1) addBatch()將一組參數(shù)添加到PreparedStatement對象內(nèi)部。
2) executeBatch()將一批參數(shù)提交給數(shù)據(jù)庫來執(zhí)行,如果全部命令執(zhí)行成功,則返回更新計數(shù)組成的數(shù)組。
Statement:
1) addBatch(String sql)方法會在批處理緩存中加入一條sql語句。
2) executeBatch()執(zhí)行批處理緩存中的所有sql語句。
注意:PreparedStatement中使用批量更新時,要先設(shè)置好參數(shù)后再使用addBatch()方法加入緩存。批量更新中只能使用更改、刪除或插入語句。
1
Statement批量處理和事務(wù)代碼如下:
2
package com.ambow.day20.jdbc.JDBCTestCommitAndRollback;
3
import java.sql.Connection;
4
import java.sql.SQLException;
5
import java.sql.Statement;
6
import com.ambow.day19.jdbc.util.JDBCConAndClo;
7
/*
8
*1,首先把Auto commit設(shè)置為false,不讓它自動提交
9
*2,進(jìn)行手動提交(commit)
10
*3,提交完成后回復(fù)現(xiàn)場將Auto commit,還原為true,
11
*4,當(dāng)異常發(fā)生執(zhí)行catch中SQLException時,記得要rollback(回滾);
12
* */
13
public class StatementCommitAndRollbackTest {
14
public static void main(String args[]) {
15
Connection con = null;
16
Statement stm = null;
17
try {
18
con = JDBCConAndClo.getConnectionBao();
19
stm = con.createStatement();
20
con.setAutoCommit(false);
21
// 若不出現(xiàn)異常,則繼續(xù)執(zhí)行到try語句完,否則跳轉(zhuǎn)到catch語句中
22
stm.addBatch("insert into student values(23,'tangbao','高數(shù)',100)");
23
stm.addBatch("insert into student values(24,'王定','c#',98)");
24
stm.addBatch("insert into student values(25,'王國云','java',90)");
25
stm.addBatch("insert into student values(26,'溜出','英語',89)");
26
stm.addBatch("insert into student values(27,'wqde','java',63)");
27
/*
28
* int[] executeBatch() throws
29
* SQLException將一批命令提交給數(shù)據(jù)庫來執(zhí)行,如果全部命令執(zhí)行成功,則返回更新計數(shù)組成的數(shù)組。
30
*/
31
stm.executeBatch();
32
System.out.println("插入成功!");
33
// commit:若成功執(zhí)行完所有的插入操作,則正常結(jié)束
34
con.commit();
35
System.out.println("提交成功!");
36
con.setAutoCommit(true);
37
38
} catch (SQLException e) {
39
e.printStackTrace();
40
try {
41
//rollback: 若出現(xiàn)異常,對數(shù)據(jù)庫中所有已完成的操作全部撤銷,則回滾到事務(wù)開始狀態(tài)
42
if (!con.isClosed()) {
43
con.rollback();
44
System.out.println("提交失敗,回滾!");
45
con.setAutoCommit(true);
46
}
47
} catch (SQLException e1) {
48
e1.printStackTrace();
49
} finally {
50
JDBCConAndClo.closeStatement(stm);
51
JDBCConAndClo.closeConnection(con);
52
}
53
}
54
}
55
}
56
PreparedStatement批量處理和事務(wù)代碼如下:
57
package com.ambow.day20.jdbc.JDBCTestCommitAndRollback;
58
import java.sql.Connection;
59
import java.sql.PreparedStatement;
60
import java.sql.SQLException;
61
import com.ambow.day19.jdbc.util.JDBCConAndClo;
62
63
/*
64
* PreparedStatement:
65
1.addBatch() 將一組參數(shù)添加到 PreparedStatement對象內(nèi)部
66
2.executeBatch() 將一批參數(shù)提交給數(shù)據(jù)庫來執(zhí)行,如果全部命令執(zhí)行成功,則返回更新計數(shù)組成的數(shù)組。
67
*
68
*/
69
public class PreparedStatementCommitAndRollbackTest {
70
public static void main(String args[]) {
71
Connection con = null;
72
PreparedStatement pstm = null;
73
74
try {
75
// 1. 建立與數(shù)據(jù)庫的連接
76
con = JDBCConAndClo.getConnectionBao();
77
// 2. 執(zhí)行sql語句
78
// 1).先創(chuàng)建PreparedStatement語句(發(fā)送slq請求):
79
pstm = con.prepareStatement("insert into student values(?,?,?,?)");
80
con.setAutoCommit(false);//1,首先把Auto commit設(shè)置為false,不讓它自動提交
81
// 2) 設(shè)置sql語句1
82
pstm.setInt(1, 33);
83
pstm.setString(2,"wangqin");
84
pstm.setString(3, "c++");
85
pstm.setDouble(4, 78.5);
86
// 3) 將一組參數(shù)添加到此 PreparedStatement 對象的批處理命令中。
87
pstm.addBatch();
88
// 2) 設(shè)置sql語句2
89
pstm.setInt(1, 34);
90
pstm.setString(2,"wuytun");
91
pstm.setString(3, "c");
92
pstm.setDouble(4, 77);
93
// 3) 將一組參數(shù)添加到此 PreparedStatement 對象的批處理命令中。
94
pstm.addBatch();
95
// 2) 設(shè)置sql語句3
96
pstm.setInt(1, 31);
97
pstm.setString(2,"tetet");
98
pstm.setString(3, "c++");
99
pstm.setDouble(4, 90);
100
// 3) 將一組參數(shù)添加到此 PreparedStatement 對象的批處理命令中。
101
pstm.addBatch();
102
// 2) 設(shè)置sql語句4
103
pstm.setInt(1, 32);
104
pstm.setString(2,"liug");
105
pstm.setString(3, "c");
106
pstm.setDouble(4, 50);
107
// 3) 將一組參數(shù)添加到此 PreparedStatement 對象的批處理命令中。
108
pstm.addBatch();
109
// 4) 將一批參數(shù)提交給數(shù)據(jù)庫來執(zhí)行,如果全部命令執(zhí)行成功,則返回更新計數(shù)組成的數(shù)組。
110
pstm.executeBatch();
111
System.out.println("插入成功!");
112
// 若成功執(zhí)行完所有的插入操作,則正常結(jié)束
113
con.commit();//2,進(jìn)行手動提交(commit)
114
System.out.println("提交成功!");
115
con.setAutoCommit(true);//3,提交完成后回復(fù)現(xiàn)場將Auto commit,還原為true,
116
117
} catch (SQLException e) {
118
e.printStackTrace();
119
try {
120
// 若出現(xiàn)異常,對數(shù)據(jù)庫中所有已完成的操作全部撤銷,則回滾到事務(wù)開始狀態(tài)
121
if(!con.isClosed()){
122
con.rollback();//4,當(dāng)異常發(fā)生執(zhí)行catch中SQLException時,記得要rollback(回滾);
123
System.out.println("插入失敗,回滾!");
124
con.setAutoCommit(true);
125
}
126
} catch (SQLException e1) {
127
e1.printStackTrace();
128
}
129
}finally{
130
JDBCConAndClo.closePreparedStatement(pstm);
131
JDBCConAndClo.closeConnection(con);
132
}
133
}
134
}
135

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

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135
