Technical Guidance for xml SQL utility in oracle9 (Java)

Source: Internet
Author: User


Technical Guidance for xml SQL utility in oracle9 (Java)
Gui_jq original (Participation score: 686, expert score: 670) published: Version: 1.0 read:5775Times


■ Configuration required before development
■ Select XML format data
■ Insert XML format data
■ Updata XML format data
■ Delete XML format data

Configuration required before development
The Oracle client must be installed
Point classpath
Classes111.zip
Xmlparserv2.jar
Xsu111.jar
Xsu12.jar

Select XML format data
Since there is a similar hierarchical data relationship between the two, the relational database mode can be simulated using XML. Assume that a database for the bibliography list has the following columns: bookid, title, author, publisher, year, ISBN, and description. The following is the applicationProgramTypical queries to databases:
Select title, author, publisher, year, ISBN from booklist where bookid = 1234;
If you submit a query using Orale xml SQL utility, the database returns the following results:
<? XML version = ''1. 0 ''?>
<Rowset>
<Row id = "1">
<Title> the difference between God and Larry Ellison: Inside Oracle
Corporation </title>
<Author> Mike Wilson <author>
<Publtsher> William Morrow and Co. </publisher>
<Year> 1997 </year>
<ISBN> 0688149251 </ISBN>
</Rowset>
If the application simply writes the output to a file, it can be output in string format. For example

The result is directly sent to the Oracle XML Parser for transformation using the XSLT processor, which can be output in the form of DOM objects. The Dom output can be parsed without any need. Otherwise, the parsing operation must be performed before the XSL conversion is applied.
See the following Code The query is submitted by passing the query to the oranle. xml. SQL. query. Oracle XML query class:
Import java. SQL .*;
Import java. Math .*;
Import oracle. xml. SQL. query .*;
Import oracle. JDBC .*;
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 for generating DTD related to the special database mode. Assuming that all the content has been correctly installed, you only need to execute the following command to obtain the complete command line selection list:

Java oraclexml
The following command line is used to create a DTD related to the special database mode being queried:
Java oraclexml getxml? Scott/tiger "Scott/TIGER"-withdtd "select "*
 
From booklist"
For the booklist table mentioned above, Oracle xml SQL utility will output the following DTD in the XML document generated by this query:

<〈! Element booklist (bookid, title, author, publisher, year, ISBN, description)> )〉
<! Element bookid (# pcdata)>
<! Element author (# pcdata)>
<! Element publisher (# pcdata)>
<! Element year (# pcdata)>
<Element ISBN (# ppcdata)>
<! Element description (# pcdata)>

Insert XML format data
Once a mode is created in the database, the xml SQL utility saves the data to the mode as long as the data in the XML format is consistent with the DTD generated in the slave mode. Xml SQL utility provides the ability to map XML documents to table rows. The storage uses simple ing to map element tag names to columns, and converts XML strings to appropriate data types through default ing. When an XML element has child elements, the XML element is mapped to the SQL object type.
To save data in XML format, the xml SQL utility starts an insert statement and binds all element values to the values clause of the insert statement. The content of each row element is mapped to a separate value set.
Return to the booklist example above this chapter. The following is the SQL statement for storing items in XML format:
Insert into booklist (bookid, title, author, publisher, year, ISBN, description) valus (?,?,?,?,?,?,?) And bind the values,
Bookid:> 1234
Title-> the difference between God and Larry Ellison: Inside Oracle Corporation
Author-> Mike Wilson
Publisher-> William Morrow & Co.
Year-> 1997
ISBN-> 0688149251
Description-> account of Larry Ellison;
The following sample code demonstrates how to complete this function in a 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 insert
String filename = "samdoc. xml"; // file containing the XML Doc
Drivermanager. 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 rowcount = Sav. insertxml (URL );
System. Out. println ("successfully inserted" + 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 a large amount of XML data. The following command loads an XML document that contains a list of bibliography that matches the sample DTD.
Java oraclexml putxml? User "Scott/TIGER" sampdoc. xml booklist

Updata XML format data
The update operation can be applied to multiple rows in the table, which is different from the insert operation. If the matching column is not a keyword column in the table, the updated XML element may match multiple rows. Therefore, the update operation requires a list of keyword columns, in which the utility uses this list to determine the row to be updated. The following example shows how to update the Bibliography:



1234
the difference between God and Larry ellison: inside Oracle <br> Corporation
Mike Wilson
William Morrow and Co.
1997
0688149251

This XML update executes the following SQL statement that passes the bookid column and keyword column value:
Update booklist set Title =?, Author =? Where bookid =?
and bind the values,
bookid-> 1234
title-> the difference between God and Larry Ellison: inside
Oracle Corporation
author-> Mike Wilson;
note that you do not need to update all columns in the original XML document. The following sample code shows how a Java program completes this function:

Import oracle. XML. SQL. DML. *;
Import Java. SQL. *;
Import oracle. JDBC. driver. *;
Import oracle. JDBC. *;
public class listupdate
{< br> Public static void main (string argv []) throws sqlexception
{< br> string tabname = "Booklist"; // table into which to insert
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. setkeycolumnnames (keycolnames);
Sav. updatexml (argv [0]);
Sav. close ();
}< BR >}

Delete XML format data
xml SQL utility also supports deletion of XML documents. Like the update operation, the delete operation also uses the keyword column to determine the row to be deleted. If one or more keyword columns are not given, the delete operation will still try to match the columns in the document. The XML document and the corresponding SQL deletion statement are as follows:



1234
the difference between God and Larry ellison: inside Oracle <br> Corporation
Mike Wilson
William Morrow and Co.
1997
0688149251

Delete from booklist WHRE Title =? And author =? And publisher =? And year =? And ISBN =? And bookid =?
Binding,
The bookid <-1234
Title <-the difference between God and Larry Ellison: Inside
Oracle Corporation
Author <-Mike Wilson
Publisher <-William Morrow & Co.
-Year <-1997
ISBN <-1, 0688149251

The following example shows how to perform the delete operation in Java, where bookid is used as the keyword column:

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.