JSTL SQL:Query Count(*)

If I want to count the number of records returned by a result, how do I access the returned value?  What is its name?  normally you would <c:out value="${row.[ColumnName]}"/>.  What the column name??

LVL 3
w0lverAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
use

select count(*) as mycount from table

and use 'mycount' as the column name
0
 
bloodredsunCommented:
On your resultset var call rowCount for the number of returned rows

e.g.

<sql:query var="emps" sql="SELECT * FROM Employee" />

Number of rows returned: <c:out value="${emps.rowCount}"/>
0
 
bloodredsunCommented:
For good examples go here:

http://www.onjava.com/pub/a/onjava/2002/09/11/jstl2.html?page=3
http://archives.postgresql.org/pgsql-jdbc/2003-08/msg00012.php
http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jsps/JstlSql.jsp.html

From the last one, an example of how to populate a table from a jstl:sql result object

<%-- An example showing how to populate a table --%>
  <sql:query var="deejays"> SELECT * FROM mytable</sql:query>

<table border="1">

  <%-- Get the column names for the header of the table --%>
  <c:forEach var="columnName" items="${deejays.columnNames}">
    <th><c:out value="${columnName}"/></th>
  </c:forEach>

  <%-- Get the value of each column while iterating over rows --%>
  <c:forEach var="row" items="${deejays.rows}">
    <tr>
    <c:forEach var="column" items="${row}">
      <td><c:out value="${column.value}"/></td>
    </c:forEach>

  </tr>
  </c:forEach>
</table>
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
suprapto45Commented:
Hi,

Alternatively, (perhaps inefficiently), you can use c:set within the <c:forEach> so it acts as the counter :).

Regards
Dave
0
 
bloodredsunCommented:
I believe that the rowCount method is the most efficient as it doesn't require an external counter or an extra sql statement but can be added in to any bit of JSTL sql code :-)
0
 
bloodredsunCommented:
>>Alternatively, (perhaps inefficiently), you can use c:set within the <c:forEach> so it acts as the counter :).

And if you wanted to do this, you should be using the intrinsic one to c:forEach, the varStatus attribute where you can call varStatus.count to get the current iteration. You can then check if it is the last one by calling varStatus.count == varStatus.last
0
 
w0lverAuthor Commented:
Thanks for the suggestions, but objects hit my particular nail on the head...

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.