How do you access the value of an SQL count () query in a Java program

前端 未结 7 1157
梦毁少年i
梦毁少年i 2021-02-01 00:54

I want to get to the value I am finding using the COUNT command of SQL. Normally I enter the column name I want to access into the getInt() getString() method, what do I do in t

7条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 01:09

    It's similar to above but you can try like

    public Integer count(String tableName) throws CrateException {
            String query = String.format("Select count(*) as size from %s", tableName);
            try (Statement s = connection.createStatement()) {
                try (ResultSet resultSet = queryExecutor.executeQuery(s, query)) {
                    Preconditions.checkArgument(resultSet.next(), "Result set is empty");
                    return resultSet.getInt("size");
                }
            } catch (SQLException e) {
                throw new CrateException(e);
            }
        }
    }
    

提交回复
热议问题