Results 1 to 3 of 3

Thread: count number of rows

  1. #1
    Join Date
    Apr 2005
    Posts
    27

    Default 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. #2
    Join Date
    Apr 2005
    Posts
    27

    Default

    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.

  3. #3
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    This can be shortened to:

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •