在 Java 中获取最大值 sql

Chu*_*ack 3 java sql max

*我没有得到其他函数中的最大值。价值回报为“0”。我尝试但没有成功:( 图片

public int PriceMax(int manhom){
        Connection conn = this.connect();
        int max = 0;
        if(conn != null){
            try {
                java.sql.Statement statement = conn.createStatement();
                String sql = "SELECT AVG(GiaSP) from tbsanpham where manhom = '"+manhom+"'";
                ResultSet rs = statement.executeQuery(sql);
                max = rs.getInt(sql);
            } catch (SQLException ex) {
                Logger.getLogger(CSDL.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return max;
    }
Run Code Online (Sandbox Code Playgroud)

帮助!!!

int manhom = cbbNhomSanPham.getSelectedIndex();
        CSDL csdl = new CSDL();
        int max = csdl.PriceMax(manhom);
        JOptionPane.showMessageDialog(null, "Nhóm s?n ph?m: '"+cbbNhomSanPham.getName()+"' \nPrice max: '"+max+"' ");
Run Code Online (Sandbox Code Playgroud)

Yas*_*jaj 6

你没有像它应该的那样使用它。

首先,您使用AVG但希望MAX将其更改为MAX(GiaSP). 其次,您必须使用rs.next()让光标移动到第一行,然后从中获取信息。

java.sql.Statement statement = conn.createStatement();
String sql = "SELECT MAX(GiaSP) from tbsanpham where manhom = '"+manhom+"'";
ResultSet rs = statement.executeQuery(sql);
if (rs.next()) {
    max = rs.getInt(1);
}
Run Code Online (Sandbox Code Playgroud)