Results 1 to 6 of 6
  1. #1
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Post How to get the no. of rows in a resultset

    Dear friends,

    I want to get the row count of a resultset (java-oracle). I have tried with the following code:

    Java Code:
    Connection objConnection = null;
    CallableStatement objStatement = null;
    
    objStatement = objConnection.prepareCall("{call sp1(?,?,?,?,?,?)}", java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
    
    objStatement.setInt(1,param1);
    objStatement.setString(2,param2);
    objStatement.setInt(3,param3);
    objStatement.setString(4,param4);
    objStatement.registerOutParameter(5,Types.INTEGER);
    objStatement.registerOutParameter(6,oracle.jdbc.driver.OracleTypes.CURSOR);
    objStatement.execute();
    
    Id=objStatement.getInt(5);
    					
    rs=(ResultSet)objStatement.getObject(6);
    
    int iCount=0;
    
    while(rs.next())
    {
         iCount++;
    }
    rs.beforeFirst();
    But i got the following exception in "rs.beforeFirst()" line:
    Java Code:
    java.sql.SQLException: Invalid operation for forward only resultset : beforeFirst
     2008-09-18 18:03:38,102  ERROR - 	at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:124)
     2008-09-18 18:03:38,102  ERROR - 	at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:161)
     2008-09-18 18:03:38,102  ERROR - 	at oracle.jdbc.driver.BaseResultSet.beforeFirst(BaseResultSet.java:66)
    I want to create a Object array to store the values(which i get from the resultset), so only i am in a need of row count.

    Any one have idea about this?

    Thanks in advance.
    Last edited by baskar.nitt; 09-18-2008 at 03:10 PM.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Instead of trying to get the length and using a for loop, why don't you stick the values into a vector inside the while loop?

    Java Code:
    //create vector
    while(rs.next()){
    //add data to vector
    }
    That's what I usually do.

  3. #3
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    ya i can put in vector.

    But we are using our own used-defined types. So have to store the resultset values in that type object array. If i store the resultset values in vector means once again i have to iterate the vector and have to store in type object array. So only i am in a need of resultset count. I think you understand my requirement..

  4. #4
    vk_satheesh is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default

    Hi Basker,

    i too faced the same problem to count the resultset value i too didnt have any solution in that if any one give any solution means it will be helpful

  5. #5
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    You could execute first
    Java Code:
    "select count(1) from ..."
    and then the other query. If you are sure the number of records is small enough you could also iterate once and put the records in a list, vector, map or stack, or whatever suits your needs.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  6. #6
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    hi,

    The following code block works for me. You have to use the latest oracle driver jar named "ojdbc14_g.jar".

    Java Code:
    rs - Resultset object in the following code:
    
    import oracle.jdbc.rowset.OracleCachedRowSet;
    
    OracleCachedRowSet ocrs = new OracleCachedRowSet();		
    ocrs.populate(rs);
    rs.close();
    ocrs.last();
    int iCount = ocrs.getRow();
    ocrs.beforeFirst();
    for(int i=0; i<iCount; i++)
    {			
    ocrs.next();
    ocrs.getString("name");
    
    //code to store in user-defined object array
    }
    
    ocrs.close();
    Last edited by baskar.nitt; 09-19-2008 at 01:39 PM.

Similar Threads

  1. Two diM aRRay and add rows and columns....
    By filly444 in forum New To Java
    Replies: 2
    Last Post: 08-30-2008, 05:24 PM
  2. Color cells and rows in SWT table
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:31 PM
  3. Deleting All rows in the JTable
    By surot in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 10:44 AM
  4. Fetching rows from DB
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-06-2008, 09:23 AM
  5. display rows in jtable
    By osval in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •