不过切记一点,调用Function会出问题,一定要是存储过程,不然会挂掉,以此作备忘,害我搞了一半天的~~~~
java 代码
-
- ArrayDescriptor desc = ArrayDescriptor.createDescriptor("Oracle中自定义数据类型", conn);
- ARRAY array = new oracle.sql.ARRAY(desc, conn,传入 数据);
- oracleCallableStatement.setARRAY(1, array);
java 代码
- 当需要把插入多条数据的行为合并为一个事务时,可以考虑把一个list型数据作为参数传入存储过程,
- 可以调用jdbc内部实现类来实现,这些类在classes12.zip(oracle 8,别的版本可能是其他名字的zip包)。
- 如:
- 需要将一个list传入存储过程。
- 具体操作如下:
- 1,建立数据库对象来映射list数据类型。
- --定义一个与list中各元素的数据类型相同的数据库对象
- CREATE TYPE AOBJECT AS OBJECT(
- aaaa NUMBER(8),
- bbbb NUMBER(8)
- )
- /
- --定义一个list数据库对象
- CREATE TYPE ALIST AS VARRAY(100) OF AOBJECT
- /
-
- 2,具体java代码如下:
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.util.ArrayList;
-
-
-
-
- import oracle.jdbc.driver.OracleCallableStatement;
- import oracle.sql.ARRAY;
- import oracle.sql.ArrayDescriptor;
- import oracle.sql.STRUCT;
- import oracle.sql.StructDescriptor;
-
- .......
-
- public static long addRecords(ArrayList list) {
- Connection con = null;
- CallableStatement stmt = null;
- int backVal = 0;
- try {
- con = pool.getConnection();
- if (con != null) {
- ARRAY aArray = getArray(con, "AOBJECT","ALIST", list);
- stmt = con.prepareCall("{call produce1(?,?)}");
- ((OracleCallableStatement) stmt).setARRAY(1, aArray);
- stmt.registerOutParameter(2, java.sql.Types.INTEGER);
- stmt.execute();
- backVal = stmt.getInt(2);
- }
- } catch (Exception e) {
- ....
- } finally {
- ....
- }
- return backVal;
- }
-
- private static ARRAY getArray(Connection con, String OracleObj,
- String Oraclelist, ArrayList objlist) throws Exception {
- ARRAY list = null;
- if (objlist != null && objlist.size() > 0) {
- StructDescriptor structdesc = new StructDescriptor(OracleObj, con);
- STRUCT[] structs = new STRUCT[objlist.size()];
- Object[] result = new Object[0];
- for (int i = 0; i < objlist.size(); i++) {
- result = new Object[2];
- result[0] = new Integer(..);
- result[1] = new Integer(..);
-
- structs[i] = new STRUCT(structdesc, con, result);
- }
- ArrayDescriptor desc = ArrayDescriptor.createDescriptor(Oraclelist,
- con);
- list = new ARRAY(desc, con, structs);
- }
- return list;
- }
|