Among the many new features and improvements provided by Java SE 6, it is worth mentioning that the JDBC version that provides database access mechanism for Java programs has been upgraded to 4.0. This version, codenamed by JSR-221, provides a more convenient code writing mechanism and flexibility, and supports more data types.
The new features of JDBC 4.0 are divided into the following four categories:
1. Driver and connection management
2. Exception handling
3. Data type support
4. API changes
The above is not the key content that this article will talk about. Here is a way to operate xml type data by jdbc4.0. The specific details are as follows:
After the launch of JDBC 4.0, its multi-character features are receiving widespread attention. The most important update is to support XML data types (this data type is defined in the latest SQL2003 standard). Of course, saving XML data in a database and updating XML data in applications is not a new technology. But this is the first time that JDBC has provided a mapping interface (java.sql.SQLXML) and uses this interface to support SQL/XML data types. Of course, in order to meet the needs of handling XML data types, other interfaces, such as java.sql.Connection and java.sql.ResultSet, have also been updated.
Before the SQL2003 standard and XML data types are launched, developers must save XML data in the BLOB, CLOB, or TEXT type fields. Now, many mainstream databases (such as SQL Server, Oracle, and DB2) have added support for XML data types. But before JDBC4, Java applications still had to convert XML data types in the database to data types supported by JDBC. But the new JDBC can bind XML through a local interface, so it becomes easier and more efficient to process XML data in any database.
In this article, we will introduce how to use JDBC4.0 to manipulate XML-type fields (save and obtain XML data), and give an example for readers' reference.
1. Store and obtain XML data
In order to save XML data in an XML-type field, we should first call the java.sql.Connection.createSQLXML() method. This method returns an instance of java.sql.SQLXML. We can then add XML data to the SQLXML object by calling setOutputStream(), setCharacterStream() or simply calling setString(String xml). This function should be noted is very similar to the use of BLOB and CLOB types.
One of the key features of JDBC4.0 is that we can also obtain an implementation of a class that sets avax.xml.transform.Result by calling the setResult(Class resultClass) method of java.sql.SQLXML. These classes include DOMResult, JAXBResult, and SAXResult. In other words, we can simply do the following without converting:
1. Get XML data
2. Create an independent DOMResult object
3. Pass DOMResult into java.sql.SQLXML object
4. Save XML data directly into the response database field through java.sql.Statement
In order to obtain SQLXML type data by java.sql.ResultSet, we only need to call getSQLXML and specify the corresponding field name or index. Then we can get the actual XML data from java.io.InputStream via getBinaryStream(), getCharacterStream() or getString(), or a simple string. Saving XML data also gets XML data similarly. We can also get the XML source by calling the getSource(Class sourceClass) method of the SQLXML object instance. Therefore, we can access the XML data from any class that implements javax.xml.transform.Source.
2. Example program
Since JDBC4 was officially released on December 11, 2006 (released with J2SE6.0), many database drivers now do not support JDBC4 very well. In this example, a version 10.2 of the Apache Derby database is used to discuss the storage and acquisition of XML-type data. This version of Derby does not yet hold java.sql.SQLXML, which means we cannot directly obtain XML data from the result value and bind XML data. But Derby is compatible with SQL 2003 and can use embed mode very easily, and it can still demonstrate how to manipulate XML data, as if using a driver that fully supports JDBC4. The code used to manipulate Derby's XML data 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 the method of using JDBC4.0 to operate XML-type data introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!