A simple getRowCount
method can look like this :
private int getRowCount(ResultSet resultSet) {
if (resultSet == null) {
return 0;
}
try {
resultSet.last();
return resultSet.getRow();
} catch (SQLException exp) {
exp.printStackTrace();
} finally {
try {
resultSet.beforeFirst();
} catch (SQLException exp) {
exp.printStackTrace();
}
}
return 0;
}
Just to be aware that this method will need a scroll sensitive resultSet
, so while creating the connection you have to specify the scroll option. Default is FORWARD and using this method will throw you exception.
//step3 create the statement object
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
No comments:
Post a Comment