*我在其他功能中没有得到max的值。值返回为“0”。我尝试但没有成功:( 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;
}
帮助!!!
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+"' ");
答案 0 :(得分:2)
你没有按原样使用它。
首先,您使用ÀVG
但需要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);
}
答案 1 :(得分:0)
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
}