The ResultSet API in JDBC does not have a way to get the number of records directly, and now describes several:
Method one: Using ResultSet's GetRow method to get the total number of resultset
Java code ResultSet RS; Rs.last (); Move to the last line int rowCount = Rs.getrow (); Get the current line number, which is the number of records Rs.beforefirst (); If you want to use the result set again, move the pointer to the initialized position
Possible exceptions: Java.sql.SQLException: Invalid operation for forward only result set: Last exception resolved
Beginners do not pay much attention to the establishment of objects, and later know that in the establishment of statement when the relevant parameters can be solved. The statement that an
exception occurred during the operation of the query result set
: Rs.last ();
Exception Details: Invalid operation for forwarding only result sets: Last
solution: stat = conn.createstatement ();
Changed to Stmt=conn.createstatement (Resultset.type_scroll_insensitive,resultset.concur_read_only); It is possible to
parse: An exception occurs when a pointer to a moving result set is generated because the parameter that is supplied when generating the statement object is
using the default parameter, and the result set type obtained after statement execution is Resultset.type_forward_only. This type of result set can only be read by Rs.next (), Method-by-article, using other methods to report an exception. If you want to perform some complicated moving result set pointers, you will need to use other parameters. For a
brief introduction of each parameter:
resultset.type_forward_only (slightly)
resultset.type_ Scroll_insensitive Two-way scrolling, but not in time to update, that is, if the data in the database has been modified, not in the resultset reaction.
resultset.type_scroll_sensitive Two-way scrolling and keeps track of updates in the database in order to change the data in the ResultSet.
resultset.concur_read_only only read ResultSet
resultset.concur_updatable Update database with ResultSet
Method Two: Using the elements of cyclic resultset to get the total number of resultset
Java code ResultSet RS; int rowCount = 0; while (Rset.next ()) {rowcount++; }
Method Three: Use the Count function in the SQL statement to get the total number of resultset
Java code String sql = "SELECT COUNT (*) Record_ from (SELECT * from yourtable t where T.column_ = ' value_ ')"; ResultSet rs = ps.executequery (SQL); int rowCount = 0; if (Rs.next ()) {Rowcount=rs.getint ("Record_"); }