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

注:本文来源于  深圳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 }

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值