In my app I have a database that contains a table
Log that logs logtime for a user. It contains the following columns:logID, username, logTime.
On this table I'm running several similar
select statements. These selections are at the end displayed in a
jsp file. In most cases only
two columns are needed to display the selections from the table. These could for instance be:
username and number of times logged in, and
username and last time logged in. I've shown an example below.
The problem is that the only thing that changes in the
jsp file is the
column names. Now I use a
different jsp file for each select statement. But this seems a bit wrong. Is there a way to get the
column names from the rows selected?
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List logs = session.createQuery("SELECT new map( l.username as username, count(*) as count ) from Log as l where l.username=:u GROUP BY l.username").setString("u", username).list();
session.getTransaction().commit();
jsp-fileCode:
<table>
<tr>
<th>Username</th><th>Count</th>
</tr>
<c:forEach items="${model.logList}" var="log" varStatus="rowCounter">
<c:choose>
<tr>
<td><c:out value="${log.username}"/></td>
<td><c:out value="${log.count}"/></td>
</tr>
</c:forEach>
</table>