日期:2014-05-16 浏览次数:20349 次
使用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(?)