Servlet and XML: complementary to each other (ii) (turn)

Source: Internet
Author: User
Tags db2 sql query stmt oracle database
Servlet|xml Listing 5. Printdomtree
if (Requestedsubtree.equalsignorecase ("author")) Printdomtree (author, out);    else if (Requestedsubtree.equalsignorecase ("lines")) Printdomtree (lines, out);    else if (requestedsubtree.equalsignorecase ("title")) Printdomtree (title, out); Else Printdomtree (doc, out);

If the subtree parameter is author, the result is:
<author> <last-name>Shakespeare</last-name> <first-name>William</first-name> <na Tionality>british</nationality> <year-of-birth>1564</year-of-birth> <year-of-death> 1616</year-of-death> </author>

If the subtree argument is title, the result is:
<title>sonnet 130</title>

You can view the HTML view of the full list or view the Java source files directly.
Docking with Database
Our final example is to generate XML from a database query. There are many ways to do this (see DeveloperWorks's article generating XML from the Data Store); For this example, we will use IBM's XML Extender for DB2 (see Resources). This free product allows you to store XML documents in DB2. Our query extracts these documents from DB2 and then sends them to the user.
If you use Oracle 8i instead of DB2, you will find that it claims to have similar functionality (see Resources). For databases that do not understand XML, you can store the XML document as a large character object (CLOB) and retrieve the document as a text block.
However, after you install the database, you need to complete the following three things to make this code work:
  1. First, change the Dbowner, Dbuserid, and dbpasswd variables to fit the system's appropriate values.
        /////////////////////////////////////////////////////////////////        //Be sure to change these three strings correctly, otherwise                              //  The   //servlet does not work.                                             //   //////////////////////////////////////////////////////        dbuserid = "xxxxxxxx";    dbpasswd = "xxxxxxxx" ;    dbowner = "xxxxxxxx";     


  2. Next, use the JDBC driver that is appropriate for your system. We are using DB2.
    static String Jdbcdriver = "COM.ibm.db2.jdbc.app.DB2Driver";    ... try {class.forname ("COM.ibm.db2.jdbc.app.DB2Driver"). newinstance ();    catch (Exception e) {System.out.println ("Can ' t get the driver!"); E.printstacktrace (); }


  3. If you want, you can get rid of the following SQL query statement. To simplify the example, only the entire XML document in the Order column of the Sales_order_view table is retrieved.
    We are here to hard-code the SQL statements, which can be more complex if the query is based on user input//restrictions. String query = "Select order from" + Dbowner + ". Sales_order_view";

In the service method, our servlet connects DB2, executes a query (the result is a set of XML documents), analyzes the query results, and writes the parsed data to the output stream. Listing 6 shows the parts of the code that are most closely related to this:
Listing 6. Xmlfromdb2.java
    //we are here to hard-code the SQL statements, which can be more complicated if you restrict the query based on user input     //.     string query = "Select order from" + Dbowner + ". Sales_order_view";     Res.setcontenttype ("Text/xml");    try      {         coninfo index = new ConInfo ();         Connection con = getcon (index);         statement stmt = Con.createstatement ( )         resultset rs = stmt.executequery (query);         ...        //Displays the result set. We extract the XML document from each row,         //it, and then print the DOM tree. When there are no more rows, Rs.next () returns         //false.         while (Rs.next ()) &NBSP;&NBSP;&NBSP;&NBSP;&NBsp;     {              string Nextorder = rs.getstring (1). Trim ();             document doc = null;             StringReader sr = new StringReader (nextorder);             inputsource isrc = new InputSource (SR);                         try               {                 parser.parse (ISRC);                 doc = Parser.getdocument ();          &Nbsp;    }            catch ( Exception e)               {                 system.err.println ("Sorry, An error occurred: "+ e);               }                          if (Doc!= null)                printdomtree (Doc, out);           }

For full details, you can view the HTML view of the full list or view the Java source file directly.
Summary
Although none of these servlet examples can change the world, they do show how well XML and the servlet fit together. The Servlet is a great mechanism for sending content to customers, and XML is a perfect mechanism for sending structured data. You can also use the servlet to process XML documents on the server and send their contents to the client. Most importantly, both technologies are cross-platform technologies that can bring greater flexibility and scalability to your applications as well.
reference materials
    • Learn more about the DB2 XML Extender. The documentation that comes with the product has an in-depth tutorial that can help you bring XML documents into the world of relational databases.
    • Learn more about Oracle database products.
    • Try the free servlet tutorial that comes with WebSphere.
    • Drill down through the DeveloperWorks XML programming in Java tutorial to explore how to parse XML files.
    • Download the PDF version of the presentation produced by the author for the city Java user group.
    • Learn to use Emacs color coded code listings on the www.gnu.org website. The Htmlize package can be obtained from the URL jagor.srce.hr.


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.