JDBC获取和操纵Oracle集合的方法

      上周需要在ibatis里调用oracle的一个存储过程,这个存储过程有一个参数是index table,需要实现TypeHandler或者TypeHandlerCallback。上网查g.cn一下,发现没有多少文章介绍,只有少数不全的,倒是有一篇鬼佬写的,比较详细,但只介绍到nested table的情况。Oracle里面三种集合类型,分别是variable array、nested table、index table,前两种已经有例子,但index table呢,如何实现?这让挺伤脑筋的!很自然的想到,看JDBC规范,发现没有相应扩展类型的介绍。想想,各大数据库厂商,都应该是有自己的JDBC实现和使用文档的。于是,上Oracle官网,下了好几篇文档。于是,在JDBC 3.0和4.0规范,Oracle8i、Oracle9i和Oracle10g的JDBC开发指引文档下,总算搞清楚了。既然费劲找到原因了,不写篇东西记下,对不起自己!加上年纪大了,记性不好,记下以后好翻查,而且自己一直说想写写技术文章的,种种原因,导致我这么多废话的记下这篇东西。
     首先,你如果是使用JDK6.0的话,那么你很幸运。JDK 6.0支持JDBC 4.0,这让你在这个问题面前,不用多几条白头发。如果是JDK5.0或者之前的版本,像我公司使用的是1.4,那么你就纠结了,需要使用Oracle的JDBC扩展去实现,像index table这种情况,还要依赖于你使用的Oracle JDBC Driver。相信很多时候,我们都是纠结的。当然,可能还有其他方法处理,因为我自己本身这类数据库应用开发经验很少。
     1. 通过Oracle的JDBC扩展实现获取和操纵Oracle集合
     不管是variable array或者nested table,在Java对应的实现,都是为JDBC里的Array类型,这两种类型后面补充。要注意,只能处理命名的集合类型。
     1.1确定你使用的Oracle数据库的版本
     你的数据库版本决定你能否操纵的集合类型。在Oracle8i的文档里,只有对Array类型进行说明,标题为“Working with Arrays”,显示没有集合这个概念。而Oracle9i的文档是,有专门的“Working with Oracle Collections”。
当然,我没有细看,文档查阅的也有限。在有限的了解下,得出只有Oracle9i以上的情况下,才有可能对index table类型支持。
     1.2其次是Driver的版本
     必须是JDBC OCI Driver才能支持。这部分文档在JDBC OCI Extensions里有介绍。支持常用的PL SQL的整数和字符串类型,类型列表文档也有列出。
     1.3使用OCI Driver下内置方法
     前面第1、2点满足后,使用OraclePreparedStatement和OracleCallableStatement内置方法支持,如下:
    

setPlsqlIndexTable()
registerIndexTableOutParameter()
getOraclePlsqlIndexTable()
getPlsqlIndexTable()

 

    把文档里的例子贴出来,看看也大概知道怎么使用。
    1.3.1 对于IN参数

    

// Prepare the statement
OracleCallableStatement procin = (OracleCallableStatement)
conn.prepareCall ("begin procin (?); end;");
// index-by table bind value
int[] values = { 1, 2, 3 };
// maximum length of the index-by table bind value. This
// value defines the maximum possible "currentLen" for batch
// updates. For standalone binds, "maxLen" should be the
// same as "currentLen".
int maxLen = values.length;
// actual size of the index-by table bind value
int currentLen = values.length;
// index-by table element type
int elemSqlType = OracleTypes.NUMBER;
// index-by table element length in case the element type
// is CHAR, VARCHAR or RAW. This value is ignored for other
// types.
int elemMaxLen = 0;
// set the value
procin.setPlsqlIndexTable (1, values,
maxLen, currentLen,
elemSqlType, elemMaxLen);
// execute the call
procin.execute ();

 
    1.3.2 对于OUT参数

    

// maximum length of the index-by table value. This
// value defines the maximum table size to be returned.
int maxLen = 10;
// index-by table element type
int elemSqlType = OracleTypes.NUMBER;
// index-by table element length in case the element type
// is CHAR, VARCHAR or RAW. This value is ignored for other
// types
int elemMaxLen = 0;
// register the return value
funcnone.registerIndexTableOutParameter
(1, maxLen, elemSqlType, elemMaxLen);

     
    1.3.3 获取OUT参数的值
    有三个方法能获取index table(OUT参数)中的值,如下:
   (1) Object getPlsqlIndexTable (int paramIndex)
    该方法返回Object类型,强制类型转换为JDBC默认对应的Java类型。

   

// access the value using JDBC default mapping
BigDecimal[] values =
(BigDecimal[])funcnone.getPlsqlIndexTable (1);

 
   (2) Datum[] getOraclePlsqlIndexTable (int paramIndex)
    该方法返回Oracel JDBC的类型,oracle.sql.Datum,使用该类获取相应类型的值。

   

// access the value using Oracle JDBC mapping
Datum[] outvalues = funcnone.getOraclePlsqlIndexTable (1);
// print the elements
for (int i=0; i<outvalues.length; i++)
System.out.println (outvalues[i].intValue());

 
   (3) Object getPlsqlIndexTable (int paramIndex, Class primitiveType)
    该方法要指定对应的Java类型。

   

// access the value as a Java primitive array.
int[] values = (int[])
funcnone.getPlsqlIndexTable (1, java.lang.Integer.TYPE);

 
    2.  补充:variable array、nested table的操纵方法
    2.1 创建oracle.sql.ARRAY对象
    首先,需要 创建oracle.sql.ArrayDescriptor对象,该类型是用来描述Array类型的,在构造函数中需要指名类型名称。

    

ArrayDescriptor arraydesc = ArrayDescriptor.createDescriptor
(sql_type_name, connection);

 
    sql_type_name指定使用集合类型,connection使用当前连接。
    接着,就可以使用这个ArrayDescriptor对象,来创建对应的oracle.sql.ARRAY对象。

   

ARRAY array = new ARRAY(arraydesc, connection, elements);

 
    elements指定集合包含的值,可以是Java基本类型或者对象数组。

    直接看文档的示例,可能更清楚,如下。

   

Connection conn = ...; // make a JDBC connection
// create the collection types
Statement stmt = conn.createStatement ();
stmt.execute ("CREATE TYPE varray1 AS VARRAY(10) OF NUMBER(12, 2)"); // one
// layer
stmt.execute ("CREATE TYPE varray2 AS VARRAY(10) OF varray1"); // two layers
stmt.execute ("CREATE TYPE varray3 AS VARRAY(10) OF varray2"); // three layers
stmt.execute ("CREATE TABLE tab2 (col1 index, col2 value)");
stmt.close ();
// obtain a type descriptor of "SCOTT.VARRAY3"
ArrayDescriptor desc = ArrayDescriptor.createDescriptor("SCOTT.VARRAY3", conn);
// prepare the multi level collection elements as a nested Java array
int[][][] elems = { {{1}, {1, 2}}, {{2}, {2, 3}}, {{3}, {3, 4}} };
// create the ARRAY by calling the constructor
ARRAY array3 = new ARRAY (desc, conn, elems);
// some operations
...
// close the database connection
conn.close();

 
    2.2 获取Array对象的值
    主要有三个方法

   

getArray()
getOracleArray()
getResultSet()

 
    不用多说,直接上文档示例代码。

   

stmt.execute ("CREATE TYPE num_varray AS VARRAY(10) OF NUMBER(12, 2)");
stmt.execute ("CREATE TABLE varray_table (col1 num_varray)");
stmt.execute ("INSERT INTO varray_table VALUES (num_varray(100, 200))");
ResultSet rs = stmt.executeQuery("SELECT * FROM varray_table");
ARRAY my_array = ((OracleResultSet)rs).getARRAY(1);
// return the SQL type names, integer codes,
// and lengths of the columns
System.out.println ("Array is of type " + array.getSQLTypeName());
System.out.println ("Array element is of typecode " + array.getBaseType());
System.out.println ("Array is of length " + array.length());
// get Array elements
BigDecimal[] values = (BigDecimal[]) my_array.getArray();
for (int i=0; i<values.length; i++)
{
BigDecimal out_value = (BigDecimal) values[i];
System.out.println(">> index " + i + " = " + out_value.intValue());
}
 ResultSet rset = my_array.getResultSet();
while (rset.next())
{
// The first column contains the element index and the
// second column contains the element value
System.out.println(">> index " + rset.getInt(1)+" = " + rset.getInt(2));
}

 
    2.3 使用Array对象作为过程函数的参数
    很简单,直接使用OraclePreparedStatement.setARRAY或者OracleCallableStatement.registerOutParameter传入。

 

后记
     这篇东西上年就写了一点,回家过年,闲着没事就写完了。回来电脑又坏了,直到今天才想起贴出来。不是什么大作,记下东西,以前没有这习惯总结,现在争取想有空写点,练练笔头。

 

参考:
     Oracle JDBC JavaDoc
     Oracle9i JDBC Developer’s Guide and Reference
     Oracle8i JDBC Developer’s Guide and Reference

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值