Use JDBC4.0 to manipulate the methods of XML types (save XML data) _java

Source: Internet
Author: User
Tags exception handling java se

Among the many new features and improvements provided by Java SE 6, it is worth mentioning that the JDBC version that provides database access to Java programs is upgraded to 4.0, a JSR-221 version that provides more convenient coding mechanisms and flexibility, and supports more data types .

The new features of JDBC 4.0 are grouped into the following four categories:

1. Drive and connection management

2. Exception handling

3. Data type Support

4. Changes in the API

This is not the focus of this article, the following to introduce the jdbc4.0 operation of XML type data, details as follows:

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:

 import java.io.StringReader; import java.sql.*; public class Xmldbtester {static final String XML1 = "<article>" + "<title>first article</title>" + "<author>john smith</author>
"+" <body>a very short article.</body> "+ </article>"; Static final String XML2 = "<article>" + "<title>second article</title>" + "<author>mary Jones
</author> "+" <body>another short article.</body> "+ </article>"; Static final String XML3 = "<article>" + "<title>third article</title>" + "<author>john smith<
/author> "+" <body>last short article.</body> "+ </article>";
Static final string[] articles = {XML1, XML2, XML3};
public static void Main (String s[]) {xmldbtester xdt = new Xmldbtester ();
Connection C = xdt.getconnection ();
Xdt.loaddemodata (c); 
Xdt.demoxmlresult (c); 

The above is a small set to introduce the use of JDBC4.0 to manipulate the XML type of data, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.