ResultSetMetaData rsmd = rs.getMetaData();
int columns = rsmd.getColumnCount();
Element numColumns = doc.createElement("numcolumns");
Text numColumnsValue = doc.createTextNode("" + columns);
numColumns.appendChild(numColumnsValue);
desc.appendChild(numColumns);
Element columnNames = doc.createElement("columnnames");
for (int i = 1, j = columns; i <= j; i++) {
columnName = rsmd.getColumnName(i);
Element name = doc.createElement("column");
Text value = doc.createTextNode(columnName);
name.appendChild(value);
columnNames.appendChild(name);
}
desc.appendChild(columnNames);
while (rs.next()) {
if (log.isLoggable(Level.FINE)) log.fine("Scanning SQL result with rowlimit=" + rowlimit + ", rows=" + rows);
if (rowlimit < 0) {
continue;
}
else if (rows >= rowlimit) {
break;
}
rows++;
Element row = doc.createElement(rownode);
for (int i = 1, j = columns; i <= j; i++) {
int cType = rsmd.getColumnType(i);
columnName = rsmd.getColumnName(i);
String columnValue = "";
switch (cType) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
columnValue = rs.getString(i);
break;
case Types.DOUBLE:
columnValue = "" + rs.getDouble(i);
break;
case Types.FLOAT:
columnValue = "" + rs.getFloat(i);
break;
case Types.INTEGER:
columnValue = "" + rs.getInt(i);
break;
case Types.NUMERIC:
columnValue = "" + rs.getLong(i);
break;
case Types.DATE:
Date d = rs.getDate(i);
columnValue = (d==null) ? NULL_STR : d.toString();
break;
case Types.TIMESTAMP:
Timestamp t = rs.getTimestamp(i);
columnValue = (t==null) ? NULL_STR : t.toString();
break;
case Types.BIT:
case Types.TINYINT:
case Types.SMALLINT:
case Types.BIGINT:
case Types.REAL:
case Types.DECIMAL:
case Types.TIME:
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
case Types.NULL:
case Types.OTHER:
case Types.JAVA_OBJECT:
case Types.DISTINCT:
case Types.STRUCT:
case Types.ARRAY:
case Types.BLOB:
case Types.CLOB:
case Types.REF:
/* since JDK 1.4
case Types.DATALINK:
case Types.BOOLEAN:
*/
Object o1 = rs.getObject(i);
columnValue = (o1==null) ? NULL_STR : o1.toString();
break;
default:
if (log.isLoggable(Level.FINE)) log.warning("Datatype '" + cType + "' of column '" + columnName + "' is not implemented, plase add a case statement in DBAdapterUtils.java");
Object o2 = rs.getObject(i);
columnValue = (o2==null) ? NULL_STR : o2.toString();
break;
}
if (log.isLoggable(Level.FINE)) log.fine("row="+ rows + ", columnName=" + columnName + ", type=" + cType + " columnValue='" + columnValue + "'");
Element col = doc.createElement(columnName);
CDATASection cvalue = doc.createCDATASection(columnValue);
col.appendChild(cvalue);
row.appendChild(col);
results.appendChild(row);
}
}
Element numRows = doc.createElement("rownum");
Text numRowsValue = doc.createTextNode("" + rows);
numRows.appendChild(numRowsValue);
desc.appendChild(numRows);
doc.appendChild(root);