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

[经验分享] Oracle XML DB -- Java DOM API for XMLType(1)

[复制链接]

尚未签到

发表于 2016-8-1 07:57:22 | 显示全部楼层 |阅读模式
  使用JDBC 访问XMLType 数据
  oracle.xdb.XMLType类的createXML()方法
   这是一个用于访问Oracle数据库,任何数据,包括在Oracle XML DB的XML文档的Java应用程序的基于SQL的方法。
  Java应用程序如何使用JDBC来访问在Oracle XML DB的XML文档
  JDBC的用户可以查询一个XMLType表,以获取一个JDBC的XMLType接口,支持通过SQL的XMLType数据类型支持的所有方法。在Java(JDBC)API 中 XMLType接口可以实现DOM文档的接口。
  ----------------------------------------------------------------------------
Example 12-1 XMLType Java: 使用JDBC 查询 XMLType 表
The following is an example that illustrates using JDBC to query an XMLType table:
 import oracle.xdb.XMLType;
  ...
 OraclePreparedStatement stmt = (OraclePreparedStatement)
 conn.prepareStatement("select e.poDoc from po_xml_tab e");
 ResultSet rset = stmt.executeQuery();
 OracleResultSet orset = (OracleResultSet) rset;
 while(orset.next())
 {
       // get the XMLType
       XMLType poxml = XMLType.createXML(orset.getOPAQUE(1));
       // get the XMLDocument as a string...
       Document podoc = (Document)poxml.getDOM();
  }
  --------------------------------------------------------------------------------
Example 12-2 XMLType Java: Selecting XMLType Data
You can select the XMLType data in JDBC in one of two ways:
    (1)Use the getClobVal(), getStringVal() or getBlobVal(csid) in SQL and get the result as an oracle.sql.CLOB, java.lang.String or oracle.sql.BLOB in Java. The following Java code snippet shows how to do this:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
 Connection conn =
    DriverManager.getConnection("jdbc:oracle:oci8:@", "scott", "tiger");
 OraclePreparedStatement stmt =
    (OraclePreparedStatement) conn.prepareStatement(
          "select e.poDoc.getClobVal() poDoc, "+
                  "e.poDoc.getStringVal() poString "+
          " from po_xml_tab e");
ResultSet rset = stmt.executeQuery();
OracleResultSet orset = (OracleResultSet) rset;
while(orset.next()){
// the first argument is a CLOB
oracle.sql.CLOB clb = orset.getCLOB(1);
// the second argument is a string..
// now use the CLOB inside the program
 }
(2) Use getOPAQUE() call in the PreparedStatement to get the whole XMLType instance, and use the XMLType constructor to construct an oracle.xdb.XMLType class out of it. Then you can use the Java functions on the XMLType class to access the data.
  import oracle.xdb.XMLType;
 ...
 OraclePreparedStatement stmt =
    (OraclePreparedStatement) conn.prepareStatement(
          "select e.poDoc from po_xml_tab e");
  ResultSet rset = stmt.executeQuery();
  OracleResultSet orset = (OracleResultSet) rset;
  // get the XMLType
  XMLType poxml = XMLType(orset.getOPAQUE(1));
  // get the XML as a string...
  String poString = poxml.getStringVal();
----------------------------------------------------------------------------
  Example 12-3 XMLType Java: 直接返回 XMLType 类型数据
该例显示使用getObject方法直接从ResultSet结果集获取XMLType数据。
这段代码是从ResultSet获取XMLType数据最简单的方式。
  import oracle.xdb.XMLType;
...
PreparedStatement stmt =  conn.prepareStatement(
          "select e.poDoc from po_xml_tab e");
ResultSet rset = stmt.executeQuery();
while(rset.next())
{
// get the XMLType
XMLType poxml = (XMLType)rset.getObject(1);
  // get the XML as a string...
String poString = poxml.getStringVal();
 }
----------------------------------------------------------------------------
  使用JDBC 进行数据库XML 文档存储操作。可以使用JDBC进行更新、插入、和删除XMLType数据。
 注意:
  extract(), transform(),和 existsNode()方法仅工作在ThickJDBC driver下。 Thin JDBC driver并不支持所有的oracle.xdb.XMLType方法。然而,如果你不使用oracle.xdb.XMLType类和OCIdriver,你可能失去与XML的智能处理相关的性能优势。
  Example 12-5 XMLType Java: Updating, Inserting, or Deleting XMLType Data
  Bind a CLOB or a string to an INSERT or UPDATE or DELETE statement, and use the XMLType constructor inside SQL to construct the XML instance:
  OraclePreparedStatement stmt =
    (OraclePreparedStatement) conn.prepareStatement(
     "update po_xml_tab set poDoc = XMLType(?) ");
  // the second argument is a string..
String poString = "<PO><PONO>200</PONO><PNAME>PO_2</PNAME></PO>";
  // now bind the string..
stmt.setString(1,poString);
stmt.execute();
Use the setObject() or setOPAQUE() call in the PreparedStatement to set the whole XMLType instance:
  import oracle.xdb.XMLType;
...
OraclePreparedStatement stmt =
    (OraclePreparedStatement) conn.prepareStatement(
        "update po_xml_tab set poDoc = ? ");
  // the second argument is a string
String poString = "<PO><PONO>200</PONO><PNAME>PO_2</PNAME></PO>";
XMLType poXML = XMLType.createXML(conn, poString);
  // now bind the string..
stmt.setObject(1,poXML);
stmt.execute();
------------------------------------------------------------------------------

运维网声明 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-251611-1-1.html 上篇帖子: Oracle's Web 2.0 interface coming soon 下篇帖子: oracle创建表空间,定义用户,用户授权
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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