Announcement Announcement Module
Collapse
No announcement yet.
count number of rows Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • count number of rows

    Is it possible to do the sql-command "count * from table" with Spring? Can anyone provide some code to do this?
    I know you can use the functions getRowCount and getColumnCount on the resultset. But this is only valid after processing is complete. I want to know the number of records before I proces. If the table is too large I don't want to proces the records...

  • #2
    In the meanwhile I already figured out a solution:

    final ArrayList results = new ArrayList();
    JdbcTemplate template =getJdbcTemplate();
    class V_D_Table_Handler implements RowCallbackHandler{
    public void processRow(ResultSet rs) throws SQLException{

    Integer aantal = new Integer(rs.getInt(1));
    results.add(aantal);
    }
    }
    template.query("SELECT COUNT (*) FROM V_D_Table", new V_D_Table_Handler());
    return results;

    I'm sure there is a beter solution, because I return an ArrayList (which I always know the length is 1). But I can go further with this solution. any suggestions are still welcome.

    Comment


    • #3
      This can be shortened to:

      return getJdbcTemplate.queryForInt("SELECT COUNT (*) FROM V_D_Table");

      Comment

      Working...
      X