Oracle9 XML SQL Utility Technical Guidance (Favorites) (Reposted)
Oracle9 XML SQL Utility Technical Guidance (Java)
■ Pre-development requirements ■ SELECT XML format data ■ INSERT XML format data ■ Updata XML format data ■ delete XML format data
Before the development, you must install the Oracle client to point ClassPath to classpath111.zipxmlparserv2.jarxsu111.jarxsu12.jar
SELECT XML format data has similar hierarchical data relationships between the two, and the relational database mode can simulate. Assuming a database of a book list, the BookList table has the following columns: BookID, Title, Author, Publisher, Year, ISBN, and description. The following is a typical query for the application for the database: SELECT TITLE, AUTHOR, PUBLISHER, YEAR, ISBN from Booklist where bookid = 1234; If you submit a query by orle XML SQL Utility, the database will return the following results: XML Version = ' '1.0'?>
If the Oracle XML parser is sent directly to the XSLT processor, you can output it in the form of a DOM object. Providing the DOM output can not be required without parsing, otherwise implement parsing operations before applying XSL conversion. As shown in the following code segment, by passing the query to the oranle.xml.sql.Query.OrCle XML Query class, IMPORT JAVA. SQL. *; Import java. Math. *; Import oracle. XML. SQL .query . *; import oracle. jdbc. driver. *; public static void main (string args []) throws sqlexception {string tabname = "booklist"; string user = "scott / tiger"; DriverManager. registerDriver (new oracle jdbc .driver .oracleDriver ().); Connection conn = DriverManager getConnection. ( "jdbc: oracle: oci8" user "@"); OracleXMLQuery qry = new OracleXMLQuery (conn, "select * from tabName" ); String xmlstring = QRY. GetXmlstring (); system. Out .println ("Output is: / n" xmlstring "); conn. Close (); Oracle XML SQL Utility also provides an optional command line interface, which is used The DTD associated with special database mode is generated. Assume that all content is installed correctly, you only need to perform the following command to get the full command line selection list: Java OracleXML The following command line is used to create a special database that is inquiry Mode-related DTD: Java OracleXML getXml? Scott / Tiger "Scott / Tiger" -withdtd "Select" * from booklist "For the BookList table, the Oracle XML SQL Utility generated in the XML document generated in the query will output the following DTD:
Insert XML Format Data Once a pattern is created in the database, as long as the data in the XML format is consistent with DTD generated from the mode, the XML SQL Utility saves the data to the mode. XML SQL Utility provides the ability to map XML documents to table rows. This storage uses a simple mapping to map the element marker name as a column, convert the XML string into a suitable data type by default mapping. When the XML element has a child element, the XML element is mapped to the SQL object type. In order to save data in XML format, XML SQL Utility launches a plug-in statement, binds all element values in the VALUES clause of the plug statement. The content of each row element is mapped to a separate value collection. Return to the Booklist example in front of this chapter, below is the SQL statement of items that store XML format: Insert Into Booklist (Bookid, Title, Author, Publisher, Year, ISBN, Description) VALUS (?,?,?,?,?,? ?,) and BIND the values, BOOKID -> 1234TITLE -> The Difference Between God and Larry Ellison: Inside Oracle CorporationAUTHOR -> Mike WilsonPUBLISHER -> William Morrow & Co.YEAR -> 1997ISBN -> 0688149251Description -> Account of Larry Ellison; The following example code demonstrates how to complete this feature in the Java program: import oracle.xml.sql.dml. *; Import java.sql. *; Import oracle.jdbc.driver. *; Import oracle.jdbc. *; Import java.net *;. public class save_sample {public static void main (String args []) throw SQLException {Srting tabName = "bOOKLIST"; // table into which to insertString fileName = "samdoc.xml"; // file containing the xml docDriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver ()); Connection conn = DriverManager.getConnection ( "jdbc: oracle: oci8: scott / tiger @"); OracleXMLSave sav = new OracleXMLSave (conn, tabName); URL URL = sav.createurl (filename); int = sav.ins OrtXML (URL); System.out.println ("Success Insert" RowCount "Rows INTO" TabName); conn.close ();}} Like getXML, the storage function also has a command line version called PUTXML. It can be used to load XML data in large numbers. The following command will load an XML document, where the document contains a list of books that meet the example DTD. Java OracleXML PUTXML? User "Scott / Tiger" Sampdoc.xml Booklist
Updata XML Format Data Update Actions can be applied to multi-line in the table, which is different from insertion operations. If the matching column is not a key column in the table, the updated XML element may match multi-line. So update the operation requires a list of keyword columns, where the utility uses the list to determine the row to update. The following bibliographic list update example illustrates this situation:
The XML update executes SQL statement that passes the BookID column and keyword column value: update booklist set title =?, Author =? Where bookid =? And bind the value -> the Difference Between god and LARRY Ellison: Inside Oracle Corporation Author -> Mike Wilson; Note that you don't need to update all columns in the initial XML document. The following sample code gives the Java program how to complete this feature:
Import oracle.xml.sql.dml. *; import java.sql. *; import oracle.jdbc.driver. *; import oracle.jdbc. *; public class listupdate {public static void main (String Argv []) THROWS SQLEXCEPTION {String tabName = "bOOKLIST"; // table into which to insertDriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver ()); Connection conn = DriverManager.getConnection ( "jdbc: oracle: oci8: scott / tiger @") ; OracleXMLSave sav = new OracleXMLSave (conn, tabName); String [] keyColNames = new string [1]; keyColNames [0] = "BOOKID"; sav.setKeyColumnNames (keyColNames); sav.updateXML (argv [0]); sav .Close ();}}}
DELETE XML Format Data XML SQL Utility also supports delete operations of an XML document. Like the update operation, the delete operation also uses the keyword column to determine the deleted row. If one or more keywords are not given, the delete operation will still try the columns in the document. Below is the XML document and the corresponding SQL delete statement:
The following example shows how Java implements delete operations, where BookId is used as a key list:
Import oracle.xml.sql.dml. *; import java.sql. *; import oracle.jdbc.driver. *; import oracle.jdbc. *;
public class ListDelete {public static void main (String argv []) throws SQLException {String tabName = "BOOKLIST"; // table into which to delete data DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver ()); Connection conn = DriverManager.getConnection (jdbc: oracle: oci8: scott / tiger @ "); OracleXMLSave sav = new OracleXMLSave (conn, tabName); String [] keyColNames = new String [1]; keyColNames [0] =" BookID "; sav .SetKeyColmnnames (KeyColnames); sav.deletexml (Argv [0]); sav.close ();}}