Core code for getting the number of record columns in the ResultSet
The code is as follows: |
Copy code |
ResultSetMetaData rsmd = this. rs. getMetaData (); This. columnCount = rsmd. getColumnCount (); |
The code for java to obtain the number of data rows is as follows:
The code is as follows: |
Copy code |
Statement stmt = conn. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_READ_ONLY ); ResultSet rs = stmt.exe cuteQuery (SQL ); Rs. last (); Int length = rs. getRow ();
|
The value of length is the number of rows. If you want to continue using the current dataset rs after obtaining the number of rows, run rs. beforeFirst () to return the cursor to the initial position.
The common format created by Statement is:
The code is as follows: |
Copy code |
Statement stmt = con. createStatement (int type, int concurrency ); |
Instance 1. Use the select count statement and obtain the result from ResultSet:
The code is as follows: |
Copy code |
Try { Statement statement = connection. createStatement (); ResultSet resultSet = statement.exe cuteQuery ("select count (*) as rowCount from tableName "); ResultSet. next (); Int rowCount = resultSet. getInt ("rowCount "); } Catch (Exception e ){ // TODO: handle exception E. printStackTrace (); }
|
Instance 2. Traverse the Resultset and use a variable to record the number of rows. The code is as follows:
The code is as follows: |
Copy code |
Int count = 0; Try { While (resultSet. next ()){ Count = count + 1; } } Catch (SQLException e1 ){ // TODO Auto-generated catch block E1.printStackTrace (); } |
Instance 3
The code is as follows: |
Copy code |
Statement stmt = conn. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_READ_ONLY ); ResultSet rs = stmt.exe cuteQuery (SQL ); Rs. last (); Int length = rs. getRow ();
|
The value of length is the number of rows.
If you want to continue using the current dataset rs after obtaining the number of rows, run rs. beforeFirst () to return the cursor to the initial position.