*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+"' ");
Advertisement
Answer
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); }