Manipulating XML type data using JDBC4.0

Source: Internet
Author: User
Tags bind

After the launch of JDBC4.0, its features from many are being widely concerned. The most important update is to support XML data types (which are defined in the latest SQL2003 standard), and of course, saving XML data in a database and updating XML data in an application is not a new technology. But this is the first time that JDBC has provided a mapping interface (Java.sql.SQLXML) and used this interface to support Sql/xml data types. Of course, other interfaces, such as Java.sql.Connection and Java.sql.ResultSet, have been updated to meet the need to handle XML data types.

Before SQL2003 standards and XML data types are rolled out, developers must save XML data in a blob, CLOB, or text Type field. Now, many mainstream databases, such as SQL Server, Oracle, and DB2, have added support for XML data types. However, before JDBC4, the Java application still had to convert the XML data type in the database to the data type supported by JDBC. But new JDBC can bind XML through local interfaces, so it becomes easier and more efficient to process XML data in any database.

In this article, I'll show you how to manipulate XML-type fields with JDBC4.0 (save and get XML data), and give an example for readers ' reference.

First, storing and acquiring XML data

In order to save XML data in a field of XML type, we should first call the Java.sql.Connection.createSQLXML () method. This method returns an instance of the Java.sql.SQLXML. We can then add XML data to the SQLXML object by calling Setoutputstream (), Setcharacterstream (), or simply calling SetString (String XML). Note that this feature is very similar to the use of BLOB and CLOB types.

One of the key features of JDBC4.0 is that we can also get an implementation of a class that sets Avax.xml.transform.Result by invoking the Java.sql.SQLXML Setresult (class ResultClass) method. These classes include Domresult, Jaxbresult, and Saxresult. In other words, we can simply do the following things without converting:

1. Get XML data

2. Establishment of an independent Domresult object

3. Pass Domresult to Java.sql.SQLXML object

4. Save XML data directly to the Response database field via Java.sql.Statement

To Java.sql.ResultSet get SQLXML type data, we just need to call Getsqlxml and specify the corresponding field name or index. We can then get the actual XML data, or a simple string, from the Java.io.InputStream via Getbinarystream (), Getcharacterstream () or GetString (). The process of saving XML data also obtains XML data is similar, we can also get the XML source by invoking the GetSource (Class Sourceclass) method of the SqlXml object instance, so We can access XML data from any class that implements Javax.xml.transform.Source.

Second, the instance procedure

Since JDBC4 was officially released on December 11, 2006 (with j2se6.0), many database drivers are not very well supported by JDBC4. In this example, a version 10.2来 of the Apache Derby database was used to discuss the preservation and acquisition of XML type data. This version of Derby is not java.sql.SQLXML, which means we can't get the XML data directly from the result value, and bind the XML data. But Derby and SQL 2003 are compatible, and it's often not easy to use embedded mode, which, in this case, can still show us how to manipulate XML data as if using a driver that fully supports JDBC4. The code for manipulating the XML data for Derby is as follows:

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.