Discussion:
Easy get method of record number
Kazuaki Miyauchi
2014-09-24 08:52:20 UTC
Permalink
Hi, it seems there is no way to get the numbers of records without
issuing count sql . It is convenient to get the number as following.

<sql:query var="rsSelect" dataSource="${example}" sql="select
firstName, address from member where id = 'aiueo';" />
<c:if test="${rsSelect.countNum == 0}" >
...

Why is there no function to get record number?

Regards,
Stuart Thiel
2014-09-24 13:46:54 UTC
Permalink
Hi Kazuaki,

Firstly, be real careful about using SQL taglibs. It very much goes
against the patterns discussed in class for the reasons discussed in
class.

However, taglibs aside, I understand your concern. The main reason is
that the ResultSet can start getting processed immediately under some
conditions, thus it makes sense not to know the final number of
records because the database might not have counted the final number
of records. A corollary from that is that UI often only displays a
certain number of records and only wants a few at a time, so we can
often go a step further and use the LIMIT qualifier (available in many
sql implementations for that purpose).

The good news about the "SELECT COUNT(id)" syntax is that you don't
need ordering, and you're usually selecting on an index, even if not
the primary one, so it can do those counts pretty darn fast. The count
may also prime the next initial select query, on a good db.
Post by Kazuaki Miyauchi
Hi, it seems there is no way to get the numbers of records without
issuing count sql . It is convenient to get the number as following.
<sql:query var="rsSelect" dataSource="${example}" sql="select
firstName, address from member where id = 'aiueo';" />
<c:if test="${rsSelect.countNum == 0}" >
...
Why is there no function to get record number?
Regards,
---------------------------------------------------------------------
--
Stuart Thiel, P. Eng.
Kazuaki Miyauchi
2014-09-25 08:51:24 UTC
Permalink
Post by Stuart Thiel
However, taglibs aside, I understand your concern. The main reason is
that the ResultSet can start getting processed immediately under some
conditions, thus it makes sense not to know the final number of
records because the database might not have counted the final number
of records.
Thanks for your immediate reply, but,
Using Apache Commons DbUtils, there is length function which show the
final number of records as following.

Object[] pi = (Object[])qr.query(
for (int i=0; i<pi.length; i++){

So, the database might return the number of records at first, I suppose.

Regards.

Loading...