Re: Returning A ResultSet
Thanks for the help Tom, I really appreciate it. I changed yours a
little since I wanted to append to an XML file if there were results.
This is what I got...
A database call now looks like this...
-----------------------------------------------------------------
try
{
conn = RDBMServices.getConnection();
stmt = conn.prepareStatement("SELECT * FROM ALERT_PROVIDERS");
executeQuery(stmt, xml, new ResultSetHandler()
{
public String handle(ResultSet results, String xml) throws
SQLException
{
while(results.next())
{
xml+="\n\t<provider>"+
"\n\t\t<name>"+results.getString("provider_name")+"</
name>"+
"\n\t\t<value>"+results.getString("id")+"</value>"+
"\n\t</provider>";
}
System.out.println("ResultSet: "+results);
}
});
}
catch(SQLException e)
{
System.out.println("SQLException: "+e);
}
-----------------------------------------------------------------
Which is a lot cleaner, and a little shorter than it was originally.
Then I changed a little of the interface and executeQuery function...
-----------------------------------------------------------------
public interface ResultSetHandler
{
void handle(ResultSet results, String xml) throws SQLException;
}
-----------------------------------------------------------------
-----------------------------------------------------------------
static void executeQuery(PreparedStatement statement, String xml,
ResultSetHandler handler) throws SQLException
{
ResultSet results = statement.executeQuery();
try
{
handler.handle(results, xml);
}
catch(SQLException e){}
finally
{
results.close();
}
}
-----------------------------------------------------------------