7

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) {
  }
2
  • Just as a side note: When you just want to count the number of rows, a query like SELECT COUNT(*) FROM testdb.emg is way more efficient Commented Jan 22, 2012 at 11:17
  • It seems you are missing a } a copy paste error? Commented Jan 22, 2012 at 11:21

5 Answers 5

19

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.

Sign up to request clarification or add additional context in comments.

Comments

5

How to get count(*) mysql data table in java. TRY IT:

   public int getRowNumber(){

   int numberRow = 0;
   Connection mysqlConn = DriverManager.getConnection(HOST, USER_ID, PASSWORD);

try{
    mysqlConn.getConnection();
    String query = "select count(*) from dataTable";
    PreparedStatement st = mysqlConn.preparedStatement(query);
    ResultSet rs = st.executeQuery();
    while(rs.next()){
        numberRow = rs.getInt("count(*)");
    }
}catch (Exception ex){
    System.out.println(ex.getMessage());
}
return numberRow;
}

Comments

4

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.

2 Comments

I have change it, however, there's this message "missing return statement." I did put the return statement "return count;"
I've added explanation for this: you're not returning anything from the catch block.
0
public void num() throws Exception {

should be

public int num() throws Exception {

2 Comments

I have change it, however, there's this message "missing return statement." I did put the return statement "return count;"
you have no return if an Exception is raised .. you can put a return statement in the Exception or put on under the Exception. Maybe you would use 0 or -1 in this case as return value
0

I use Fahim Parker answer with a bit change

`

public int num() throws Exception {
 try {
 Class.forName("com.mysql.jdbc.Driver");
 connect = DriverManager.getConnection("jdbc:mysql://localhost/testdb?"
 + "user=root&password=");

 statement = connect.createStatement();
 resultSet = statement.executeQuery("<your query statement>");

 resultSet.last(); //go to last row;
 return resultSet.getRow(); //get row number which is equal to rows count

} catch (Exception e) {
}

`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.