How to find the number of records in a ResultSet

I get a ResultSet after an Oracle request. when I repeat through ResultSet its motion in an infinite loop.

 ResultSet rs = (ResultSet) // getting from statement while (rs.next()) { // // } 

this loop does not end, so I tried to find the number of records using rs.getFetchSize() and return a value of 10 . I want to know if this is the right method to find out the number of records in a ResultSet, and if the count is 10, why does it go in an infinite loop. Please give your opinion.

+4
source share
7 answers

Actually, the ResultSet has no idea about the actual number of rows that it will return. In fact, using a hierarchical query or pipeline function, the number can also be infinite. 10 is the suggested number of rows that the result set should / try to extract in one operation. (see comment below).

It is best to check your query if it returns more rows than you expect.

+6
source

To find out the number of records available, try the following code

 ResultSet rs = // getting from statement try { boolean b = rs.last(); int numberOfRecords = 0; if(b){ numberOfRecords = rs.getRow(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
+5
source

A simple getRowCount method might 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; } 

To use this method, your profile must scroll.

Just looked what it looks like similar lines on this question

+2
source

When you execute the request and get a ResultSet, I would say that at this moment you or even the program itself do not see how many results will be returned, this case is very similar to Oracle CURSOR, just declare Oracle what you want to do such a request, therefore, for each ResultSet we must get a row one by one to the last.

As already mentioned above, the guys answered: rs.last will iterate to the last, at this time the program has the ability to fully return the number of lines.

+1
source
 if(res.getRow()>0) { // Data present in resultset<br> } else { //Data not present in resultset<br> } 
0
source

You can see the code snippet below, where you can find how to calculate the loaded number of records from the data set. This example works with an external data set (in json format), so you can start with yours. The necessary code fragment is placed in the controller script controller (this page is based on the ApPML JavaScript-script, and cotroller works with loaded ApPML objects). The code in the controller returns the number of loaded dataset recorders and the number of data model fields.

 <!DOCTYPE html> <html lang="en-US"> <title>Customers</title> <style> body {font: 14px Verdana, sans-serif;} h1 { color: #996600; } table { width: 100%;border-collapse: collapse; } th, td { border: 1px solid grey;padding: 5px;text-align: left; } table tr:nth-child(odd) {background-color: #f1f1f1;} </style> <script src="http://www.w3schools.com/appml/2.0.2/appml.js"></script> <body> <div appml-data="http://www.w3schools.com/appml/customers.aspx" appml-controller="LukController"> <h1>Customers</h1> <p></p> <b>It was loaded {{totalRec}} records in total.</b> <p></p> <table> <tr> <th>Customer</th> <th>City</th> <th>Country</th> </tr> <tr appml-repeat="records"> <td>{{CustomerName}}</td> <td>{{City}}</td> <td>{{Country}}</td> </tr> </table> </div> <script> function LukController($appml) { if ($appml.message == "loaded") { $appml.totalRec = Object.keys($appml.data.records).length; } } // ***************************************************************** // Message Description // // ready Sent after AppML is initiated, and ready to load data. // loaded Sent after AppML is fully loaded, ready to display data. // display Sent before AppML displays a data item. // done Sent after AppML is done (finished displaying). // submit Sent before AppML submits data. // error Sent after AppML has encountered an error. // ***************************************************************** </script> </body> </html> 
0
source

I got the answer: - The following are the steps you need to follow:

  • Make sure you are using a select query (for example, select * from an employee).
  • Do not use the count query (for example, select count (*) for the employee).

Then use the following steps:

 Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery("select * from employee"); while(rs.next()){ rowCount++; } return rowCount; } 

where rs is the ResultSet object.

Then you will get the exact number of rows.

0
source

Source: https://habr.com/ru/post/1384056/


All Articles