Java:如何从ResultSet Select查询中提取Count

时间:2015-09-13 04:07:25

标签: java sql count resultset

此问题已在此处得到解答,但发布的解决方案对我不起作用。以下是我的代码:

Statement query = this.conn.createStatement();
            ResultSet resultSet = query.executeQuery("Select COUNT(*) FROM Questions AS total");
            resultSet.next();
            int totalQuestions = resultSet.getInt("total");
            System.out.println(totalQuestions);
            System.exit(1);
            resultSet.close();

它一直在说未找到列总数。我已经尝试过没有“resultSet.next()”但同样的问题。我也尝试过resultSet.getInt(1),但这也不起作用。

4 个答案:

答案 0 :(得分:1)

您想要的SQL语法是

Select COUNT(*) AS total FROM Questions

或者,你可以写Select COUNT(*) FROM Questions 并使用resultSet.getInt(1)

答案 1 :(得分:1)

变化:

Select COUNT(*) FROM Questions AS total

为:

Select COUNT(*) AS total FROM Questions 

答案 2 :(得分:0)

你的SQL查询有点不对劲。尝试:

ResultSet resultSet = query.executeQuery("Select COUNT(*) AS total FROM Questions");

答案 3 :(得分:0)

像这样更改您的查询,

SQL语法是

Select COUNT(*) AS total FROM Questions