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.io.FileDescriptor;
import java.io.FileOutputStream;
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 javax.xml.transform.stream.StreamResult;

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 {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.err.println("Driver class not found");
			e.printStackTrace();
		}
		Connection con = null;
		try {
			con =
				(Connection) DriverManager.getConnection(
					"jdbc:mysql://databaseServer/database/",
					"username",
					"password");
		} catch (SQLException e1) {
			System.err.println("Error establishing database connection");
			e1.printStackTrace();
		}
		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 =
					(TransformerFactory.newInstance()).newTransformer();
				System.out.println(
					"XML Schema instance describing the document");
				myTransformer.transform(
					new DOMSource(xsd),
					new StreamResult(new FileOutputStream(FileDescriptor.out)));

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

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

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: http://www.jeckle.de/freeStuff/JDBC2XML/index.html
RDF metadata describing this page RDF description for this page