使用 Java JDBC 对 MySQL 进行“计数”查询的返回类型是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7165576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:55:18  来源:igfitidea点击:

What is the return type of a "count" query against MySQL using Java JDBC?

javamysqljdbccount

提问by Beladula

String query = "SELECT COUNT(*) AS count FROM table1";
ResultSet rs = DBConnection.executeQuery(query);

The above code works fine; however:

上面的代码工作正常;然而:

long count = rs.getLong("count");

This wont work, it throws SQLException. How should I get data from the ResultSet?

这行不通,它会抛出 SQLException。我应该如何从ResultSet?

采纳答案by ZenMaster

long tmpLong = rs.getLong(1); 

should work from what I can recall.

应该从我能回忆起的工作开始。

EDIT:

编辑:

of course:

当然:

rs.next();

回答by Petar Minchev

Have you called rs.next()before calling rs.getLong("count");

打电话rs.next()之前你打过电话吗rs.getLong("count");

回答by Czar Pino

rs.getLonghas 2 versions rs.getLong(java.lang.String)and rs.getLong(int). For the string version use:

rs.getLong有 2 个版本rs.getLong(java.lang.String)rs.getLong(int). 对于字符串版本使用:

long count = rs.getLong("COUNT(*)");

Since the column name is COUNT(*)

由于列名是 COUNT(*)

Based on the query used:

基于所使用的查询:

SELECT COUNT(*) AS count FROM table1