The Artima Developer Community
Sponsored Link

Java Community News
Lance Andersen on the JDBC 4.0 SQLXML Interface

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Lance Andersen on the JDBC 4.0 SQLXML Interface Posted: May 4, 2006 7:31 AM
Reply to this message Reply
Summary
JDBC 4.0 spec lead Lance Andersen discusses how a new JDBC feature, the SQLXML interface, provides methods for accessing the XML data in a datasource as a String, a Reader, a Writer, or as a Stream.
Advertisement
Lance Andersen, spec lead for JDBC 4, comments that,

The JDBC EG has reworked the SQLXML interface completely providing for more flexibility. The new version of the interface will be available in the Java SE 6 upcoming beta and should also be available in the current nightly builds.

As an earlier Artima article about JDBC 4 noted, the JDBC 4 XML features are part of a goal to bring JDBC in line with the SQL 2003 standard (PDF) that offers an xml database data type. In a database that supports SQL 2003, you could create a table such as this:

create table user_has_blog(userid int, blog_entry xml);

Then, with JDBC 4's SQLXML data type, you could retrieve a user's blog entries as follows:

Connection c = myDataSource.getConnection();
PreparedStatement st = c.prepareStatement("select userid, blog_entry from user_has_blog");
ResultSet rs = st.executeQuery();
while (rs.next()) {
	SQLXML blog = st.getSQLXML("blog_entry");
        InputStream binaryStream = sqlxml.getBinaryStream();
        DocumentBuilder parser =
  DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document result = parser.parse(binaryStream);
        javax.xml.stream.XMLStreamReader reader =  
           blog.createXMLStreamReader();
         //read data here
         blog.free();
}

It is also possible to obtain StAX events from the SQLXML type:

SQLXML blog = st.getSQLXML("blog_entry");
StAXSource staxSource = sqlxml.getSource(StAXSource.class);
   XMLStreamReader streamReader = staxSource.getXMLStreamReader();

Here getSource() returns a source used as input to XML parsers and XSLT transformers. JDBC 4 currently supports javax.xml.transform.dom.DOMSource, javax.xml.transform.sax.SAXSource, javax.xml.transform.stax.StAXSource, and javax.xml.transform.stream.StreamSource as possible class values.

As database vendors gradually adopt SQL 2003 in their products, most database servers will support XML natively. To what extent will wide XML support at the database level impact your application design?

For instance, would you prefer to use an object to model a blog entry and persist that object through an O/R tool in a more traditional, flattened database table, or would you rather write that object into an XML representation and save that XML in the database? What will be your use-cases for using XML natively in the database?

Topic: Lance Andersen on the JDBC 4.0 SQLXML Interface Previous Topic   Next Topic Topic: Logic in Database Apps: Stored Procedures or Java?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use