Get Number of Rows returned by ResultSet in Java

前端 未结 14 1270
挽巷
挽巷 2020-11-29 20:35

I have used a ResultSet that returns certain number of rows. My code is something like this:

ResultSet res = getData();
if(!res.next())
{
    Sy         


        
14条回答
  •  被撕碎了的回忆
    2020-11-29 21:05

    You could count with sql and retrieve the answer from the resultset like so:

    Statment stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
                                         ResultSet.CONCUR_READ_ONLY);
    ResultSet ct = stmt.executeQuery("SELECT COUNT(*) FROM [table_name]");
    if(ct.next()){
       td.setTotalNumRows(ct.getInt(1));
    }
    

    Here I'm counting everything but you can easily modify the SQL to count based on a criteria.

提交回复
热议问题