查询返回的访问计数

时间:2014-03-25 06:29:22

标签: mysql sql database

Connection con = ConnectionProvider.getCon();
Statement s_sickLeave = con.createStatement("select count(leavetypeid) from leave where leavetypeid=2");
ResultSet rs_sickLeave = s_sickLeave.executeQuery();
Integer sick_leave_count = rs_sickLeave.getInt(1);//line 4

我在第4行遇到异常。如何访问查询返回的计数值?

1 个答案:

答案 0 :(得分:0)

您必须遍历结果集才能读取每一行。

更改

ResultSet rs_sickLeave = s_sickLeave.executeQuery();
Integer sick_leave_count = rs_sickLeave.getInt(1);//line 4

ResultSet rs_sickLeave = s_sickLeave.executeQuery();
if ( rs_sickLeave.next() ) {
    Integer sick_leave_count = rs_sickLeave.getInt(1);//line 4
}

count(x)返回单行时,您只能迭代一行。否则使用while(rs.next())循环语法。

请参阅

ResultSet.next()
将光标从当前位置向前移动一行。 ResultSet游标最初位于第一行之前;对方法的第一次调用使得第一行成为当前行;第二个调用使第二行成为当前行,依此类推。