fur866
fur866

Reputation: 413

Java: How to extract a Count from ResultSet Select query

This question has been answered before here but the solutions that were posted are not working for me. Following is my code:

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

It just keeps on saying column total not found. I have tried it without "resultSet.next()" as well but same issue. I have also tried resultSet.getInt(1) but that doesn't work either.

Upvotes: 1

Views: 1809

Answers (4)

Vignesh Shiv
Vignesh Shiv

Reputation: 1157

Change your query like this,

The SQL syntax is

Select COUNT(*) AS total FROM Questions

Upvotes: 0

user4695271
user4695271

Reputation:

Your SQL query is a little bit wrong. Try:

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

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53525

Change:

Select COUNT(*) FROM Questions AS total

to:

Select COUNT(*) AS total FROM Questions 

Upvotes: 1

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

The SQL syntax you want is

Select COUNT(*) AS total FROM Questions

or alternatively, you could just write Select COUNT(*) FROM Questions and use resultSet.getInt(1)

Upvotes: 1

Related Questions