0

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 "com.mysql.jdbc.JDBC42ResultSet@4a9b1e8b" or "com.mysql.jdbc.JDBC42PreparedStatement@4a9b1e8b" (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.

episkeyyy
  • 109
  • 11

1 Answers1

1

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

Have a look at Oracle's documentation on JDBC

You can also have a look at this post on SO

Community
  • 1
  • 1
C.Champagne
  • 5,381
  • 2
  • 23
  • 35