*我没有在其他函数中得到 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+"' ");
最佳答案
您没有按应有的方式使用它。
首先,您使用 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);
}
关于java - 在Java中获取最大值sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37500709/