I have a query string that is dynamic. The items I am searching for in the database are based upon what the user selects in a checkbox on an html form. The user types selects a city name from a drop down list. Then the user can select from 3 different attributes to the city: CountryCode(int), District(String), and Population(int).
I have no problem creating the query. For example if the user selects Tulsa and selects District and Population the queryString SELECT District, Population FROM City WHERE name ='Tulsa' is created no problem. For another example if the user simply selected country code of Tulsa the query string created would be SELECT CountryCode FROM City WHERE name ='Tulsa'.
The problem is parsing the data and displaying it neatly to the screen. I am no database programmer so I am having a tough time. Here is a tidbit of my code so you can see what I’m doing.
The only thing I can do correctly is if the user selects District and Population. Any other selection doesn’t work. I am basically hard coding District and Population. I am not sure how to parse it dynamically. Here is the code. Query string is SELECT District, Population FROM City WHERE name ='Tulsa'. Its the only one I can get to work.
public String getData( String c)
{
String query = c;
ResultSet rs = null;
StringBuffer back = new StringBuffer();
try
{
rs = st.executeQuery(c);
ResultSetMetaData rsmd = rs.getMetaData();
int numColumns = rsmd.getColumnCount();
back.append( "number of columns is " + numColumns);
back.append( "</br>");
back.append( "<table border=\"10\" >\n" );
while(rs.next())
{
if(rsmd.getColumnTypeName(1).equals("CHAR"))
back.append("<tr><td>" + rsmd.getColumnName(1) + "</td>" + "<td>" + rs.getString(1) + "</trd</tr>");
if(rsmd.getColumnTypeName(2).equals("INT"))
back.append("<tr><td>" + rsmd.getColumnName(2) + "</td>" + "<td>" + Integer.toString(rs.getInt(2)) + "</td></tr>");
}
back.append( "</table>" );
}
catch( SQLException e )
{
back.append( "<h6>something bad is happening</h6>");
e.printStackTrace();
return null;
}
return new String( back );
}
I hope you guys understand what I’m asking. Thanks alot for the help!
If all you want to do is output the data, you can just get everything as a
StringusingResultSet.getString(index)since you know the number of columns from your metadata.