Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java mysql计数行数_Java_Mysql_Sql - Fatal编程技术网

java mysql计数行数

java mysql计数行数,java,mysql,sql,Java,Mysql,Sql,我创建此代码是为了计算表中的行数。但是,我无法返回计数的数字,错误是“无法从结果类型为void的方法返回值”。有人能告诉我“我的错误”在哪里吗?非常感谢 public void num() throws Exception { try { // This will load the MySQL driver, each DB has its own driver Class.forName("com.mysql.jdbc.Driver"); // Setu

我创建此代码是为了计算表中的行数。但是,我无法返回计数的数字,错误是“无法从结果类型为void的方法返回值”。有人能告诉我“我的错误”在哪里吗?非常感谢

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

public void num() throws Exception {

您正在从变量
count
返回值,该变量的类型为
int
,因此该方法的返回类型也应为
int

您还应该确保通过代码的每个执行路径中都有一个
return
语句,包括
catch
块中的异常处理程序(否则您将收到一条“missing return statement”错误消息)。但是,最好避免捕获所有异常(如您的异常)的
catch
语句。此外,忽略(即不处理)catch块中的异常通常会导致难以诊断的问题,这是一种不好的做法

代码还有其他问题:除了
count
之外,没有声明任何变量

请注意,您可以使用以下SQL语句直接获取行数:

select count(*) from testdb.emg
这避免了将表
testdb.emg
中的所有数据发送到应用程序,对于大型表来说速度更快

public void num() throws Exception {
应该是

public int num() throws Exception {
试试下面的代码

 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) {
}
下面是错误

  • public void num()引发异常{

    应该是

    public int num() throws Exception {
    
    public int num()引发异常{

  • 要计算总行数,您应该使用query
    从testdb.emg中选择count(*)

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

    如何获取java中的count(*)mysql数据表。 试试看:


    我用了法希姆·帕克的答案,稍作改动

    `

    public int num()引发异常{
    试一试{
    Class.forName(“com.mysql.jdbc.Driver”);
    connect=DriverManager.getConnection(“jdbc:mysql://localhost/testdb?"
    +“user=root&password=”);
    语句=connect.createStatement();
    resultSet=statement.executeQuery(“”);
    resultSet.last();//转到最后一行;
    return resultSet.getRow();//获取等于行数的行数
    }捕获(例外e){
    }
    

    `

    作为旁注:当您只想计算行数时,类似testdb.emg的
    SELECT count(*)这样的查询效率更高。您似乎缺少一个
    }
    复制粘贴错误?我已经更改了它,但是,这里有一条消息“缺少返回语句”。我确实将返回语句放在了“返回计数”如果引发异常,则不会返回。您可以在异常中放置return语句,也可以在异常下放置on。在这种情况下,您可能会使用0或-1作为返回值。我已经更改了它,但是,出现了此消息“缺少return语句”。我确实放置了return语句“return count;”我添加了解释:您没有从
    catch
    块返回任何内容。
       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;
    }
    
    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) {
    }