back to top   Purpose


Tiny little hack to transfer objects conforming to JDBC's ResultSet interface into objects conforming to W3C's DOM Document interface.
In essence the application transforms arbitrary query results generated by JDBC into XML documents and matching XML Schema instances.

back to top   Usage


  1. Import the class import de.jeckle.RS2DOM.RS2DOM;
  2. Query your database using JDBC.
  3. Generate a XML Schema according to results extracted from the database by invoking RS2DOM.ResultSet2XSDDOM(resultSet)
  4. Generate a XML document matching the generated schema containing the results extracted from the database by invoking RS2DOM.ResultSet2DOM(resultSet)

back to top   Example


Note: Although, the example uses the DOM implementation shipped with SUN's JDK and connects to the database using MySQL's Connector/J JDBC-complaint driver the methods called to create the DOM trees operate solely on standardized interfaces. Hence, they are compatible to all DOM and JDBC compliant implementations.

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Document;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import de.jeckle.RS2DOM.RS2DOM;

public class TestDriver {
	public static void main(String[] args) {
		try {
		} catch (ClassNotFoundException e) {
			System.err.println("Driver class not found");
		Connection con = null;
		try {
			con =
				(Connection) DriverManager.getConnection(
		} catch (SQLException e1) {
			System.err.println("Error establishing database connection");
		Statement stmt = null;
		try {
			stmt = (Statement) con.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * FROM test;");

			//transfer ResultSet into XML Schema stored as DOM
			Document xsd = RS2DOM.ResultSet2XSDDOM(rs);

			//transfer ResultSet into DOM
			Document d = RS2DOM.ResultSet2DOM(rs);
			//that's all ;-)

			try {
				Transformer myTransformer =
					"XML Schema instance describing the document");
					new DOMSource(xsd),
					new StreamResult(new FileOutputStream(FileDescriptor.out)));

					"\n\nXML document containing the result set");
					new DOMSource(d),
					new StreamResult(new FileOutputStream(FileDescriptor.out)));
			} catch (Exception e) {

		} catch (SQLException e2) {
			System.err.println("Error creating SQL-Statement");

back to top   License


The implementation is released unter the Lesser GNU Public License.

back to top   Download


separator line
Service provided by Mario Jeckle
Generated: 2004-06-11T07:11:37+01:00
Feedback Feedback       SiteMap SiteMap
This page's original location This page's original location:
RDF metadata describing this page RDF description for this page