简体   繁体   中英

how to find the maximum value of a column mysql and return the value?

I want to search the maximum value of a column but the value found receives 0 all the time

public  int maXnumR()
{
    DataBase s = DataBase.getInstance();
    int numR= getnumR();

    String req1 = "SELECT max(`idrf`) FROM   `reference`  WHERE `numR` = "
        + numR + " GROUP BY `numR` ";

    try 
    {
        Statement m=  s.getConn().createStatement();
        ResultSet r1 = m.executeQuery(req1);
        while (r1.next()) 
        {
            maxnumR =r1.getInt("idrf");
            nbp++;  
        } 
    } 
    catch (SQLException e1) 
    {
        e1.printStackTrace();
        System.out.println("maXnumR : "+e1);
    } 
    return maxnumR;
}

maXnumR return 0 and the table is not empty.

if I execute the query it works fine with MySQL

erreur:

java.sql.SQLException: Column 'idrf' not found.
maXnumR : java.sql.SQLException: Column 'idrf' not found.
idrf existe:0

尝试

SELECT max(`idrf`) as idrf

Like Luca said, the problem is that your query doesn't return a column named "idrf", but something more like: "max(idrf)".

This means that when you use getInt("idrf") there is no match in the resultSet returned by your query.

That being said, you need to use

SELECT max(`idrf`) as idrf

(or any other name for your resultSet column), like Luca answered, and use that as the key to your getInt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM