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