Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 353
Beladula Avatar asked Aug 23 '11 18:08

Beladula


People also ask

What is Count return type?

COUNT() returns the number of items in a group, including NULL values and duplicates. COUNT(expression) evaluates expression for each row in a group and returns the number of non null values. Follow this answer to receive notifications.

How do you run a count query in Java?

The SQL Count() function returns the number of rows in a table. Using this you can get the number of rows in a table.

What does JDBC execute return?

execute : Returns true if the first object that the query returns is a ResultSet object. Use this method if the query could return one or more ResultSet objects. Retrieve the ResultSet objects returned from the query by repeatedly calling Statement.

Which function returns the number of occurrences in MySQL?

The COUNT() function returns the number of records returned by a select query.


1 Answers

long tmpLong = rs.getLong(1); 

should work from what I can recall.

EDIT:

of course:

rs.next();
like image 172
ZenMaster Avatar answered Sep 22 '22 07:09

ZenMaster