Java Reference
In-Depth Information
our terms
and conditions above -->
<moreovernews>
<article id="_34226715">
<url>http://c.moreover.com/click/here.pl?x34226715</url>
<headline_text>
Assistant fire chief ascends to departments top role
</headline_text>
<source>Springfield News-Leader</source>
<media_type>text</media_type>
<cluster>moreover...</cluster>
<tagline></tagline>
<document_url>
http://www.springfieldnews-leader.com/news/
</document_url>
<harvest_time>Mar 20 2002 2:30AM</harvest_time>
<access_registration></access_registration>
<access_status></access_status>
</article>
...
</moreovernews>
The SQLInsertBean used to insert the content of an XML document into a database table is an
extension of the basic Xbean base class of Listing 17-2 . The processDocument() method first calls
the prepareStatement() method, which creates a PreparedStatement to handle the SQL
INSERT command. The PreparedStatement is simply a SQL INSERT command with 11
placeholders, one for each data node in the article element (10 child elements, plus the id attribute).
Cross-
Reference
PreparedStatements reduce the processing overhead of compiling a
SQL statement when it is to be used repetitivley. The advantages of using
the PreparedStatement object when performing multiple repetitions of a
SQL command is discussed in Chapter 4 , with a brief example in Chapter
13 .
The getValues() method is passed an <article> element, which it parses to retrieve the id
attribute and the child elements. These are returned in a String array, which is passed to the
insertHeadline() method. Note that a Java null is specifically inserted into the array for empty
child elements. These nulls are converted automatically to SQL NULL s on insertion.
The insertHeadline() method sets the parameters of the PreparedStatement from the String
array and then calls the PreparedStatement 's executeUpdate() method to insert the data from
the XML document.
After looping through all the <article> elements, the Connection object's close() method is called to
close the connection to the DataSource . If you fail to close the connection, the garbage collector will
close it for you. Listing 17-11 illustrates the use of a PreparedStatement object to insert news
headlines into the database.
Listing 17-11: SQLInsertBean
Search WWH ::




Custom Search