I'm looping through a ResultSet in Java; which for testing purposes is returning about 30 rows with 17 Columns (all String data) per row. I'm manually building an XML String out of the results using StringBuilder and its literally taking about 36 seconds for the loop to finish these iterations.
Note: I realize this isn't the best way to go about getting XML out of a Database, or even the best way to get XML out of a ResultSet - but this has me curious of the slow performance regardless.
Update: As per responses so far I have to address the following: The time to run the Query is less than a second, and I did a System.currentTimeMillis() before and after every section of my code to narrow this down. The 36 seconds is entirely within the code below.
ResultSetMetaData rsmeta = rset.getMetaData();
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.append("<?xml version=\"1.0\" ?><ROWSET>");
if(numColumns != 0){
while (rset.next()) {
resultBuilder.append("<ROW>");
for (int i = 0; i <= numColumns -1;i++) {
columnName = rsmeta.getColumnName(i+1);
resultBuilder.append("<");
resultBuilder.append(columnName);
resultBuilder.append(">");
resultBuilder.append(rset.getString(i+1));
resultBuilder.append("</");
resultBuilder.append(columnName);
resultBuilder.append(">");
}
resultBuilder.append("</ROW>");
numRows += 1;
}
}
else {
stmt.close();
wsConn.close();
return "No Results";
}
Update: Given the suggestions I received - this code takes roughly the same amount of time give or take half a second.
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.append("<?xml version=\"1.0\" ?><ROWSET>");
if(numColumns != 0){
while (rset.next()) {
resultBuilder.append("<ROW>");
for (int i = 0; i <= numColumns -1;i++) {
//columnName = rsmeta.getColumnName(i+1);
resultBuilder.append("<");
resultBuilder.append("TestColumnName");
resultBuilder.append(">");
//resultBuilder.append(rset.getString(i+1));
resultBuilder.append("TestData");
resultBuilder.append("</");
resultBuilder.append("TestColumnName");
resultBuilder.append(">");
}
resultBuilder.append("</ROW>");
numRows += 1;
}
}
else {
stmt.close();
wsConn.close();
return "No Results";
}
The last test I did having eliminated everything else was to replace the while test with a realistic number of iterations (160, the max rows returned from the small tests I've done previously). The question now is, what could it be about this result set that causes such a slow down.
while (numRows <= 160) {
// same as above
}
Update: As suggested I'll be closing this question as the title doesn't reflect the direction the problem took.