Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Total Number of Row Resultset getRow Method

Tags:

Read the Following Code:

public class selectTable {  public static ResultSet rSet; public static int total=0; public static ResultSet onLoad_Opetations(Connection Conn, int rownum,String sql) { int rowNum=rownum; int totalrec=0; try {    Conn=ConnectionODBC.getConnection();    Statement stmt = Conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);             String sqlStmt = sql;             rSet = stmt.executeQuery(sqlStmt);     total = rSet.getRow();             }     catch(Exception e)     {         System.out.println(e.getMessage());     }     System.out.println("Total Number of Records="+totalrec);     return rSet;     }  } 

The folowing code dos't show actual total:

total = rSet.getRow(); 

my jTable display 4 record in jTable but total = 0; when I evaluate through debug, it shows:

total=(int)0;  

rather than total=(int)4 And if I use

rSet=last(); above from the code  total = rSet.getRow(); 

then total shows accurate value = 4 but rSet return nothing. then jTable is empty. Update me!

like image 797
Shahid Ghafoor Avatar asked Sep 25 '11 13:09

Shahid Ghafoor


People also ask

How do I find the number of rows in a ResultSet?

Getting the number of rows using methodsThe last() method of the ResultSet interface moves the cursor to the last row of the ResultSet and, the getRow() method returns the index/position of the current row.

How do you find the count of a ResultSet?

You can get the column count in a table using the getColumnCount() method of the ResultSetMetaData interface. On invoking, this method returns an integer representing the number of columns in the table in the current ResultSet object.

Which function returns the number of rows in ResultSet?

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. COUNT() returns 0 if there were no matching rows.

How do I count the number of rows in Java?

The SQL Count() function returns the number of rows in a table. Using this you can get the number of rows in a table.


2 Answers

BalusC's answer is right! but I have to mention according to the user instance variable such as:

rSet.last();  total = rSet.getRow(); 

and then which you are missing

rSet.beforeFirst(); 

the remaining code is same you will get your desire result.

like image 186
Both FM Avatar answered Nov 21 '22 11:11

Both FM


You need to call ResultSet#beforeFirst() to put the cursor back to before the first row before you return the ResultSet object. This way the user will be able to use next() the usual way.

resultSet.last(); rows = resultSet.getRow(); resultSet.beforeFirst(); return resultSet; 

However, you have bigger problems with the code given as far. It's leaking DB resources and it is also not a proper OOP approach. Lookup the DAO pattern. Ultimately you'd like to end up as

public List<Operations> list() throws SQLException {     // Declare Connection, Statement, ResultSet, List<Operation>.      try {         // Use Connection, Statement, ResultSet.          while (resultSet.next()) {             // Add new Operation to list.         }     } finally {         // Close ResultSet, Statement, Connection.     }      return list; } 

This way the caller has just to use List#size() to know about the number of records.

like image 35
BalusC Avatar answered Nov 21 '22 11:11

BalusC