get Record Count in ResultSet - Java java.sql

Java examples for java.sql:ResultSet

Description

get Record Count in ResultSet

Demo Code


//package com.java2s;
import java.sql.*;

public class Main {
    public static int getRecordCount(ResultSet rs) {
        if (rs == null)
            return 0;

        try {//  w ww .jav  a 2 s .c  o m
            if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY)
                return 0;
            else {
                int pos = rs.getRow();
                rs.last();
                int count = rs.getRow();
                if (pos == 0)
                    rs.first();
                else
                    rs.absolute(pos);

                return count;
            }
        } catch (SQLException e) {
            return 0;
        }
    }
}

Related Tutorials