78 stmt.addBatch("INSERT INTO product1(id, name) VALUES(2, 'B')");
79 stmt.addBatch("INSERT INTO product1(id, name) VALUES(3, 'C')");
80 stmt.addBatch("DELETE FROM product1");
81 // 4. 执行批量插入
82 int[] updateCounts = stmt.executeBatch();
83 // 5. 手动提交批量插入的数据到数据库服务器。
84 conn.commit();
85 }
86 } catch (Exception e) {
87 e.printStackTrace();
88 } finally {
89 try {
90 conn.close();
91 } catch (SQLException e) {
92 e.printStackTrace();
93 }
94 }
95 }
96 }
9. 执行存储过程:
1 public class MyTest {
2 private static Connection getConnection() throws SQLException,
3 ClassNotFoundException {
4 Class.forName("oracle.jdbc.driver.OracleDriver");
5 Properties conProps = new Properties();
6 conProps.put("user", "sys");
7 conProps.put("password", "helloworld");
8 conProps.put("internal_logon", "sysdba");
9 return DriverManager.getConnection(
10 "jdbc:oracle:thin:@//192.168.1.101:1526/OraHome", conProps);
11 }
12
13 public static void main(String[] args) throws ClassNotFoundException {
14 Connection conn = null;
15 Statement stmt = null;
16 try {
17 conn = getConnection();
18 //1. 创建函数或存储过程
19 stmt = conn.createStatement();
20 String function = "CREATE OR REPLACE FUNCTION myfuncinout("
+ "x IN VARCHAR,y OUT VARCHAR) RETURN VARCHAR IS "
21 + "BEGIN y:= x||'outvalue'; RETURN 'a returned string'; END;";
22 stmt.executeUpdate(function);
23 //2. 准备调用存储过程
24 CallableStatement cs = conn.prepareCall("{ = call myfuncinout( , )}");
25 //3. 注册返回值输出参数
26 cs.registerOutParameter(1, Types.VARCHAR);
27 cs.registerOutParameter(3, Types.VARCHAR);
28 //4. 设置输入参数
29 cs.setString(2, "hello");
30 //5. 执行存储过程
31 cs.execute();
32 String retValue = cs.getString(1); // return value
33 String outParam = cs.getString(3); // IN/OUT parameter
34 System.out.println("Return value = " + retValue);
35 System.out.println("Out value = " + outParam);
36
37 } catch (Exception e) {
38 e.printStackTrace();
39 } finally {
40 try {
41 stmt.close();
42 conn.close();
43 } catch (SQLException e) {
44 e.printStackTrace();
45 }
46 }
47 }
48 }
10. 获取存储过程的Metadata:
1 public class MyTest {
2 private static Connection getConnection() throws SQLExcept