首页 > 解决方案 > Spring JdbcTemplate:PreparedStatement的ResultSet中count(*)的返回值类型

问题描述

Spring JdbcTemplate的Prepared Statement的ResultSet中count(*)的返回值类型是什么?

        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();
        }

这是否取决于 JDBC 驱动程序/和或数据库?

例如,在 DB2 中,返回值类型是 Integer,但在 PostgreSQL 中是 Long。

为什么这不一样?

标签: springpostgresqldb2jdbctemplate

解决方案


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).


推荐阅读