- 浏览: 151279 次
- 性别:
- 来自: 北京
最新评论
-
pandengzhegt:
好牛!正需要!谢谢了!
JPA 2.0 中的动态类型安全查询 -
yanlp:
万分的感谢。
仿google 的输入提示框 -
huangwenji6111:
可谓良师,在此拜谢!受益匪浅!
hibernate lazy -
jwx0925:
不错!mark!
hibernate对象状态 -
leftstick:
大有裨益,谢了!
hibernate lazy
SQL批处理是JDBC性能优化的重要武器,经本人研究总结,批处理的用法有三种。
package lavasoft.jdbctest;
import lavasoft.common.DBToolkit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
* JDBC的批量操作三种方式
*/
public class BatchExeSQLTest {
public static void main(String[] args) {
exeBatchStaticSQL();
}
/**
* 批量执行预定义模式的SQL
*/
public static void exeBatchParparedSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
String sql = "insert into testdb.book (kind, name) values (?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "java");
pstmt.setString(2, "jjjj");
pstmt.addBatch(); //添加一次预定义参数
pstmt.setString(1, "ccc");
pstmt.setString(2, "dddd");
pstmt.addBatch(); //再添加一次预定义参数
//批量执行预定义SQL
pstmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
/**
* 批量执行混合模式的SQL、有预定义的,还有静态的
*/
public static void exeBatchMixedSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
String sql = "insert into testdb.book (kind, name) values (?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "java");
pstmt.setString(2, "jjjj");
pstmt.addBatch(); //添加一次预定义参数
pstmt.setString(1, "ccc");
pstmt.setString(2, "dddd");
pstmt.addBatch(); //再添加一次预定义参数
//添加一次静态SQL
pstmt.addBatch("update testdb.book set kind = 'JAVA' where kind='java'");
//批量执行预定义SQL
pstmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
/**
* 执行批量静态的SQL
*/
public static void exeBatchStaticSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
Statement stmt = conn.createStatement();
//连续添加多条静态SQL
stmt.addBatch("insert into testdb.book (kind, name) values ('java', 'java in aciton')");
stmt.addBatch("insert into testdb.book (kind, name) values ('c', 'c in aciton')");
stmt.addBatch("delete from testdb.book where kind ='C#'");
stmt.addBatch("update testdb.book set kind = 'JAVA' where kind='java'");
// stmt.addBatch("select count(*) from testdb.book"); //批量执行不支持Select语句
//执行批量执行
stmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
}
注意:JDBC的批处理不能加入select语句,否则会抛异常:
java.sql.BatchUpdateException: Can not issue SELECT via executeUpdate().
at com.mysql.jdbc.StatementImpl.executeBatch(StatementImpl.java:1007)
转载自:http://lavasoft.blog.51cto.com/62575/238651
批量更新,插入代码
1./**
2. * 更新数据库已有的customer信息
3. * @param List<CustomerBean>
4. * @return
5. */
6.public int updateExistsInfo(List<CustomerBean> updateList){
7.
8. //查询的SQL语句
9. String sql = "update t_customer set LICENSE_KEY=?,CORPORATE_NAME=?,INTEGRATED_CLASSIFICATION=?,BOSSHEAD=?," +
10. "CONTACT_PHONE=?,ORDER_FREQUENCY=?,CONTACT_ADDRESS=?,USER_ID=? where CUSTOMER_ID=?" ;
11.
12. //插入需要的数据库对象
13. Connection conn = null;
14. PreparedStatement pstmt = null;
15.
16. try {
17. conn = new DBSource().getConnection();
18.
19. //设置事务属性
20. conn.setAutoCommit(false);
21.
22. pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
23.
24. for(CustomerBean cbean : updateList){
25. pstmt.setString(1, cbean.getLicense_key());
26. pstmt.setString(2, cbean.getCorporate_name());
27. pstmt.setString(3, cbean.getIntegrated_classification());
28. pstmt.setString(4, cbean.getBosshead());
29. pstmt.setString(5, cbean.getContact_phone());
30. pstmt.setString(6, cbean.getOrder_frequency());
31. pstmt.setString(7, cbean.getContact_address());
32. pstmt.setInt (8, cbean.getUser_id());
33. pstmt.setInt (9, cbean.getCustomer_id());
34.
35. pstmt.addBatch();
36.
37. }
38. int[] tt = pstmt.executeBatch();
39. System.out.println("update : " + tt.length);
40.
41. //提交,设置事务初始值
42. conn.commit();
43. conn.setAutoCommit(true);
44.
45. //插入成功,返回
46. return tt.length;
47.
48. }catch(SQLException ex){
49. try{
50. //提交失败,执行回滚操作
51. conn.rollback();
52.
53. }catch (SQLException e) {
54. e.printStackTrace();
55. System.err.println("updateExistsInfo回滚执行失败!!!");
56. }
57.
58. ex.printStackTrace();
59. System.err.println("updateExistsInfo执行失败");
60.
61. //插入失败返回标志0
62. return 0;
63.
64. }finally {
65. try{
66. //关闭资源
67. if(pstmt != null)pstmt.close();
68. if(conn != null)conn.close();
69.
70. }catch (SQLException e) {
71. e.printStackTrace();
72. System.err.println("资源关闭失败!!!");
73. }
74. }
75.}
76.
77./**
78. * 插入数据中没有的customer信息
79. * @param List<CustomerBean>
80. * @return
81. */
82.public int insertNewInfo(List<CustomerBean> insertList){
83.
84. //查询的SQL语句
85. String sql = "insert into t_customer(CUSTOMER_ID," +
86. "LICENSE_KEY,CORPORATE_NAME,INTEGRATED_CLASSIFICATION,BOSSHEAD,CONTACT_PHONE," +
87. "ORDER_FREQUENCY,CONTACT_ADDRESS,USER_ID,CUSTOMER_NUM,CUSTOMER_CODING," +
88. "INVESTIGATION_TIME,SMS_REC_FLAG,WAP_FLAG,PRICE_GATHERING_FLAG,SOCIETY_STOCK_FLAG," +
89. "REGION_TYPE)" +
90. "VALUES(CUSTOMER.NEXTVAL," +
91. "?,?,?,?,?," +
92. "?,?,?,?,?," +
93. "TO_DATE(?,'YYYY-MM-DD'),?,0,0,0," +
94. "?)" ;
95.
96. //插入需要的数据库对象
97. Connection conn = null;
98. PreparedStatement pstmt = null;
99.
100. try {
101. conn = new DBSource().getConnection();
102.
103. //设置事务属性
104. conn.setAutoCommit(false);
105.
106. pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
107.
108. for(CustomerBean cbean : insertList){
109. pstmt.setString(1, cbean.getLicense_key());
110. pstmt.setString(2, cbean.getCorporate_name());
111. pstmt.setString(3, cbean.getIntegrated_classification());
112. pstmt.setString(4, cbean.getBosshead());
113. pstmt.setString(5, cbean.getContact_phone());
114. pstmt.setString(6, cbean.getOrder_frequency());
115. pstmt.setString(7, cbean.getContact_address());
116. pstmt.setInt(8, cbean.getUser_id());
117. pstmt.setString(9, "gyyc00000");//
118. pstmt.setString(10, "95000000");//
119. pstmt.setString(11, getToday());
120. pstmt.setInt(12, cbean.getSms_rec_flag());
121. pstmt.setInt(13, cbean.getRegion_type());
122.
123.
124. pstmt.addBatch();
125.
126. }
127. int[] tt = pstmt.executeBatch();
128. System.out.println("insert : " + tt.length);
129.
130. //提交,设置事务初始值
131. conn.commit();
132. conn.setAutoCommit(true);
133.
134. //插入成功,返回
135. return tt.length;
136.
137. }catch(SQLException ex){
138. try{
139. //提交失败,执行回滚操作
140. conn.rollback();
141.
142. }catch (SQLException e) {
143. e.printStackTrace();
144. System.err.println("insertNewInfo回滚执行失败!!!");
145. }
146.
147. ex.printStackTrace();
148. System.err.println("insertNewInfo执行失败");
149.
150. //插入失败返回标志0
151. return 0;
152.
153. }finally {
154. try{
155. //关闭资源
156. if(pstmt != null)pstmt.close();
157. if(conn != null)conn.close();
158.
159. }catch (SQLException e) {
160. e.printStackTrace();
161. System.err.println("资源关闭失败!!!");
162. }
163. }
164.}
/**
* 更新数据库已有的customer信息
* @param List<CustomerBean>
* @return
*/
public int updateExistsInfo(List<CustomerBean> updateList){
//查询的SQL语句
String sql = "update t_customer set LICENSE_KEY=?,CORPORATE_NAME=?,INTEGRATED_CLASSIFICATION=?,BOSSHEAD=?," +
"CONTACT_PHONE=?,ORDER_FREQUENCY=?,CONTACT_ADDRESS=?,USER_ID=? where CUSTOMER_ID=?" ;
//插入需要的数据库对象
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = new DBSource().getConnection();
//设置事务属性
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
for(CustomerBean cbean : updateList){
pstmt.setString(1, cbean.getLicense_key());
pstmt.setString(2, cbean.getCorporate_name());
pstmt.setString(3, cbean.getIntegrated_classification());
pstmt.setString(4, cbean.getBosshead());
pstmt.setString(5, cbean.getContact_phone());
pstmt.setString(6, cbean.getOrder_frequency());
pstmt.setString(7, cbean.getContact_address());
pstmt.setInt (8, cbean.getUser_id());
pstmt.setInt (9, cbean.getCustomer_id());
pstmt.addBatch();
}
int[] tt = pstmt.executeBatch();
System.out.println("update : " + tt.length);
//提交,设置事务初始值
conn.commit();
conn.setAutoCommit(true);
//插入成功,返回
return tt.length;
}catch(SQLException ex){
try{
//提交失败,执行回滚操作
conn.rollback();
}catch (SQLException e) {
e.printStackTrace();
System.err.println("updateExistsInfo回滚执行失败!!!");
}
ex.printStackTrace();
System.err.println("updateExistsInfo执行失败");
//插入失败返回标志0
return 0;
}finally {
try{
//关闭资源
if(pstmt != null)pstmt.close();
if(conn != null)conn.close();
}catch (SQLException e) {
e.printStackTrace();
System.err.println("资源关闭失败!!!");
}
}
}
/**
* 插入数据中没有的customer信息
* @param List<CustomerBean>
* @return
*/
public int insertNewInfo(List<CustomerBean> insertList){
//查询的SQL语句
String sql = "insert into t_customer(CUSTOMER_ID," +
"LICENSE_KEY,CORPORATE_NAME,INTEGRATED_CLASSIFICATION,BOSSHEAD,CONTACT_PHONE," +
"ORDER_FREQUENCY,CONTACT_ADDRESS,USER_ID,CUSTOMER_NUM,CUSTOMER_CODING," +
"INVESTIGATION_TIME,SMS_REC_FLAG,WAP_FLAG,PRICE_GATHERING_FLAG,SOCIETY_STOCK_FLAG," +
"REGION_TYPE)" +
"VALUES(CUSTOMER.NEXTVAL," +
"?,?,?,?,?," +
"?,?,?,?,?," +
"TO_DATE(?,'YYYY-MM-DD'),?,0,0,0," +
"?)" ;
//插入需要的数据库对象
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = new DBSource().getConnection();
//设置事务属性
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
for(CustomerBean cbean : insertList){
pstmt.setString(1, cbean.getLicense_key());
pstmt.setString(2, cbean.getCorporate_name());
pstmt.setString(3, cbean.getIntegrated_classification());
pstmt.setString(4, cbean.getBosshead());
pstmt.setString(5, cbean.getContact_phone());
pstmt.setString(6, cbean.getOrder_frequency());
pstmt.setString(7, cbean.getContact_address());
pstmt.setInt(8, cbean.getUser_id());
pstmt.setString(9, "gyyc00000");//
pstmt.setString(10, "95000000");//
pstmt.setString(11, getToday());
pstmt.setInt(12, cbean.getSms_rec_flag());
pstmt.setInt(13, cbean.getRegion_type());
pstmt.addBatch();
}
int[] tt = pstmt.executeBatch();
System.out.println("insert : " + tt.length);
//提交,设置事务初始值
conn.commit();
conn.setAutoCommit(true);
//插入成功,返回
return tt.length;
}catch(SQLException ex){
try{
//提交失败,执行回滚操作
conn.rollback();
}catch (SQLException e) {
e.printStackTrace();
System.err.println("insertNewInfo回滚执行失败!!!");
}
ex.printStackTrace();
System.err.println("insertNewInfo执行失败");
//插入失败返回标志0
return 0;
}finally {
try{
//关闭资源
if(pstmt != null)pstmt.close();
if(conn != null)conn.close();
}catch (SQLException e) {
e.printStackTrace();
System.err.println("资源关闭失败!!!");
}
}
}
Notice:
//设置事务属性
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
for(CustomerBean cbean : updateList){
pstmt.setString(1, cbean.getLicense_key());
...
pstmt.addBatch();
}
int[] tt = pstmt.executeBatch();
System.out.println("update : " + tt.length);
//提交,设置事务初始值
conn.commit();
conn.setAutoCommit(true);
...
package lavasoft.jdbctest;
import lavasoft.common.DBToolkit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
* JDBC的批量操作三种方式
*/
public class BatchExeSQLTest {
public static void main(String[] args) {
exeBatchStaticSQL();
}
/**
* 批量执行预定义模式的SQL
*/
public static void exeBatchParparedSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
String sql = "insert into testdb.book (kind, name) values (?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "java");
pstmt.setString(2, "jjjj");
pstmt.addBatch(); //添加一次预定义参数
pstmt.setString(1, "ccc");
pstmt.setString(2, "dddd");
pstmt.addBatch(); //再添加一次预定义参数
//批量执行预定义SQL
pstmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
/**
* 批量执行混合模式的SQL、有预定义的,还有静态的
*/
public static void exeBatchMixedSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
String sql = "insert into testdb.book (kind, name) values (?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "java");
pstmt.setString(2, "jjjj");
pstmt.addBatch(); //添加一次预定义参数
pstmt.setString(1, "ccc");
pstmt.setString(2, "dddd");
pstmt.addBatch(); //再添加一次预定义参数
//添加一次静态SQL
pstmt.addBatch("update testdb.book set kind = 'JAVA' where kind='java'");
//批量执行预定义SQL
pstmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
/**
* 执行批量静态的SQL
*/
public static void exeBatchStaticSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
Statement stmt = conn.createStatement();
//连续添加多条静态SQL
stmt.addBatch("insert into testdb.book (kind, name) values ('java', 'java in aciton')");
stmt.addBatch("insert into testdb.book (kind, name) values ('c', 'c in aciton')");
stmt.addBatch("delete from testdb.book where kind ='C#'");
stmt.addBatch("update testdb.book set kind = 'JAVA' where kind='java'");
// stmt.addBatch("select count(*) from testdb.book"); //批量执行不支持Select语句
//执行批量执行
stmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
}
注意:JDBC的批处理不能加入select语句,否则会抛异常:
java.sql.BatchUpdateException: Can not issue SELECT via executeUpdate().
at com.mysql.jdbc.StatementImpl.executeBatch(StatementImpl.java:1007)
转载自:http://lavasoft.blog.51cto.com/62575/238651
批量更新,插入代码
1./**
2. * 更新数据库已有的customer信息
3. * @param List<CustomerBean>
4. * @return
5. */
6.public int updateExistsInfo(List<CustomerBean> updateList){
7.
8. //查询的SQL语句
9. String sql = "update t_customer set LICENSE_KEY=?,CORPORATE_NAME=?,INTEGRATED_CLASSIFICATION=?,BOSSHEAD=?," +
10. "CONTACT_PHONE=?,ORDER_FREQUENCY=?,CONTACT_ADDRESS=?,USER_ID=? where CUSTOMER_ID=?" ;
11.
12. //插入需要的数据库对象
13. Connection conn = null;
14. PreparedStatement pstmt = null;
15.
16. try {
17. conn = new DBSource().getConnection();
18.
19. //设置事务属性
20. conn.setAutoCommit(false);
21.
22. pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
23.
24. for(CustomerBean cbean : updateList){
25. pstmt.setString(1, cbean.getLicense_key());
26. pstmt.setString(2, cbean.getCorporate_name());
27. pstmt.setString(3, cbean.getIntegrated_classification());
28. pstmt.setString(4, cbean.getBosshead());
29. pstmt.setString(5, cbean.getContact_phone());
30. pstmt.setString(6, cbean.getOrder_frequency());
31. pstmt.setString(7, cbean.getContact_address());
32. pstmt.setInt (8, cbean.getUser_id());
33. pstmt.setInt (9, cbean.getCustomer_id());
34.
35. pstmt.addBatch();
36.
37. }
38. int[] tt = pstmt.executeBatch();
39. System.out.println("update : " + tt.length);
40.
41. //提交,设置事务初始值
42. conn.commit();
43. conn.setAutoCommit(true);
44.
45. //插入成功,返回
46. return tt.length;
47.
48. }catch(SQLException ex){
49. try{
50. //提交失败,执行回滚操作
51. conn.rollback();
52.
53. }catch (SQLException e) {
54. e.printStackTrace();
55. System.err.println("updateExistsInfo回滚执行失败!!!");
56. }
57.
58. ex.printStackTrace();
59. System.err.println("updateExistsInfo执行失败");
60.
61. //插入失败返回标志0
62. return 0;
63.
64. }finally {
65. try{
66. //关闭资源
67. if(pstmt != null)pstmt.close();
68. if(conn != null)conn.close();
69.
70. }catch (SQLException e) {
71. e.printStackTrace();
72. System.err.println("资源关闭失败!!!");
73. }
74. }
75.}
76.
77./**
78. * 插入数据中没有的customer信息
79. * @param List<CustomerBean>
80. * @return
81. */
82.public int insertNewInfo(List<CustomerBean> insertList){
83.
84. //查询的SQL语句
85. String sql = "insert into t_customer(CUSTOMER_ID," +
86. "LICENSE_KEY,CORPORATE_NAME,INTEGRATED_CLASSIFICATION,BOSSHEAD,CONTACT_PHONE," +
87. "ORDER_FREQUENCY,CONTACT_ADDRESS,USER_ID,CUSTOMER_NUM,CUSTOMER_CODING," +
88. "INVESTIGATION_TIME,SMS_REC_FLAG,WAP_FLAG,PRICE_GATHERING_FLAG,SOCIETY_STOCK_FLAG," +
89. "REGION_TYPE)" +
90. "VALUES(CUSTOMER.NEXTVAL," +
91. "?,?,?,?,?," +
92. "?,?,?,?,?," +
93. "TO_DATE(?,'YYYY-MM-DD'),?,0,0,0," +
94. "?)" ;
95.
96. //插入需要的数据库对象
97. Connection conn = null;
98. PreparedStatement pstmt = null;
99.
100. try {
101. conn = new DBSource().getConnection();
102.
103. //设置事务属性
104. conn.setAutoCommit(false);
105.
106. pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
107.
108. for(CustomerBean cbean : insertList){
109. pstmt.setString(1, cbean.getLicense_key());
110. pstmt.setString(2, cbean.getCorporate_name());
111. pstmt.setString(3, cbean.getIntegrated_classification());
112. pstmt.setString(4, cbean.getBosshead());
113. pstmt.setString(5, cbean.getContact_phone());
114. pstmt.setString(6, cbean.getOrder_frequency());
115. pstmt.setString(7, cbean.getContact_address());
116. pstmt.setInt(8, cbean.getUser_id());
117. pstmt.setString(9, "gyyc00000");//
118. pstmt.setString(10, "95000000");//
119. pstmt.setString(11, getToday());
120. pstmt.setInt(12, cbean.getSms_rec_flag());
121. pstmt.setInt(13, cbean.getRegion_type());
122.
123.
124. pstmt.addBatch();
125.
126. }
127. int[] tt = pstmt.executeBatch();
128. System.out.println("insert : " + tt.length);
129.
130. //提交,设置事务初始值
131. conn.commit();
132. conn.setAutoCommit(true);
133.
134. //插入成功,返回
135. return tt.length;
136.
137. }catch(SQLException ex){
138. try{
139. //提交失败,执行回滚操作
140. conn.rollback();
141.
142. }catch (SQLException e) {
143. e.printStackTrace();
144. System.err.println("insertNewInfo回滚执行失败!!!");
145. }
146.
147. ex.printStackTrace();
148. System.err.println("insertNewInfo执行失败");
149.
150. //插入失败返回标志0
151. return 0;
152.
153. }finally {
154. try{
155. //关闭资源
156. if(pstmt != null)pstmt.close();
157. if(conn != null)conn.close();
158.
159. }catch (SQLException e) {
160. e.printStackTrace();
161. System.err.println("资源关闭失败!!!");
162. }
163. }
164.}
/**
* 更新数据库已有的customer信息
* @param List<CustomerBean>
* @return
*/
public int updateExistsInfo(List<CustomerBean> updateList){
//查询的SQL语句
String sql = "update t_customer set LICENSE_KEY=?,CORPORATE_NAME=?,INTEGRATED_CLASSIFICATION=?,BOSSHEAD=?," +
"CONTACT_PHONE=?,ORDER_FREQUENCY=?,CONTACT_ADDRESS=?,USER_ID=? where CUSTOMER_ID=?" ;
//插入需要的数据库对象
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = new DBSource().getConnection();
//设置事务属性
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
for(CustomerBean cbean : updateList){
pstmt.setString(1, cbean.getLicense_key());
pstmt.setString(2, cbean.getCorporate_name());
pstmt.setString(3, cbean.getIntegrated_classification());
pstmt.setString(4, cbean.getBosshead());
pstmt.setString(5, cbean.getContact_phone());
pstmt.setString(6, cbean.getOrder_frequency());
pstmt.setString(7, cbean.getContact_address());
pstmt.setInt (8, cbean.getUser_id());
pstmt.setInt (9, cbean.getCustomer_id());
pstmt.addBatch();
}
int[] tt = pstmt.executeBatch();
System.out.println("update : " + tt.length);
//提交,设置事务初始值
conn.commit();
conn.setAutoCommit(true);
//插入成功,返回
return tt.length;
}catch(SQLException ex){
try{
//提交失败,执行回滚操作
conn.rollback();
}catch (SQLException e) {
e.printStackTrace();
System.err.println("updateExistsInfo回滚执行失败!!!");
}
ex.printStackTrace();
System.err.println("updateExistsInfo执行失败");
//插入失败返回标志0
return 0;
}finally {
try{
//关闭资源
if(pstmt != null)pstmt.close();
if(conn != null)conn.close();
}catch (SQLException e) {
e.printStackTrace();
System.err.println("资源关闭失败!!!");
}
}
}
/**
* 插入数据中没有的customer信息
* @param List<CustomerBean>
* @return
*/
public int insertNewInfo(List<CustomerBean> insertList){
//查询的SQL语句
String sql = "insert into t_customer(CUSTOMER_ID," +
"LICENSE_KEY,CORPORATE_NAME,INTEGRATED_CLASSIFICATION,BOSSHEAD,CONTACT_PHONE," +
"ORDER_FREQUENCY,CONTACT_ADDRESS,USER_ID,CUSTOMER_NUM,CUSTOMER_CODING," +
"INVESTIGATION_TIME,SMS_REC_FLAG,WAP_FLAG,PRICE_GATHERING_FLAG,SOCIETY_STOCK_FLAG," +
"REGION_TYPE)" +
"VALUES(CUSTOMER.NEXTVAL," +
"?,?,?,?,?," +
"?,?,?,?,?," +
"TO_DATE(?,'YYYY-MM-DD'),?,0,0,0," +
"?)" ;
//插入需要的数据库对象
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = new DBSource().getConnection();
//设置事务属性
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
for(CustomerBean cbean : insertList){
pstmt.setString(1, cbean.getLicense_key());
pstmt.setString(2, cbean.getCorporate_name());
pstmt.setString(3, cbean.getIntegrated_classification());
pstmt.setString(4, cbean.getBosshead());
pstmt.setString(5, cbean.getContact_phone());
pstmt.setString(6, cbean.getOrder_frequency());
pstmt.setString(7, cbean.getContact_address());
pstmt.setInt(8, cbean.getUser_id());
pstmt.setString(9, "gyyc00000");//
pstmt.setString(10, "95000000");//
pstmt.setString(11, getToday());
pstmt.setInt(12, cbean.getSms_rec_flag());
pstmt.setInt(13, cbean.getRegion_type());
pstmt.addBatch();
}
int[] tt = pstmt.executeBatch();
System.out.println("insert : " + tt.length);
//提交,设置事务初始值
conn.commit();
conn.setAutoCommit(true);
//插入成功,返回
return tt.length;
}catch(SQLException ex){
try{
//提交失败,执行回滚操作
conn.rollback();
}catch (SQLException e) {
e.printStackTrace();
System.err.println("insertNewInfo回滚执行失败!!!");
}
ex.printStackTrace();
System.err.println("insertNewInfo执行失败");
//插入失败返回标志0
return 0;
}finally {
try{
//关闭资源
if(pstmt != null)pstmt.close();
if(conn != null)conn.close();
}catch (SQLException e) {
e.printStackTrace();
System.err.println("资源关闭失败!!!");
}
}
}
Notice:
//设置事务属性
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
for(CustomerBean cbean : updateList){
pstmt.setString(1, cbean.getLicense_key());
...
pstmt.addBatch();
}
int[] tt = pstmt.executeBatch();
System.out.println("update : " + tt.length);
//提交,设置事务初始值
conn.commit();
conn.setAutoCommit(true);
...
发表评论
-
java实现Tree
2012-07-10 09:59 875/****************************** ... -
Java正则表达式应用总结
2012-05-25 12:23 1095一、概述 正则表达式是Java处理字符串、文本的重要工具。 ... -
JdbcTemplate与事务
2012-05-17 17:42 1122JdbcTemplate与事务 上例中的JdbcTemplat ... -
Java编程中“为了性能”尽量要做到的一些地方
2012-05-09 17:59 922最近的机器内存又爆满 ... -
jconsole远程监控Java进程
2012-05-07 11:44 1073JDK中的工具jconsole可以很好地监控Java进程及其运 ... -
spring集成quartz
2012-04-16 15:56 2218首先,让spring框架运转起来,可以参看一下:ht ... -
JMX RMI 访问
2011-09-02 10:46 4512RMI(Remote Method Invocation) R ... -
采用开发框架quartz调度管理Job
2011-07-11 10:03 19541.所需要的第三方包:quartz-1.5.2.jarcom ... -
java类型转换
2011-05-20 17:13 910string和int之间的转换? 字符串转换成数据 ... -
java整型数与网络字节序的 byte[] 数组转换关系
2011-05-05 10:47 3777因工作需要在java和c/c++之间进行socket通信,而 ... -
线程安全总结(二)
2010-11-12 10:34 851站内很多人都问我,所谓线程的“工作内存”到底是个什么东西? ... -
java线程安全总结
2010-11-12 10:33 830java线程安全总结(二 ... -
ora-02289问题解决
2010-10-19 12:35 1639<id name="id" type ... -
log4j输出多个自定义日志文件
2010-05-12 10:28 1514<转>http://hi.baidu.com/ ... -
spring任务调度
2010-04-28 09:48 1406概述 在JDK 1.3以后的版本中,Java通过java.ut ... -
JDK线程池的使用
2010-04-07 16:35 1454一、简介 线程池类为 j ... -
Java文件操作
2010-02-06 15:29 872本文汇集常用文件操作方法,包括文件的建立/检查与删除,目录的建 ... -
[JMX一步步来] 6、Model Bean
2009-12-21 11:46 1213在上一节是用apache的commons-modeler来 ... -
[JMX一步步来] 5、用Apache的commons-modeler来辅助开发JMX
2009-12-21 11:45 1079一、前言 每一个MBean都要有一个接口,比如前面的Hello ... -
[JMX一步步来] 4、动态MBean:DynamicMBean
2009-12-21 11:37 1531一、前言 动态MBean是在运行期才定义它的属性和方法 ...
相关推荐
本文将详细阐述JDBC批处理的基本概念、优势以及如何在实际应用中使用。 一、批处理的概念 批处理是指一次性提交多个SQL语句到数据库进行执行,而不是逐个发送和执行。在JDBC中,批处理主要通过Statement或...
在Java数据库连接(JDBC)中,增删改查(CRUD)操作是与数据库交互的基础,事务处理、批处理和预编译SQL语句则是提高效率和保证数据一致性的关键技术。以下是对这些概念的详细说明: 1. **JDBC增删改查(CRUD)**:...
JDBC批处理的实现依赖于数据库驱动的实现,不同数据库的驱动可能会有不同的优化策略。深入研究JDBC源码,可以帮助我们理解批处理在底层是如何工作的,以及如何针对特定数据库优化性能。 ### 8. 工具支持 很多...
以下是JDBC操作步骤的详细解释: 1. **加载数据库驱动程序**: 在进行数据库操作之前,首先需要在Java环境中加载对应的数据库驱动。这通常涉及到将JDBC驱动的JAR文件添加到项目的类路径(classpath)中。对于MySQL...
**JDBC批处理操作** 批处理是提高数据库操作性能的一种方式,通过一次性提交多条SQL语句,减少了网络通信和数据库处理的开销。以下是使用Statement和PreparedStatement对象进行批处理的基本步骤: 1. **使用...
#### 三、事务与批处理混合使用 在实际应用中,通常会结合使用事务管理和批处理技术,以进一步提高性能并保证数据的一致性。 ```java Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager....
【IT十八掌徐培成】Java基础第23天的课程主要聚焦在JDBC操作数据库时的批量处理技术,特别是`addBatch`和`executeBatch`这两个关键方法。在Java中,当我们需要对数据库进行大量数据的插入、更新或删除操作时,传统的...
6. **批处理**:JDBC支持批处理,可以一次提交多个SQL语句,提高效率: ```java PreparedStatement pstmt = conn.prepareStatement("INSERT INTO myTable VALUES (?, ?)"); pstmt.setInt(1, 1); pstmt.setString...
保存大文本_分页_批处理”揭示了本文将要讨论的是Java数据库连接(Java Database Connectivity, JDBC)的基础知识,主要包括如何在数据库中处理大文本数据、实现分页查询以及利用批处理提高数据库操作效率这三个核心...
- 使用批处理:`pstmt.addBatch();`和`pstmt.executeBatch();` - 使用PreparedStatement代替Statement,减少解析SQL的时间。 - 关闭资源:`rs.close(); pstmt.close(); conn.close();`以释放数据库连接。 **7. ...
PrepareStatement是JDBC提供的一种预编译的SQL语句,它可以提高数据库操作的效率和安全性。本资源主要涵盖了使用JDBC PrepareStatement进行MySQL数据库操作的各种场景,包括基本的查询、更新以及批量处理。 首先,...
本篇将详细讲解如何使用JDBC来连接Oracle数据库,包括必备的环境配置、连接步骤、SQL操作以及异常处理。 ### 1. 环境配置 首先,确保你的开发环境中已经安装了以下组件: - Java Development Kit (JDK):JDBC是...
9. **批处理**:对于大量相似的SQL操作,JDBC提供了批处理功能,可以提高效率。通过`addBatch()`添加SQL语句,然后调用`executeBatch()`执行。 10. **元数据获取**:JDBC提供`DatabaseMetaData`接口,用于获取关于...
对于大量相似的SQL操作,JDBC支持批处理,可以提高性能。使用`addBatch()`添加SQL语句到批处理队列,然后用`executeBatch()`执行: ```java PreparedStatement pstmt = conn.prepareStatement("INSERT INTO ...
批处理导入SQL文件是数据库管理中的一个重要操作,它主要用于高效地执行大量SQL语句,比如在数据迁移、数据库初始化或更新时。批处理通过合并多个SQL命令为一个单一的处理单元,可以显著提高数据处理速度,减少网络...
- **JDBC驱动类型**:JDBC驱动分为四种类型,分别是Type 1、Type 2、Type 3和Type 4。其中,Type 4驱动是纯Java实现,无需依赖数据库厂商的本地库,性能最优,如Oracle的 Thin Driver。 - **JDBC连接流程**:加载...
- 使用批处理提高性能:`pstmt.addBatch()` 和 `pstmt.executeBatch();` - 使用连接池管理数据库连接,例如C3P0、Apache DBCP或HikariCP,以提高性能和资源利用率。 - 设置合理的超时和最大连接数,防止资源耗尽...
在Java中,通过使用JDBC的批处理功能,开发者可以有效地管理大量数据的导入和更新,从而提高应用的开发效率和运行效率。如果你正在处理这样的问题,这个“批量插入或更新.zip”文件可能包含有价值的示例和指导,帮助...
JDBC提供了一种标准的API,使得开发者能够用Java编写数据库应用程序,无论数据库是哪种类型,如MySQL、Oracle、SQL Server等。本自学文档将带你深入理解和掌握JDBC的核心概念和操作。 **1. JDBC基础知识** JDBC的...