Java MySQL 计算行数

7

我创建了这段代码来计算我的表格中的行数。然而,当我试图返回计数的数字时却出现了一个错误提示:“无法从结果类型为空的方法中返回值”。请问有人能告诉我错在哪里吗?非常感谢!

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) {
  }

只是顺便提一下:当你只想计算行数时,像 SELECT COUNT(*) FROM testdb.emg 这样的查询更加高效。 - klaustopher
你好像缺少了一个 },是复制粘贴错误吗? - amit
5个回答

19

尝试以下代码

 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) {
}

以下是错误信息:

  1. public void num() throws Exception {

    应该改为

    public int num() throws Exception {

  2. 要计算总行数,您应该使用查询 select count(*) from testdb.emg

如果有任何问题,请告诉我。


5

如何在Java中获取MySQL数据表的count(*)。

尝试以下代码:
   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;
}

4

更改

public void num() throws Exception {

to

public int num() throws Exception {

你正在从类型为 int 的变量 count 中返回值,因此,方法的返回类型也应该是 int
你还需要确保你的代码中每个执行路径都有一个 return 语句,包括 catch 块中的异常处理程序(否则你将得到 "missing return statement" 错误消息)。不过,最好避免像你的一样捕获所有异常的 catch 语句。而且,在 catch 块中忽略(即不处理)异常通常会导致难以诊断的问题,并且是一种不好的做法。
此外,你的代码还存在其他问题:除了 count 之外,你的变量都没有声明。
请注意,你可以使用以下 SQL 语句直接获取行数:
select count(*) from testdb.emg

这可以避免将testdb.emg表的所有数据发送到您的应用程序,对于大表格而言速度更快。

我已经修改过了,然而还有这个错误信息“缺少返回语句”。我确实添加了返回语句“return count;”。 - Eugene
我已经为此添加了解释:你没有从catch块中返回任何内容。 - Adam Zalcman

0

我使用了 Fahim Parker 的答案并稍作修改

`

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) {
}

`


0
public void num() throws Exception {

应该是

public int num() throws Exception {

我已经修改了它,但是出现了“缺少返回语句”的消息。我确实放置了返回语句“return count;”。 - Eugene
如果出现异常,您将没有返回值.. 您可以在异常中放置一个返回语句或在异常下放置一个返回语句。在这种情况下,您可能会使用0或-1作为返回值。 - rauschen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接