episkeyyy episkeyyy - 10 months ago 27
HTML Question

Get the value of COUNT(*) into an html table

I'm using a servlet to make the jdbc connection, write the PreparedStatements and execute ResultSets. I am able to display the data into a webpage just fine, however I also want to be able to count the number of entries. I know there are other ways to count how many rows I have using java code, but I want to use SQL statements and I saw this

SELECT COUNT(*) FROM table_name;

and made a preparedstatement and tried to execute. However, it is not returning the value of the count, I instead get
(because I tried getting the value of the count using both).

Basically, I am wondering how to get the count value in my html table from the servlet, not the long statements above.

Many thanks, I'm a beginner.


When you execute your SQL query with JDBC, you get a Resultset even when you get only one record with only one field as in your question.

You need to call the getInt or the getLong method of your recordset to get the actual value.

long countValue = rs.getLong(0);

Have a look at Oracle's documentation on JDBC

You can also have a look at this post on SO