zoukankan      html  css  js  c++  java
  • java怎样将一组对象传入Oracle存储过程

     java怎样将一组对象传入Oracle存储过程。须要注意的是jar包是有要求的,假设使用不当会导致仅仅有数字能传过去,字符串传只是去。假设是Oracle11g则须要选用例如以下的jar包,F:appAdministratorproduct11.2.0dbhome_1jliborai18n.jar、

    D:programweblogicoracle_commonmodulesoracle.jdbc_11.2.0ojdbc6.jar

    样例例如以下:

    CREATE OR REPLACE TYPE TEST_OBJECT AS OBJECT 
    (
      id number,
      name varchar2(32)
    );
    CREATE OR REPLACE TYPE TABLES_ARRAY AS VARRAY(100) OF TEST_OBJECT;
    drop table test purge;
    create table test
    (
      id number,
      name varchar2(32)
    );
    create or replace procedure t_list_to_p(arr_t in tables_array) 
    is
    begin
      for i in 1..arr_t.count loop
          insert  into test values(arr_t(i).id,arr_t(i).name);
      end loop;
      commit;
    end t_list_to_p;


    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    import oracle.sql.ARRAY;
    import oracle.sql.ArrayDescriptor;
    import oracle.sql.STRUCT;
    import oracle.sql.StructDescriptor;
    
    public class TestListToProcedure {
        static final String driver_class  = "oracle.jdbc.driver.OracleDriver";
        static final String connectionURL = "jdbc:oracle:thin:@10.150.15.150:1521:orcl";
        static final String userID        = "test";
        static final String userPassword  = "test";
        public void runTest() {
            Connection  con = null;
            CallableStatement stmt = null ;
            try {
                Class.forName (driver_class).newInstance();
                con = DriverManager.getConnection(connectionURL, userID, userPassword);
                StructDescriptor tDescriptor = StructDescriptor.createDescriptor("TEST_OBJECT", con);
            	List<STRUCT> structs = new ArrayList<STRUCT>();
            	Object[] 	tObject  = null ;
            	//能够将系统中VO,DTO转化成Object对象,先创建struts
            	for(int i = 0; i<10; i++){
            		tObject = new Object[2];
            		tObject[0] = i;
            		tObject[1] = "name"+i;
                    STRUCT tStruct = new STRUCT(tDescriptor, con, tObject);
                    structs.add(tStruct);   		
            	}
            	ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("TABLES_ARRAY", con);
            	ARRAY tArray = new ARRAY(arrayDescriptor, con, structs.toArray());
                stmt = con.prepareCall("{call t_list_to_p(?

    )}"); stmt.setArray(1, tArray); stmt.execute(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }finally{ if(stmt != null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(con != null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public static void main(String[] args) { TestListToProcedure testListToProcedure = new TestListToProcedure(); testListToProcedure.runTest(); } }




  • 相关阅读:
    基于jquery的弹幕实现
    Cookie在顶级域名、二级域名和三级域名之间共享的情况
    报错:Win10 这台计算机中已经安装了 .NET Framework 4.5.2/4.6.1/4.7.1等等任何版本 或版本更高的更新
    Unity中的Text内容有空格导致换行
    逆波兰表达式
    Java基础03 byte[] 与 16进制字符串之间的转换
    nacos Linux 单机模式配置
    Oracle 常用SQL
    软件安装01 Linux下redis安装
    Java基础04 JSONObject 与范型对象转换
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6740929.html
Copyright © 2011-2022 走看看