0
votes

What is the return value type of count(*) in the ResultSet of a Prepared Statement with Spring JdbcTemplate?

        String query = "select count(*) as ROW_COUNT from table1";

        List<Map<String, Object>> list = executePreparedStatement(query);

        Iterator<Map<String, Object>> iter = list.iterator();
        if (iter.hasNext()) {
            Map<String, Object> lom = iter.next();
            return (((Long) lom.get("ROW_COUNT"))).intValue();
        }

Does this depend on the JDBC driver / and or the database?

For example in DB2 the return value type was Integer, but in PostgreSQL it is Long.

Why is this different?

1
What about return (((Number) lom.get("ROW_COUNT"))).intValue(); ?StanislavL

1 Answers

1
votes

Different vendors have different implementations.

Sometimes even a single vendor can have different implementations.

You need to code accordingly.

Db2 for Linux/Unix/Windows, Db2 for i-Series, Db2 for Z/OS, all return a large integer from the COUNT function.

Additionally Db2 for i can return DECIMAL(15,0) from the count function if the table is distributed.

There's also the COUNT_BIG function in Db2 which returns DECIMAL(31,0).