设为首页 收藏本站
查看: 636|回复: 0

[经验分享] JDBC获取和操纵Oracle集合的方法

[复制链接]

尚未签到

发表于 2016-8-5 15:58:52 | 显示全部楼层 |阅读模式
        上周需要在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.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;
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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-253404-1-1.html 上篇帖子: 提高Oracle数据库安全性的几个建议 下篇帖子: Oracle OS认证、口令文件、密码丢失处理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表