[英]Count mysql statement not returning correct value in java
I am getting a value of 9 from a count statement, but when I tried it in the localhost I am getting 18. Help me pls I'm stuck here thank you so much 我从count语句中得到的值是9,但是当我在localhost中尝试它时,得到的值是18。请帮助我,我被困在这里,非常感谢
Here is my code 这是我的代码
private int getgoods;
String query="Select count(item_id) as 'total' from item_tb";
PreparedStatement pst =conn.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next())
{
getgoods=rs.getInt("total");
}
JOptionPane.showMessageDialog(null, getgoods);
// supposedly to be 18 but im getting 9
Use *
instead of item_id
in the query like this, 像这样在查询中使用
*
代替item_id
,
String query="Select count(*) as 'total' from item_tb";
and use if(rs.next())
instead of while
loop. 并使用
if(rs.next())
代替while
循环。
Try following: 请尝试以下操作:
ResultSet rs = st.executeQuery("Select count(*) as 'total' from item_tb");
rs.next();
int count = rs.getInt(1);
or 要么
ResultSet rs = st.executeQuery("Select count(1) as 'total' from item_tb");
rs.next();
int count = rs.getInt(1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.