锁定老帖子 主题:JDBC获取和操纵Oracle集合的方法
精华帖 (0) :: 良好帖 (2) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-02-05
最后修改:2012-02-07
上周需要在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开发指引文档下,总算搞清楚了。既然费劲找到原因了,不写篇东西记下,对不起自己!加上年纪大了,记性不好,记下以后好翻查,而且自己一直说想写写技术文章的,种种原因,导致我这么多废话的记下这篇东西。 setPlsqlIndexTable()
registerIndexTableOutParameter()
getOraclePlsqlIndexTable()
getPlsqlIndexTable()
把文档里的例子贴出来,看看也大概知道怎么使用。
// 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 ();
// 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);
// access the value using JDBC default mapping
BigDecimal[] values =
(BigDecimal[])funcnone.getPlsqlIndexTable (1);
// 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());
// access the value as a Java primitive array.
int[] values = (int[])
funcnone.getPlsqlIndexTable (1, java.lang.Integer.TYPE);
ArrayDescriptor arraydesc = ArrayDescriptor.createDescriptor
(sql_type_name, connection);
ARRAY array = new ARRAY(arraydesc, connection, elements);
直接看文档的示例,可能更清楚,如下。
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();
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));
}
3. 结论 供java调用的oracle过程不要使用index table,再封装个过程,使用其他集合类型或者其他类型参数代替。一般的话,我们使用的jdbc driver都是thin driver,纯java的driver,通socket实现。
后记
参考: 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-02-06
没有遇到过这种情况,先mark一下。
|
|
返回顶楼 | |
发表时间:2012-02-06
楼主能补充一些实际应用不?
|
|
返回顶楼 | |
发表时间:2012-02-07
应用应该比较少吧,所以网上的资料不多
|
|
返回顶楼 | |
发表时间:2012-02-07
lazybird86 写道
上周需要在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开发指引文档下,总算搞清楚了。既然费劲找到原因了,不写篇东西记下,对不起自己!加上年纪大了,记性不好,记下以后好翻查,而且自己一直说想写写技术文章的,种种原因,导致我这么多废话的记下这篇东西。 setPlsqlIndexTable()
registerIndexTableOutParameter()
getOraclePlsqlIndexTable()
getPlsqlIndexTable()
把文档里的例子贴出来,看看也大概知道怎么使用。
// 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 ();
// 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);
// access the value using JDBC default mapping
BigDecimal[] values =
(BigDecimal[])funcnone.getPlsqlIndexTable (1);
// 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());
// access the value as a Java primitive array.
int[] values = (int[])
funcnone.getPlsqlIndexTable (1, java.lang.Integer.TYPE);
ArrayDescriptor arraydesc = ArrayDescriptor.createDescriptor
(sql_type_name, connection);
ARRAY array = new ARRAY(arraydesc, connection, elements);
直接看文档的示例,可能更清楚,如下。
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();
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));
}
后记
参考:
|
|
返回顶楼 | |
发表时间:2012-02-07
最后修改:2012-02-07
foohsinglong 写道 楼主能补充一些实际应用不?
实际应用就是当你要在java调用oracle的存储过程,如果这个过程,有集合类型,那么就可以用这些方法。 如果是新建的存储过程,要供java层调用,不要使用index table这个类型,再封装个接口使用其他集合类型或者varchar2的字符序列代替。另外,自定义的类型,不要声明在包内,这样java层调用不到。 |
|
返回顶楼 | |
发表时间:2013-01-25
不是说ibatis中调用吗?怎么没见一行ibatis的配置呢?请问一下ibatis中如何调用存储过程返回自定义对象数组?
|
|
返回顶楼 | |
浏览 8940 次