现在有表:
create table gps(
id int primary key,
name varchar(32)
);name为一数组,使用存储过程怎么实现将数组里的值全都插入到ORACLE中

解决方案 »

  1.   


    简单点,不需要存储过程啊,你代码里面数组 变成字符串,中间用,串联起来,存入name字段中,不就可以了吗。比如insert into gps(id, name) values(11312, 'sky,tian,key,view'); commit;
    不就搞定了吗?
      

  2.   

    如果你的name数组 对应多条记录的话。你就需要在存储过程里面,获取name相对应的id值。然后通过for循环的方式,insert,然后统一commit;
      

  3.   

    package com.jdbc.procedure;import java.sql.Connection;
    import java.sql.DriverManager;import oracle.jdbc.driver.OracleCallableStatement;
    import oracle.sql.ARRAY;
    import oracle.sql.ArrayDescriptor;
    //主要利用oracle API实现java数组与oracle集合之间的映射
    public class procedure01 { public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(
    "jdbc:oracle:thin:@localhost:1521:ora", "dj", "123456");

    // 创建String[]
    int[] arr = { 1, 2 };
    //利用oracle API
    OracleCallableStatement stmt = (OracleCallableStatement) conn
    .prepareCall("{call testoracle(?)}");
    // ARRAY(ArrayDescriptor, Connection, Object)
    //创建ArrayDescriptor,其中第1个参数是对应oracle中的嵌套表类型
    ArrayDescriptor desc = ArrayDescriptor
    .createDescriptor("STRINGS", conn);
    //创建ARRAY对象,将java数组传入
    ARRAY array = new ARRAY(desc, conn, arr);
    //设置存储过程参数
    stmt.setARRAY(1, array);
    stmt.execute();
    //close ...
    }}
    /*
     * --oracle部分
    create or replace package pkg_testoracle
    as
     type index_type is table of varchar2(10) index by binary_integer;end pkg_testoracle;create type strings is table of varchar2(10);--nested table--create table
    drop table test;
    create table test (name varchar2(10));--procedure
    create or replace procedure testoracle(param in strings)--param is nested table
    as
    begin
           for i in param.first .. param.last loop
               insert into test values(param(i));
           end loop;
           commit;
    end testoracle;*/