码迷,mamicode.com
首页 > 数据库 > 详细

java如何将一个List传入Oracle存储过程

时间:2018-04-09 21:11:25      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:lex   des   line   import   exe   script   value   cat   https   

注:本文来源于  深圳gg  java如何将一个List传入Oracle存储过程   》

 

一:数据库端建一个PL/SQL的数组。

  1 CREATE OR REPLACE TYPE tables_array AS VARRAY(100) OF VARCHAR2(32) ;
  2 
  3 drop table test purge;
  4 create table test
  5 (
  6   name varchar2(32)
  7 );
  8 
  9 
 10 create or replace procedure t_list_to_p(arr_t in tables_array) is
 11 begin
 12   for i in arr_t.first .. arr_t.last loop
 13     insert  into test values(arr_t(i));
 14   end loop;
 15   commit;
 16 end t_list_to_p;

 

二:java代码:

  1  import java.sql.CallableStatement;
  2 import java.sql.Connection;
  3 import java.sql.DriverManager;
  4 import java.sql.SQLException;
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 
  8 import oracle.sql.ARRAY;
  9 import oracle.sql.ArrayDescriptor;
 10 
 11 
 12 public class TestListToProcedure {
 13     static final String driver_class  = "oracle.jdbc.driver.OracleDriver";
 14     static final String connectionURL = "jdbc:oracle:thin:@10.150.15.150:1521:orcl";
 15     static final String userID        = "test";
 16     static final String userPassword  = "test";
 17     public void runTest() {
 18         Connection  con = null;
 19         CallableStatement stmt = null ;
 20         try {
 21             Class.forName (driver_class).newInstance();
 22             con = DriverManager.getConnection(connectionURL, userID, userPassword);
 23             stmt = con.prepareCall("{call t_list_to_p(?)}");
 24             ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("TABLES_ARRAY",con);
 25             List list = new ArrayList();
 26             list.add("张三");
 27             list.add("李四");
 28             list.add("王五");
 29             ARRAY array = new ARRAY(descriptor,con,list.toArray());
 30             stmt.setArray(1, array);
 31             stmt.execute();
 32         }  catch (SQLException e) {
 33             e.printStackTrace();
 34         } catch (Exception e) {
 35             e.printStackTrace();
 36         }finally{
 37         	if(stmt != null){
 38         		try {
 39 					stmt.close();
 40 				} catch (SQLException e) {
 41 					e.printStackTrace();
 42 				}
 43         	}
 44         	if(con != null){
 45         		try {
 46         			con.close();
 47 				} catch (SQLException e) {
 48 					e.printStackTrace();
 49 				}
 50         	}
 51         }
 52     }
 53 
 54     public static void main(String[] args) {
 55     	TestListToProcedure testListToProcedure = new TestListToProcedure();
 56     	testListToProcedure.runTest();
 57     }
 58 
 59 }

 

 

 

 

 

 

 

 

 

 

java如何将一个List传入Oracle存储过程

标签:lex   des   line   import   exe   script   value   cat   https   

原文地址:https://www.cnblogs.com/ios9/p/8762178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!