I have a webpage which accepts a query from the user, and passes it to a servlet. This servlet calls my java functions which run the query and get the resulting table. I now have this result set, but I wish to display this result set in a table on my webpage. The problem is I don't know what the query is before hand or the schema of the table it will run on. I wanted to know if there is a way to dynamically present the result set contents on a table on my webpage. Since I don't know the schema, don't have the option of creating a java model object. I'm using HTML, JSP and obviously Java
-
Can you write a code to transfer ArrayList content into HTML table? – PM 77-1 Aug 15 '16 at 16:18
-
@PM 77-1 Could you elaborate? Because I think I can figure that out, wanted to know how that could help with my problem? – newbie Aug 15 '16 at 17:05
-
Read your resultset into `ArrayList
>` (araylist of arraylists of strings) and pass to your JSP. Your JSP will render it as a table without any knowledge of its actual content. – PM 77-1 Aug 15 '16 at 17:26 -
Duplicate of http://stackoverflow.com/q/9201128 – BalusC Aug 15 '16 at 18:20
2 Answers
I hope my memory about JSP is good. I suppose you have do a select with '*' to have all columns. In the result you can have the number of columns, so know how much column you have to create, and a "for" on column names, give you the column names. With this you should be able show select result without knowing in advance the column name in table. May be its only possible only on result set from JDBC. Sorry if not apply for you

- 21
- 4
Use getMetaData() on your resultset object to get the details of resultset like number of columns and names of the column. Using size() method get the size of the resultset.
Next using the size() you can compute how many rows should be there in your html table and using resultset.getMetaData().getColumnCount() you can compute number of columns in your html table. You can use the column names to be headings of your table columns.
Let me know if you need further help.
sample code retrieve metadata from you resultset.
st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM survey");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
System.out.println("resultSet MetaData column Count=" + numberOfColumns);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column MetaData ");
System.out.println("column number " + i);
// get the column's name.
System.out.println(rsMetaData.getColumnName(i));
}

- 61
- 4