[jdom-interest] Another org.jdom.contrib.input.ResultSetBuilder modification

Van Dooren, Damian VanDoorenD at icfg.com
Wed Feb 28 08:03:45 PST 2001


I have another modification I'd like to suggest for
org.jdom.contrib.input.ResultSetBuilder. I'd like to see a buildElement().
This again is mostly for convenience, I find a lot of times I'm getting the
root element and cloning it just so I can add the result to another element.
Having buildElement return an Element would save time and memory (while it
doing the clone).


    /**
     * <p>
     *   This builds a <code>Document</code> from the
     *   <code>java.sql.ResultSet</code>.
     * </p>
     *
     * @return <code>Document</code> - resultant Document object.
     * @throws <code>JDOMException</code> when there is a problem
     *                                    with the build.
     *
     */
    public Document build() throws JDOMException {
        Document doc = new Document(buildElement());
        return doc;
    }

    /**
     * <p>
     *   This builds a <code>Element</code> from the
     *   <code>java.sql.ResultSet</code>.
     * </p>
     *
     * @return <code>Element</code> - resultant Element object.
     * @throws <code>JDOMException</code> when there is a problem
     *                                    with the build.
     *
     */
    public Element buildElement() throws JDOMException {
      if (exception != null) {
        throw new JDOMException("Database problem", exception);
      }

      try {
        int colCount = rsmd.getColumnCount();

        Element root = new Element(rootName, ns);

        int rowCount = 0;

        // get the column labels for this record set
        String[] columnName = new String[colCount];
        for (int index = 0; index < colCount; index++) {
          columnName[index] = rsmd.getColumnName(index+1);
        }

        // build the org.jdom.Document out of the result set
        String name;
        String value;
        Element entry;
        Element child;

        while (rs.next() && (rowCount++ < maxRows)) {
          entry = new Element(rowName, ns);
          for (int col = 1; col <= colCount; col++) {
            if (names.isEmpty()) {
              name = columnName[col-1];
            }
            else {
              name = lookupName(columnName[col-1]);
            }

            value = rs.getString(col);
            if (!attribs.isEmpty() && isAttribute(name)) {
              if (!rs.wasNull()) {
                entry.addAttribute(name, value);
              }
            }
            else {
              child = new Element(name, ns);
              if (!rs.wasNull()) {
                child.setText(value);
              }
              entry.addContent(child);
            }
          }
          root.addContent(entry);
        }

        return root;
      }
      catch (SQLException e) {
        throw new JDOMException("Database problem", e);
      }
    }

-----
Damian Van Dooren
Information Technology
The Investment Centre
(519) 672-4389 x718
 



More information about the jdom-interest mailing list