[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JSTL SQL:Query Count(*)

Posted on 2005-05-11
7
Medium Priority
?
6,797 Views
Last Modified: 2013-11-24
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??

0
Comment
Question by:w0lver
7 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13982674
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
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13982686
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 13983189
Hi,

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

Regards
Dave
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 13983814
use

select count(*) as mycount from table

and use 'mycount' as the column name
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13985201
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
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13985234
>>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
 
LVL 3

Author Comment

by:w0lver
ID: 13986543
Thanks for the suggestions, but objects hit my particular nail on the head...

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question