3

*I don't get value of max in function other. Value return is "0". I trying but not success :( Image

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;
    }

Help!!!

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+"' ");
2
  • max = rs.getInt(sql);? Does this code even compile? Commented May 28, 2016 at 15:21
  • Ok, I try change object Commented May 28, 2016 at 15:22

2 Answers 2

6

You're not using it as it should be.

First of all, you use AVG but want MAX so change it to MAX(GiaSP). Second, you must use rs.next() to have your cursor go to the first row and then get information from it.

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have a mistake "avg" with "max" @@
1
ResultSet rs = stmt.executeQuery("select MAX(GiaSP) as maxGiaSP from tbsanpham where manhom = '"+manhom+"'");
if (rs.next())
{
    int w = rs.getInt("maxGiaSP ");

    // just return this int
}

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.