Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Get the number of rows?

Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I get the number of rows on a specific table? I'm using Kodo JDO 3.1.5 with Oracle.

I want to know how many rows there are in the table before to execute the final query. I want to do this because when there are 100.000 rows or more I get a java.lang.OutOfMemoryError. So I don't to want to execute the final query if there are too many rows.

protected Collection execute(Class type, String filter) {

final Extent extent = getPersistenceManager().getExtent(type, true);
query = pm.newQuery(extent, filter);
query.setOrdering(orderBy + " " +
(orderAscending ? "ascending" : "decending"));

// Get the number of rows
numberOfRows = Xxxxxxxxxxxxx;
if (numberOfRows > MAX_PER_COLLECTION) {
throw new XxxxxxException("Too Many Rows: " + result.size());

final Collection result = (Collection) query.execute();

if (log.isDebugEnabled()) {
log.debug("Found " + result.size() + " match(es)");
return result;
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't supported in JDO1, but it looks like Kodo offers it as an extension:

Simplifying their example, we have:

// determine the total employees
KodoQuery q = (KodoQuery) pm.newQuery (Employee.class);
query.setResult ("count(this)");
long employees = ((Long) q.execute ()).longValue ();

However, it's worth asking whether you really need to bring 100,000 row over from the database. Very often this can be avoided.
Whatever. Here's a tiny ad:
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic