Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java mysql count number of rows

Tags:

java

sql

mysql

I created this code to allow me calculate the number of rows in my table. However, I'm not able to return the counted number with an error saying "cannot return a value from method whose result type is void." Could someone show me where' my error? Thanks alot!

public void num() throws Exception {
  try {
      // This will load the MySQL driver, each DB has its own driver
      Class.forName("com.mysql.jdbc.Driver");
      // Setup the connection with the DB
      connect = DriverManager.getConnection("jdbc:mysql://localhost/testdb?"
      + "user=root&password=");

      // Statements allow to issue SQL queries to the database
      statement = connect.createStatement();
      resultSet = statement.executeQuery("select * from testdb.emg");
      int count = 0;
      while (resultSet.next()) {
        count++;
      }  
      return count;
  } catch (Exception e) {
  }
like image 817
Eugene Avatar asked Jan 22 '12 11:01

Eugene


2 Answers

Try below code

 public int num() throws Exception {
 try {
 // This will load the MySQL driver, each DB has its own driver
 Class.forName("com.mysql.jdbc.Driver");
 // Setup the connection with the DB
 connect = DriverManager.getConnection("jdbc:mysql://localhost/testdb?"
 + "user=root&password=");

 // Statements allow to issue SQL queries to the database
 statement = connect.createStatement();
 resultSet = statement.executeQuery("select count(*) from testdb.emg");

 while (resultSet.next()) {
 return resultSet.getInt(1);
 }
} catch (Exception e) {
}

Below were error

  1. public void num() throws Exception {

    should be

    public int num() throws Exception {

  2. For counting total rows you should use query select count(*) from testdb.emg

Let me know incase of any problem.

like image 132
Fahim Parkar Avatar answered Oct 03 '22 01:10

Fahim Parkar


Change

public void num() throws Exception {

to

public int num() throws Exception {

You are returning value from variable count which is of type int therefore the return type of the method should be int as well.

You should also make sure there is a return statement in every execution path through your code including the exception handler in the catch blocks (or you will get a "missing return statement" error message). However, it is best to avoid catch statements which catch all exceptions (like yours). Also, ignoring (i.e. not handling) exceptions in the catch block often leads to hard to diagnose problems and is a bad practice.

There are also other problems with the code: with the exception of count none of your variables have been declared.

Note that you may use the following SQL statement to obtain the number of rows directly:

select count(*) from testdb.emg

This avoids sending all of the data from table testdb.emg to your application and is much faster for big tables.

like image 37
Adam Zalcman Avatar answered Oct 03 '22 03:10

Adam Zalcman