Example usage for org.jdom2.contrib.input ResultSetBuilder ResultSetBuilder

List of usage examples for org.jdom2.contrib.input ResultSetBuilder ResultSetBuilder

Introduction

In this page you can find the example usage for org.jdom2.contrib.input ResultSetBuilder ResultSetBuilder.

Prototype

public ResultSetBuilder(ResultSet rs) 

Source Link

Document

This sets up a java.sql.ResultSet to be built as a Document.

Usage

From source file:ResultSetBuilderDemo.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // Tested against Cloudscape database that comes with the J2EE ref impl
    Class.forName("COM.cloudscape.core.JDBCDriver");
    Connection con = DriverManager.getConnection("jdbc:cloudscape:rsbd;create=true");

    // Create and fill commands, needed only on the first run
    Statement prep = con.createStatement();
    prep.executeUpdate(PREP);/*from  ww  w.j  a  va  2  s .c  o m*/

    Statement fill = con.createStatement();
    fill.executeUpdate(FILL);

    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select id, name, home_url || contact_phone from rsbd");
    ResultSetBuilder builder = new ResultSetBuilder(rs);
    builder.setAsElement(3, "num3");
    //builder.setNamespace(ns);
    //builder.setAsElement("id", "newid");
    //builder.setAsElement("home_url", "newhome_url");
    //builder.setAsElement(4, "some4");
    //builder.setAsAttribute(4, "some4");
    //builder.setAsAttribute("state_flag");
    builder.setAsAttribute("created_time", "ctime");
    Document doc = builder.build();
    XMLOutputter outputter = new XMLOutputter();
    outputter.output(doc, System.out);
}

From source file:sxql.java

License:Open Source License

/**
 * Execute a SQL query and return the result as XML
 * (as a String. But this can be changed to return a DOM/SAX/JDOM tree,
 * to be used, for example, as input to an XSLT processor)
 *//*from   ww w .j a  v  a2  s . c o  m*/
public static String query(Connection con, String query, String root, String row, String ns, int maxRows,
        Vector<String> attributes, Vector<String> elements) throws Exception {
    // Execute SQL Query
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);

    // Create a ResultSetBuilder
    ResultSetBuilder builder = new ResultSetBuilder(rs);

    // Configure some parameters...

    if (root != null) {
        builder.setRootName(root);
    }

    if (row != null) {
        builder.setRowName(row);
    }

    if (ns != null) {
        String namespace = null;
        String url = null;
        int sep = ns.indexOf("/");

        if (sep > 0) {
            namespace = ns.substring(0, sep);
            url = ns.substring(sep + 1);
            builder.setNamespace(Namespace.getNamespace(namespace, url));
        }
    }

    if (maxRows > 0) {
        builder.setMaxRows(maxRows);
    }

    for (int i = 0; i < attributes.size(); i++) {
        String colName = attributes.get(i);
        String attrName = null;

        if (colName.indexOf("/") >= 0) {
            String col = colName;
            int sep = col.indexOf("/");
            colName = col.substring(0, sep);
            attrName = col.substring(sep + 1);
        }

        try { // If it looks like an integer, is the column number
            int colNum = Integer.parseInt(colName);

            if (attrName == null) {
                builder.setAsAttribute(colNum); // attrName = column Name
            } else {
                builder.setAsAttribute(colNum, attrName);
            }
        } catch (NumberFormatException e) {
            // Otherwise it's the column name
            if (attrName == null) {
                builder.setAsAttribute(colName); // attrName = column Name
            } else {
                builder.setAsAttribute(colName, attrName);
            }
        }
    }

    // Rename element
    for (int i = 0; i < elements.size(); i++) {
        String colName = elements.get(i);
        String elemName = null;

        if (colName.indexOf("/") >= 0) {
            String col = colName;
            int sep = col.indexOf("/");
            colName = col.substring(0, sep);
            elemName = col.substring(sep + 1);
        }

        try { // If it looks like an integer, is the column number
            int colNum = Integer.parseInt(colName);

            if (elemName != null) { // It must have an element name
                builder.setAsElement(colNum, elemName);
            }
        } catch (NumberFormatException e) {
            // Otherwise it's the column name
            if (elemName != null) { // It must have an element name
                builder.setAsElement(colName, elemName);
            }
        }
    }

    // Build a JDOM tree
    Document doc = builder.build();

    // Convert the result to XML (as String)
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    outputter.output(doc, output);
    return output.toString();
}