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

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

Introduction

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

Prototype

public void setMaxRows(int maxRows) 

Source Link

Document

Set the maximum number of rows to add to your Document.

Usage

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)
 */// w w  w  .j  ava2s .  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();
}