Counting rows in a ResultSet, help please?

sorry there was a type error
how to count the number of rows in a resultser object which may contains millions of rows? besides using a while loop? thanks

You can do rs.last() then get the number of rows using rs.getRow() but as the previous poster hinted at this will take very long time with a lot of records as they all have to be retrieved before the rs.last() method can complete.

Similar Messages

  • Counting Rows from a ResultSet

    Hi!
    If I retreive some information from a database, say by using:
    ResultSet RS = Stmt.executeQuery("select * from events");How can I get a value for the amount of rows in this ResultSet?
    Thanks!
    Alastair

    Almost everyone, when they first use ResultSet thinks of it as a container with the data rows already in there, and wonders why there's no size method.
    But that's not how they work. A ResultSet, generally, holds only one row at a time. next requests the next row from the database and before next is called the ResultSet doesn't know if another row is going to be available. Indeed the number of rows that qualify for return in the result set may actually change while you are processing it.
    Doing the SELECT COUNT(*) request asks the database for the number of rows that match any criteria in the SELECT, but that number may have changed when you actually retrieve the rows. Someone else may have added or removed rows.

  • Can i count row from ResultSet ?

    I would like to count row from ResultSet for
    take it into my array object because i must know number of row before create array object.
    Example:
    ResultSet rset = stmt.executeQuery("select * from user ");
    /*i = amount of ResultSet*/
    User[] user = new User;
    int l=0;
    while (rset.next()){
    user[l] = new User();
    user.name = rset.getString(1);
    l++;

    Hi,
    As per my knowledge there is no method by which you can get the count of items in a resultset directly. You will have to loop through the reseltset and set a variable for count. In your specific case I would advise you to use a Vetor instead of an array so that you need not bother about the size.
    ResultSet rset = stmt.executeQuery("select * from user ");
    Vector user = new Vector();
    while (rset.next()){
    user.addElement(rset.getString(1));
    Now you will have a Vector that holds the user info. To retrieve the user info loop through the Vector.
    for (int i; i<user.size(); i++){
    userName = user.elementAt(i);
    Hope I was of some help.
    cheers!!!
    Nish

  • An app counting minutes and data use. Please help!!!

    An app counting minutes and data use. Please help!!!

    Here's one, there are others in the App store:
    http://itunes.apple.com/us/app/data-usage/id386950560?mt=8
    However, since your carrier is the one that bills you, the only stats that matter are your carrier's. Some carrier's offer apps that provide this info, see if yours does.

  • Counting the rows in a ResultSet

    I am wondering how I could find out the number of rows in a ResultSet after performing a query?

    In case anyone's curious I figured out the problem. The ResultSet needs to be set to move forward AND backward. This is done as follows:
    Statement stmt = conn.createStatement(
                             ResultSet.TYPE_SCROLL_INSENSITIVE,
                                  ResultSet.CONCUR_READ_ONLY);

  • Count rows from multiple tables using SQL only

    Hi, I know this has probably been answered before, but I couldn't find the answer anywhere. Please help.
    I'd like count(*) [rows] for all tables in database using SQL only - no PL/SQL
    The result should be something like:
    Table RowCount
    DBA_TABLES 1000
    DBA_USERS 50
    etc.
    Thanks!

    offcource write this script:
    create or replace procedure count_tables (ip_schema VARCHAR2)
    is
    lv_owner VARCHAR2(100);
    lv_table_name VARCHAR2(100);
    lv_sql_statement VARCHAR2(2000);
    lv_count_table NUMBER;
    CURSOR c1 IS
    SELECT owner, table_name
    FROM all_tables
    WHERE owner = ip_schema
    ORDER BY table_name;
    begin
    dbms_output.put_line ('+--------------------------------------------------------------------+');
    dbms_output.put_line ('¦ | | ¦');
    dbms_output.put_line ('¦ Schema Name | Table Name | Number of Rows ¦');
    dbms_output.put_line ('¦ | | ¦');
    dbms_output.put_line ('¦------------------------------------------------------------------¦');
    OPEN c1;
    LOOP
    FETCH c1 INTO lv_owner , lv_table_name;
    EXIT WHEN c1%NOTFOUND;
    lv_sql_statement := 'SELECT count(*) FROM ' || lv_owner || '.' || lv_table_name;
    EXECUTE IMMEDIATE lv_sql_statement INTO lv_count_table;
    IF lv_count_table > 0 THEN
    dbms_output.put_line ('| '||rpad(lv_owner, 14, ' ')||'| '|| rpad(lv_table_name, 32, ' ')||'| '|| rpad(lv_count_table, 16, ' ')||' |');
    -- dbms_output.put_line ('|---------------|---------------------------------|------------------|');
    END IF;
    END LOOP;
    CLOSE c1;
    dbms_output.put_line ('+--------------------------------------------------------------------+');
    exception
    WHEN OTHERS THEN
    dbms_output.put_line ('owner: '||lv_owner||' - table: '||lv_table_name||' - '||sqlerrm);
    end count_tables;
    set serveroutput on size 1000000
    exec count_tables
    drop procedure count_tables;

  • Count rows from several tables

    hello,
    im trying to count row from multiple tables
    for example i need the select statement to produce the following
    table_name count
    table1 5
    table2 6
    table3 3
    i came up with the following script but it counts the number of tables i have
    select object_name, (select count(*) from user_tables where table_name = object_name) from all_objects
    where object_type = 'TABLE'

    Manik wrote:
    May be possible:
    Check this:
    SELECT table_name,
    TO_NUMBER (
    EXTRACTVALUE (
    xmltype (
    DBMS_XMLGEN.getxml ('select count(*) c from ' || table_name)),
    '/ROWSET/ROW/C'))
    COUNT
    FROM (select * from all_tables where table_name in ('TABLE1','TABLE2'))
    WHERE owner = 'SCOTT';Cheers,
    Manik.Awesome Manik... Just too good. Thanks.
    I wish i could have given you the 'Correct' points. ;-)
    Can you please explain the logic in brief? Will be helpful for everybody to understand...

  • Database don't delete prom the table, help please !!

    there is my code :
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    Connection con=DriverManager.getConnection(jdbc:odbc:Medic);
    con.setReadOnly(false);
    try{
    Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    int res=st.executeUpdate("DELETE FROM PATIENT WHERE PATIEN_ID="+id+";");
    System.out.println(String.valueOf(res)+" rows deleted");
    st.close();
    }catch(Exception exc){
    System.out.print(exc+"\n");
    the code execute with no error or exception , the message that i recieve is "1 rows deleted" and i want delete just one row, it's ok until the moment but whene i read the table i see that the row is not deleted.
    I use dBase database, help please !!!
    Thanks

    Don't trust the return value.
    Because you got no exception it means that the SQL executed. SQL can execute without deleting anything however if the where clause does not match.
    As posted your code is not valid java so anything else is meaningless.

  • Multiselect problem Help please!!

    Hi all,
    I am currently using Application Express 3.1.2.00.02 I have developed an online questionnaire with a multiselect list for one of the questions. The problem i am having is that i need each choice to be totaled up separately on the report generated.
    My table of answers looks like this:
    Column name e.g. (Question 9)
    Answer "Dog" "Cat" "Mouse" "Snake" (underneath column name Question 9 depending on what is selected).
    The reporting page would look like below:
    *(Column Question 9)* Count
    Dog 1
    Mouse 1
    Snake 1
    and for the question you could have another respondent who chooses similar of items on the multiselect, so their answer looks like this:
    Answer
    "Question 9" Dog
    "Question 9" Snake
    This would then start a new row on the reporting page like below which is not what i want:
    *(Column Question 9)* Count
    Dog 1
    Mouse 1
    Snake 1
    Dog: Snake 1
    But the report should look like this:
    *(Column Question 9)* Count
    Dog 2
    Mouse 1
    Snake 2
    The current SQLquery i am using is below:
    select "QUESTION 9",
    count(*) c
    from "P1 DQUESTIONNAIRE"
    group by "QUESTION 9"
    Thanks for your help and suggestions,
    'S'

    How to make sure your forum post will never be answered:
    1) Make sure your subject line is completely meaningless (i.e. HELP PLEASE!) and describes no part of your actual problem
    2) State that you are having "a few problems" but make sure to offer no details whatsoever what those problems are or what the symptoms are.
    3) Post large volumes of code instead of distilling the problem down to a compact snippet
    4) For extra points, make the code hard to read by refusing to make use of the "code" tag.

  • My Mac Book Pro was really slow at everything and would kick me out of my photos every time i got on them. now it won't even allow me to log on. i put my password in and the screen will flash white and go back to the login page. HELP please...

    My Mac Book Pro was really slow at everything and would kick me out of my photos every time i got on them. now it won't even allow me to log on. i put my password in and the screen will flash white and go back to the login page. HELP please...

    There is nothing wrong with your Dell, it will work fine with any MacbookPro. I have been using Dell displays for over 12 years with many different Mac models. I have two 21" Ultra Sharp displays working side by side to design a Keynote presentation right now.
    The issue your having is with the way Keynote  takes control of the video output to both displays, it sends the presentation signal to one and the presenter display to the other, this is set up in;
    Keynote preferences > Presenter display.
    If you want to show a wesite or another app on  either display,  use application switcher:
    press the the  command key on the keyboard, then the tab key; a row of applications will show what applications are running, choose which one you want to show. Use command  > tab to return to Keynote.

  • Excel ActiveX: Count Rows

    Hi, I am trying to simply count the used rows in an excel spreadsheet (which I have done in the past).
    Attached is a simple vi which I am trying to get to work.  On my development PC, this works fine, counts the rows etc...on this PC I have Excel 2003.
    When I create an EXE out of this VI and put it on another PC with Excel 2000, it returns "0" for the number of used rows.
    Can ANYONE help me here, this is driving me insane.
    Working in 7.1 (can use 7.0 or 8.0 if it makes it easier)
    Thanks,
    Ryan
    Ryan
    LV 7.1
    Attachments:
    test.vi ‏59 KB

    It is limiting but I place the biggest blame on Microsoft for changing the properties and methods so much between different releases. You could put all of your Excel functions in a library that you call dynamically. That way, you don't have to re-build the exe but just distribute the library specific to whatever version of Excel you need to support. Unless you really need some specific Excel formatting, you could also save to comma/tab delimited text files. All versions of Excel can open them and you won't have to make a career out of modifying your application for as long as Excel is going to be around.

  • I cannot update apps on ipod. Help please.

    So before, i already posted a question about this and i just figured out that it is my internet conection that has the problem, not the ipod.
    It seems that my internet connection or router settings has a problem connecting to my ipod or i don't know... Please help.
    Usually, when there is an app update, a number pops up beside the app store icon, right?
    And when you open the app store:
    WHEN THERE IS AN APP UPDATE - it displays all the apps that needs to be updated
    WHEN THERE'S NONE  - it displays, ALL APPS ARE UP TO DATE.
    My problem is...
    When i connect to the internet at school, it shows the updates or the all apps are up to date when i go to the app store updates tab.
    recently, i leave the update counter (the one beside the app store icon) until i get home so i can be sure there are apps that needs to be updated.
    When i'm home and connect to the internet. Open the app store >updates tab:
    WHEN THERE IS AN APP UPDATE - it keeps on loading.
    WHEN THERE'S NONE  - it keeps on loading.
    And when there's an update counter and i connect to the app store at home, it disappears.
    Please help.. i cannot update my apps on my ipod. There isn't any problem with my device. I just restored it and i started again from scratch. No pictures, music, just apps.
    HELP PLEASE. THANK YOU.

    Please help.. Thank you.

  • Returning a subset of rows through a ResultSet WITHOUT rownum?

    I am trying to figure out a way to get a subset of rows from a ResultSet (or ResultSet equivalent) without using rownum and without sending all the rows before the requested row over the wire.
    My problem is this: I am not in control of the SQL that is sent to the database. It is autogenerated by a third party tool (TOPLink). Therefore using the rownum solution isn't practical. My steps of operation are:
    1. Get SQL from TOPLink
    2. Pass SQL through a Statement
    3. Get resulting ResultSet
    At this point, I want to say, "Mr. ResultSet, I only want rows 200-250." And I then want Mr. ResultSet to fetch only rows 200 to 250, and no others. I don't want to see rows 1 to 199 coming over the wire.
    Anyone have any ideas? Is this possible at all? Also, if there's a way to do this through the TOPLink mechanisms (ScrollableCursors and ReportQuery objects and whatnot), please do let me know. I have tried using the TOPLink facilities, and sure enough rows 1 to 199 flow across the network.
    Thanks,
    Michael Allen
    [email protected]

    Hi Michael,
    Is there some sort of selection criteria in which you could use return the required 200-250 rows of data? If so, I would embed that into the TopLink query.
    Another option is using the ORDER BY operator. Without order, constructing a query of elements 200-250 twice would not guarentee that they would be the same elements.
    You can also construct your own SQL statement and execute it directly on a TopLink session.
    Darren Melanson
    Strategic Implementation Consultant
    Oracle Canada.
    I am trying to figure out a way to get a subset of rows from a ResultSet (or ResultSet equivalent) without using rownum and without sending all the rows before the requested row over the wire.
    My problem is this: I am not in control of the SQL that is sent to the database. It is autogenerated by a third party tool (TOPLink). Therefore using the rownum solution isn't practical. My steps of operation are:
    1. Get SQL from TOPLink
    2. Pass SQL through a Statement
    3. Get resulting ResultSet
    At this point, I want to say, "Mr. ResultSet, I only want rows 200-250." And I then want Mr. ResultSet to fetch only rows 200 to 250, and no others. I don't want to see rows 1 to 199 coming over the wire.
    Anyone have any ideas? Is this possible at all? Also, if there's a way to do this through the TOPLink mechanisms (ScrollableCursors and ReportQuery objects and whatnot), please do let me know. I have tried using the TOPLink facilities, and sure enough rows 1 to 199 flow across the network.
    Thanks,
    Michael Allen
    [email protected]

  • How do you return the number of Rows in a ResultSet??

    How do you return the number of Rows in a ResultSet? It's easy enough to do in the SQL query using COUNT(*) but surely JDBC provides a method to return the number of rows.
    The ResultSetMetaData interface provides a method for counting the number of columns but nothing for the rows.
    Thanks

    No good way before JDBC2.0. u can use JDBC2.0 CachedRowSet.size() to retrieve the number of rows got by a ResultSet.

  • No. Of Rows in a resultset

    Is there a method in result which one could use to get the no. of rows in a resultset. I currently get my no. by looping thru the resultset and summing the number of times the loop runs.
    Cheers,
    Havasen

    I don't know if it's driver specific - what can you say what any driver does inside?
    But I think, the most common way should be, that the driver does the same like I and others did it formerly in C programs with embedded SQL:
    Let the DBMS create a cursor for the query.
    Open this cursor - so the DBMS creates a temporary result set inside.
    Position that cursor inside this result set (inside he DBMS).
    Fetch the actual row.
    After finnishing your logic, close the cursor. So the DBMS clears up its temporary result set.
    Now what JDBC presents you as a scrollable result set, should be nothing more than an opened cursor, ready to position on ra single row and fetch it, ... and at the end - when you call ResultSet.close() - close the cursor.
    So each row can stay inside the DBMS, until you cause JDBC to fetch it. I think, this is, when you position on it, and JDBC does some caching and retrieves a couple of rows together - there are methods to control this to a certain degree, like Statement.setFetchSize().
    But the DBMS must hold the cursor and maintain its temporary result set.
    So it's not equal if you cause JDBC to retrieve a complete resultset - and for positioning on the last row, it has to retrieve them all, I fear.
    As I said - I can't promise that your driver will work this way.
    If I would write a JDBC driver, I probably would try it this way. But I haven't yet, and why should I.
    That was the explanation.
    And to the original question:
    For counting the records only, I would never suggest the last() trick, but a simple query on
    "SELECT COUNT(*) FROM table".

Maybe you are looking for