利用JDBC中处理批量更新oracle数据
2014-07-13 20:27
621 查看
JDBC课的时候,听到一节是讲在利用JDBC中处理批量更新oracle数据时候的特性,让我很为JDBC的特性感的兴奋,利用这个特性可以在批量更新数据的时候不同往常一样每次都需要传送完成的SQL语句到数据库中。其中示范代码如下:
1 import java.sql.*;
2
3 public class BatchUpdates
4 {
5 public static void main(String[] args)
6 {
7 Connection conn = null;
8 Statement stmt = null;
9 PreparedStatement pstmt = null;
10 ResultSet rset = null;
11 int i = 0;
12
13 try
14 {
15 DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
16
17 String url = "jdbc:oracle:oci8:@";
18 try {
19 //检查是否配置JDBC环境变量
20 String url1 = System.getProperty("JDBC_URL");
21 if (url1 != null)
22 url = url1;
23 } catch (Exception e) {
24 //如果是在集成开发环境导入了JDBC的话可以注释这句
25 }
26
27 // 连接到数据库用 scott
28 conn = DriverManager.getConnection (url, "scott", "tiger");
29
30 stmt = conn.createStatement();
31 try { stmt.execute(
32 "create table mytest_table (col1 number, col2 varchar2(20))");
33 } catch (Exception e1) {}
34
35 //
36 // 批量插入新值.
37 //
38 pstmt = conn.prepareStatement("insert into mytest_table values (?, ?)");
39
40 pstmt.setInt(1, 1);
41 pstmt.setString(2, "row 1");
42 pstmt.addBatch();
43
44 pstmt.setInt(1, 2);
45 pstmt.setString(2, "row 2");
46 pstmt.addBatch();
47
48 pstmt.executeBatch();
49
50 //
51 // 查询 输出结构集
52 //
53 rset = stmt.executeQuery("select * from mytest_table");
54 while (rset.next())
55 {
56 System.out.println(rset.getInt(1) + ", " + rset.getString(2));
57 }
58 }
59 catch (Exception e)
60 {
61 e.printStackTrace();
62 }
63 finally
64 {
65 if (stmt != null)
66 {
67 try { stmt.execute("drop table mytest_table"); } catch (Exception e) {}
68 try { stmt.close(); } catch (Exception e) {}
69 }
70 if (pstmt != null)
71 {
72 try { pstmt.close(); } catch (Exception e) {}
73 }
74 if (conn != null)
75 {
76 try { conn.close(); } catch (Exception e) {}
77 }
78 }
79 }
80 }
1 import java.sql.*;
2
3 public class BatchUpdates
4 {
5 public static void main(String[] args)
6 {
7 Connection conn = null;
8 Statement stmt = null;
9 PreparedStatement pstmt = null;
10 ResultSet rset = null;
11 int i = 0;
12
13 try
14 {
15 DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
16
17 String url = "jdbc:oracle:oci8:@";
18 try {
19 //检查是否配置JDBC环境变量
20 String url1 = System.getProperty("JDBC_URL");
21 if (url1 != null)
22 url = url1;
23 } catch (Exception e) {
24 //如果是在集成开发环境导入了JDBC的话可以注释这句
25 }
26
27 // 连接到数据库用 scott
28 conn = DriverManager.getConnection (url, "scott", "tiger");
29
30 stmt = conn.createStatement();
31 try { stmt.execute(
32 "create table mytest_table (col1 number, col2 varchar2(20))");
33 } catch (Exception e1) {}
34
35 //
36 // 批量插入新值.
37 //
38 pstmt = conn.prepareStatement("insert into mytest_table values (?, ?)");
39
40 pstmt.setInt(1, 1);
41 pstmt.setString(2, "row 1");
42 pstmt.addBatch();
43
44 pstmt.setInt(1, 2);
45 pstmt.setString(2, "row 2");
46 pstmt.addBatch();
47
48 pstmt.executeBatch();
49
50 //
51 // 查询 输出结构集
52 //
53 rset = stmt.executeQuery("select * from mytest_table");
54 while (rset.next())
55 {
56 System.out.println(rset.getInt(1) + ", " + rset.getString(2));
57 }
58 }
59 catch (Exception e)
60 {
61 e.printStackTrace();
62 }
63 finally
64 {
65 if (stmt != null)
66 {
67 try { stmt.execute("drop table mytest_table"); } catch (Exception e) {}
68 try { stmt.close(); } catch (Exception e) {}
69 }
70 if (pstmt != null)
71 {
72 try { pstmt.close(); } catch (Exception e) {}
73 }
74 if (conn != null)
75 {
76 try { conn.close(); } catch (Exception e) {}
77 }
78 }
79 }
80 }
相关文章推荐
- .net中利用oracle产品自带的数据访问组件(Oracle.DataAccess.dll)提升批量更新操作的执行效率
- .net中利用oracle产品自带的数据访问组件(Oracle.DataAccess.dll)提升批量更新操作的执行效率
- oracle表数据批量更新或插入
- 第五章 利用数组处理批量数据
- Oracle笔记(七) 数据更新、事务处理、数据伪列
- oracle 中批量数据的处理(转载)
- 今晚我最得意之作——利用Oracle超级无敌强大函数生成“批量生成数据语句”!
- 6.利用数组处理批量数据
- Oracle数据的批量更新
- Oracle笔记(七) 数据更新、事务处理、数据伪列
- 利用JDBC操作Oracle CLOB和BLOB类型数据
- ORACLE 和SQLSERVER 两表之间批量更新数据对比
- 更新ORACLE数据时遇到锁死情况的处理
- JDBC第三章知识点总结——JDBC高级特性1--结果集,批量更新,高级数据类型
- Oracle笔记(七) 数据更新、事务处理、数据伪列
- 今晚我最得意之作——利用Oracle超级无敌强大函数生成“批量生成数据语句”!
- 利用“contentEditable” + jQuery 批量更新数据
- ORACLE 和SQLSERVER 两表之间批量更新数据对比
- oracle利用游标 往表中批量插入数据