Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Servlets and XML: Made for Each Other

By Doug Tidwell
2005-05-18


Interfacing with a database

Our final example generates XML from a database query. There are many ways of doing this; for this example, we'll use IBM XML Extender for DB2 (see Resources). This free product lets you store XML documents in DB2. Our query extracts those documents from DB2, then passes them on to the user.

If you're using Oracle 8i instead of DB2, you'll find that it boasts similar functions (see Resources). For databases that aren't XML-aware, you could store the XML document as a character large object (CLOB) and retrieve the document as a chunk of text.

However your database is set up, you need to do three things to get this code to work:

  1. First, change the DbOwner, DbUserid, and DbPasswd variables to the correct values for your system.
    
    
    ///////////////////////////////////////////////////////////////////////////////

    // Be sure to change these three strings appropriately, or the //

    // servlet won't work. //

    ///////////////////////////////////////////////////////////////////////////////

    DbUserid = "xxxxxxxx";

    DbPasswd = "xxxxxxxx";

    DbOwner = "xxxxxxxx";


  2. Next, use the JDBC driver that works with your system. We're using DB2.

    
    
    staticStringJDBCDriver = "COM.ibm.db2.jdbc.app.DB2Driver";

    ...

    try

    {

    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();

    }

    catch (Exceptione)

    {

    System.out.println("Can't get the driver!"); e.printStackTrace();

    }


  3. If you want to, change the SQL query. To keep the demo simple, we're simply retrieving all of the XML documents from the order column in the sales_order_view table.

    
    
    // We hardcoded the SQL statement here; this would be more

    // sophisticated if we qualified the query based on user input.

    Stringquery = "select order from " + DbOwner + ".sales_order_view";


In the service method, our servlet connects to DB2, executes a query (the results of which are a set of XML documents), parses the query results, and writes the parsed data to the output stream. Listing 6 shows the most relevant sections of the code:

Listing 6. xmlfromdb2.java


// We hardcoded the SQL statement here; this would be more
// sophisticated if we qualified the query based on user input.
Stringquery = "select order from " + DbOwner + ".sales_order_view";

res.setContentType("text/xml");

try
{
ConInfoindex = newConInfo();
Connectioncon = getCon(index);
Statementstmt = con.createStatement();
ResultSetrs = stmt.executeQuery(query);

...

// Display the result set. We take the XML doc from each row,
// parse it, then print the DOM tree. rs.next() returns
// false when there are no more rows.
while (rs.next())
{
StringnextOrder = rs.getString(1).trim();
Documentdoc = null;
StringReadersr = newStringReader(nextOrder);
InputSourceiSrc = newInputSource(sr);

try
{
parser.parse(iSrc);
doc = parser.getDocument();
}
catch (Exceptione)
{
System.err.println("Sorry, an error occurred: " + e);
}

if (doc != null)
printDOMTree(doc, out);
}

To see all the details, check out the HTML view of the complete listing or view the Java source file directly.



Tutorial Pages:
» Our first sample servlet
» A very basic servlet
» Generating XML fragments
» Interfacing with a database
» Summary
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» Starting with XML
» Performing Client-Side XSL Transformations
» Create a Google Sitemap for your Web Site
» XML and Scripting Languages
» Parsing Comma-Separated Values
» XML Security Suite: Increasing the Security of E-Business