1 2 3 Previous Next 30 Replies Latest reply: Dec 5, 2008 11:43 AM by 843785 RSS

    how to find size of ResultSet in JAVA

    843785
      hi ,
      I have one problem... went for sun API but i did not get any solution....please any body help me... my problem is ......
      I got the data from DataBase by executing a query . so my Resultset having some records.Now my problem is how to find the size of ResultSet.without converting into list or without wile loop I want to find out size of my ResultSet

      ..... Is there any predefined method for that;


      thanks in advance
        • 1. Re: how to find size of ResultSet in JAVA
          masijade
          If there were it would be in the API for ResultSet or ResultSetMetaData. (There might be one in JRE 7, but I don't think so.)
          • 2. Re: how to find size of ResultSet in JAVA
            843785
            Why not execute a count query first?
            • 3. Re: how to find size of ResultSet in JAVA
              843785
              codingMonkey  
              ya you r correct , you know dealing with the database is more expensive. it takes more time for two queries . so i want to find in single query itself.but not count query ..


              so that's why............if any body knows please help me
              • 4. Re: how to find size of ResultSet in JAVA
                843785
                You can still do it using one query.
                "SELECT COUNT(*), t.* FROM TABLE t"
                (Works in MySQL)
                • 5. Re: how to find size of ResultSet in JAVA
                  masijade
                  codingMonkey wrote:
                  You can still do it using one query.
                  "SELECT COUNT(*), t.* FROM TABLE t"
                  (Works in MySQL)
                  In fact, that is the only reliable way to do it, as doing it in two queries may (and probably will if you have a very "active" data source) invalidate the "count" query before you ever get to the "data" query.
                  • 6. Re: how to find size of ResultSet in JAVA
                    843785
                      
                    
                    You can still do it using one query.
                    
                    "SELECT COUNT(*), t.* FROM TABLE t"
                     
                    
                    
                    (Works in MySQL) 
                    hay.... again you are diverting from topic. my problem is how to find resultset size.... not for how to write query for counting records.

                    for example my query is.
                    select * from emp
                    now i will get resultset after executing the query. so now how to find resultset size

                    thanks in advance

                    Edited by: gbabu on Dec 5, 2008 6:58 PM
                    • 7. Re: how to find size of ResultSet in JAVA
                      843785
                      In what way would:
                      SELECT COUNT(*), t.* FROM TABLE t
                      ever differ from the size of the result set?
                      • 8. Re: how to find size of ResultSet in JAVA
                        masijade
                        gbabu wrote:
                        hay.... again you are diverting from topic. my problem is how to find resultset size.... not for how to write query for counting records.
                        What do you think the resultset size is? The number of columns? No it's the number of rows, which is exactly what count(*) is. Every row returned will have the same value in that position, that value being the number of rows returned by the query, which, once again, is exactly what the resultset size is.

                        Why don't you try the suggestion before attempting to blast it.
                        • 9. Re: how to find size of ResultSet in JAVA
                          800323
                          gbabu wrote:
                            
                          
                          You can still do it using one query.
                          
                          "SELECT COUNT(*), t.* FROM TABLE t"
                          
                          
                          
                          (Works in MySQL) 
                          hay.... again you are diverting from topic. my problem is how to find resultset size.... not for how to write query for counting records.

                          for example my query is.
                          select * from emp
                          now i will get resultset after executing the query. so now how to find resultset size

                          thanks in advance

                          Edited by: gbabu on Dec 5, 2008 6:58 PM
                          Also, if you are worried about the "expense" of database operations (or best practices for that matter), you should definetly not be using
                          SELECT *
                          in your query.
                          • 10. Re: how to find size of ResultSet in JAVA
                            843785
                            gbabu wrote:
                            hay.... again you are diverting from topic. my problem is how to find resultset size.... not for how to write query for counting records.
                            Of cousre he is. Because the answer to your question is: it can't be done. So he's kindly moved on, and offered you an alternative way to achieve what you want. If that offends you, I suggest you have your head in an uncomfortable and unsuitable orifice

                            Why are you so desperate to know the size of the ResultSet anyway? If you've got limits on the size of ResultSet that's acceptable, you can adjust your query to limit the number of rows returned. if you simply need to know how many rows there are, pulling the entire content of the table(s) back and counting them afterwards is horrifically inefficient. If you need to know to so you can prepare a datastructure to put the results in, maybe you've chosen the wrong structure (I'm thinking 'array' here). You might have a legitimate reason to find out the size of a ResultSet, I don't know. But the ResultSet interface doesn't give you that option, so you're on a hiding to nothing, and might as well use the suggested alternative, or reconsider your requirement
                            • 11. Re: how to find size of ResultSet in JAVA
                              843785
                              gbabu wrote:
                              Now my problem is how to find the size of ResultSet.without converting into list
                              Why not? That's the first thing most code does with a SELECT query -- iterate through the ResultSet, creating business objects and adding them to a collection. Sounds pretty good darn right now, don't it, buddy?
                              • 12. Re: how to find size of ResultSet in JAVA
                                843785
                                BigDaddyLoveHandles wrote:
                                gbabu wrote:
                                Now my problem is how to find the size of ResultSet.without converting into list
                                Why not? That's the first thing most code does with a SELECT query -- iterate through the ResultSet, creating business objects and adding them to a collection. Sounds pretty good darn right now, don't it, buddy?
                                UI populating itself directly from the ResultSet would be my guess. "How many rows deep do I have to be?"
                                • 13. Re: how to find size of ResultSet in JAVA
                                  843785
                                  georgemc wrote:
                                  BigDaddyLoveHandles wrote:
                                  gbabu wrote:
                                  Now my problem is how to find the size of ResultSet.without converting into list
                                  Why not? That's the first thing most code does with a SELECT query -- iterate through the ResultSet, creating business objects and adding them to a collection. Sounds pretty good darn right now, don't it, buddy?
                                  UI populating itself directly from the ResultSet would be my guess. "How many rows deep do I have to be?"
                                  In that case UI should be pronounced: "euw-eek!". Let the presentation layer be database agnostic.
                                  • 14. Re: how to find size of ResultSet in JAVA
                                    796447
                                    hay.... again you are diverting from topic. my problem is how to find resultset size.... not for how to write query for counting records.
                                    Is this you, Babu?
                                    [http://www.youtube.com/watch?v=b4_TeT4XhlY]
                                    1 2 3 Previous Next