Convert a ResultSet to XML : Query ResultSet « Database « Java Tutorial






import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
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 org.w3c.dom.Element;

public class Main {

  public static void main(String args[]) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager
        .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb");
    
    ResultSet rs = con.createStatement().executeQuery("select * from product");

    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    while (rs.next()) {
      Element row = doc.createElement("Row");
      results.appendChild(row);
      for (int i = 1; i <= colCount; i++) {
        String columnName = rsmd.getColumnName(i);
        Object value = rs.getObject(i);
        Element node = doc.createElement(columnName);
        node.appendChild(doc.createTextNode(value.toString()));
        row.appendChild(node);
      }
    }
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);

    System.out.println(sw.toString());

    con.close();
    rs.close();
  }
}








20.6.Query ResultSet
20.6.1.ResultSet's Methods to Access Columns by Index
20.6.2.ResultSet's Methods to Access Columns by Name
20.6.3.ResultSet: A SQL query returns a ResultSet containing the requested data
20.6.4.Get Data from a ResultSet
20.6.5.Retrieving the Value of a Column Using the Index Number
20.6.6.Retrieving the Value of a Column Using the Column Name
20.6.7.Selecting all rows from a table and creates a result set:
20.6.8.If you Do Not Know the Name, Position, and Type of Each Column, how to get value from ResultSet
20.6.9.Determine If a Fetched Value Is NULL
20.6.10.Get int value from ResultSet
20.6.11.Get the Number of Rows in a Database Table
20.6.12.Limit the Number of Rows Returned from a SQL Query
20.6.13.Checking for a Warning Using a ResultSet Object
20.6.14.Convert a ResultSet to XML
20.6.15.Converting types: DATE to String