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);

Similar Messages

  • Count the rows in a list-component

    Hi,
    How can I count the rows with a value in a list component?
    I thought it would be RowCount but as I figured out that shows the number of shown rows, not the number of rows with a value.

    Hi,
    Try list.dataProvider.length

  • Number of columns of the rows in a ResultSet?

    Hello
    Is there any way to get the amount of columns of the rows in a ResultSet? The values of the columns might be empty, so wasNull() wont work?
    Thanks

    Yes, you can see the number of columns and lots of other info, see ResultSetMetaData. Example:
    ResultSet rs = statement.executeQuery("...");
    ResultSetMetaData rsmd = rs.getMetaData();
    System.out.println("Number of columns: " + rsmd.getColumnCount());Look it up in the API documentation.

  • Count the rows which are selected ?

    Hi Experts,
    I need help.My requirement is:
    I am having a table which can be multiple selected. But there are two buttons attached to it.Edit and Delete.For Delete I want the Multiple Selection not for EDit.So, If i will click the Edit after multiple selection of the rows.How i can give pop-up that multiple edit is not possible?Means I want to count the rows which are selected how to do that?
    Please guide me.
    Urgent need
    Regards
    Nutan

    Hi Nutan,
    Marcel is right about backend. However I might have an idea once I am developing a quiet similar solution. I would like to ask you to explain with more detail. It seems you have two requirements in one question.
    Regards,
    Gilson

  • Counting the rows

    Hi,
    I've a datamodel with BI Query. I want count the rows like a rownum function. How can I do this?
    Thanks R.
    Edited by: user12003776 on Jan 26, 2012 5:02 PM

    Do you want to get a count of rows in the query or do you want to find the count in the template? Rownum function returns the rows' position.
    If you want to find the count of rows within the template for some conditionally formatting, then you could use something similar to this:
    <?count(/XXTA_SHIP_LABEL/LIST_G_CUSTOMER/G_CUSTOMER)?> -- change the path to the field in your dataset
    Can you explain a bit more as to what you are trying to do?
    Thanks,
    Bipuser

  • Is it possible to count the rows returned from a query?

    Hello,
    When using JDBC is there anyway of finding out the number of
    rows returned from a query before actually getting each row?
    In Forms 4.5 you can use the count_query function, does anyone
    know of an equivalent function or work around in JDBC and/or
    SQLJ?
    Thanks.
    null

    Pasi Hmlinen (guest) wrote:
    : Try
    : SELECT COUNT(*) FROM the_table WHERE <conditions>;
    : Hope this helps,
    : Pasi
    Thanks for the advice, I'm currently using SELECT COUNT(*) but
    I'm looking for a more efficient way of doing it. If I SELECT
    COUNT each time then I have to prepare and execute a SQL
    statement each time. What I want to do it execute a single SQL
    statement to return my results and somehow find out the number
    of rows in the resultset without having to go back to the
    database.
    Gethin.
    null

  • How do i count the rows??

    hello,
    i have a database from where I import a table.
    now, because of my data-structure i need to know the nubmers of row in my table.
    For that i don't want to use the sql-statement: select count(*) ....
    but instead I want to use a java JDBC function. I looked at the docu, but i wasn't able to find it.
    Can somebody help me please?
    thanks
    Markus

    i have a database from where I import a table.
    now, because of my data-structure i need to know the
    nubmers of row in my table.Using an array perhaps?
    Why not use an ArrayList and simply add
    each record as you fetch it -- then you don't
    need foreknowledge of how many you fetched.
    For that i don't want to use the sql-statement: select
    count(*) ....
    but instead I want to use a java JDBC function. I
    looked at the docu, but i wasn't able to find it.
    Can somebody help me please?
    ResultSet rset = ...
    int iRowCount=0;
    if (rset.next())
      rset.last();
      iRowCount = rset.getRow();
      rset.first();

  • Count of rows deleted from ResultSet

    Hi: I have the following
    String query = "delete from REMEDY.trs_notes where ROWID NOT IN (SELECT MIN(ROWID) from REMEDY.trs_notes group by NOTEID)";
    stmt = remedyMDSconn.createStatement();
    rs = stmt.executeQuery(query);
    Is there anyway I can get the number of rows that were deleted from rs variable?
    Thanks
    Ravi

    I wonder that this worked without exception.
    [http://java.sun.com/javase/6/docs/api/java/sql/Statement.html]
    As the API docs states, you should use Statement#executeUpdate() for this.

  • Count the rows which differes a column value

    msg deleted
    Edited by: user11253970 on Jul 14, 2009 11:33 AM

    Perhaps this
    with a as (
         SELECT cert_no, count(distinct RACE_CODE) RACE_CODES   FROM subscriber
         group by cert_no having count(distinct RACE_CODE) > 0 )
    b as (  
         SELECT cert_no, count(distinct RACE_CODE) RACE_CODES FROM med_subscriber
         group by cert_no having count(distinct RACE_CODE) > 0 )
    c as (
         select cert_no, count(distinct RACE_CODE) RACE_CODES from den_subscriber
         group by cert_no having count(distinct RACE_CODE) > 0 )
    select  cert_no, a.race_codes + b.race_codes||' (in med_subscriber and subscriber tables)'
            ||' , '||  c.race_codes ||' (in den_subscriber table).'  col1
    from a , b ,  c
    where a.cert_no = b.cert_no
    and a.cert_no = c.cert_no
      note: not tested for your data.
    SS

  • Count the number of rows based on the values!!!

    Hi all,
    What I am using:
    I am working with a multidimensional database in Visual Studio 2010 using its Data source view and calculation member and dimension usage.
    What I want to do:
    I have a fact table that has five columns(leg(s),hand(s), Head and body,overall) that shows the category of how severe the injury is. Let say for the records all columns never have an empty value(no injury is stated with 'No injury' ) . These five columns
    are connected with a dimension that has all the available values (Category A-E of injury).The overall has the most severe from the other four columns. I want to create a bar chart with five different measure
    values, one for each column, and count the values in those columns. 
    For example : I have a slicer in the excel and a bar chart and the slicer has all the values of the Category of the injury ( Cat a,Cat B, Cat C, ... Cat E, No injury ) and when i select one of them, lets say
    Cat C,  the bar chart should update and show how many Cat C each measurement column has. 
    Example FACT table:
    ID      LEG      HAND    HEAD   BODY OVERALL
    1        No         A           No        No        A
    2        No        D            C          C         C
    3    E         C            D           A         A
    4         E          E           B            C         B
    So if i selected C the bar chart will count   (Leg = 0, Hand = 1, Head = 1, body = 2 and Overall = 1).
    Any ideas ?
    Thanks for the help and the time :) 

    Hi DBtheoN,
    According to your description, you want to create a chart on excel worksheet to count the rows based on the value, right? If in this case, I am afraid this issue is related to Office forum, I am not the expert of Office, you can post the issue on the corresponding
    forum.
    However, this requirement can be done easily on SQL Server Reporting Services. You can using the expression below to count the rows.
    =COUNT(IIF(Fields!LEG.Value=Parameters!TYPE.Value,1,NOTHING))
    Regards,
    Charlie Liao
    TechNet Community Support

  • Count the displayed date rows on a column.

    Hi,
    I am trying to have a table with the following information and try to give a total count (of the dates visible) at the bottom of the table for the date columns. (not counting the rows)
    (E.g.- Under the Sent column there are 5 dates displayed and there are 6 numbers associated. On the received column there are 3 dates displayed for 6 numbers)
    Could you please advice me how to accomplish this. Thank you in advance.
    Rob.
    Name SENum SentReceived
    John      1234     12/22/06     
         5678     12/13/06     
         6565     12/19/06     1/6/07
         5656 - -
    Jane     9866 12/18/06     1/5/07
    Jim     5657 12/18/06 12/14/06           
    Total:           5      3

    I ma not sue what you want but
    here is some idea at sql plus
    SQL> compute count of ename on deptno
    SQL> compute count of sal on deptno
    SQL> select deptno,ename,sal from emp order by deptno
      2  ;
        DEPTNO ENAME                                                     SAL
            10 CLARK                                                    2450
               KING                                                     5000
               MILLER                                                   1300
    count                                                       3          3
            20 SMITH                                                     880
               ADAMS                                                    1100
               FORD                                                     3000
               SCOTT                                                    3000
               JONES                                                    2975
    count                                                       5          5
            30 ALLEN                                                    1600
               BLAKE                                                    2850
               MARTIN                                                   1250
               JAMES                                                    1045
               TURNER                                                   1500
               WARD                                                     1250
    count                                                       6          6
               DEV                                                      5000
    count                                                       1          1

  • Count all rows of a Database table

    I want to count all the rows of a table.
    I have made correctly the connection with the database and all the other things (make a statement, make the query and the resultSet), I use the MySQL server v4.0.17
    Here is the code I use:
    int count=0;
    String temp=null;
    String query = "SELECT COUNT(*) FROM customers";//customers is my Database Table
    try {
    rs = stmt.executeQuery(query);
    }//end try
    catch(Exception exc) {
    createFeatures.PlayErrorSound(registerRenter);
    Optionpane.showMessageDialog(registerRenter,
    "MESSAGE", "ERROR", JOptionPane.ERROR_MESSAGE);
    }//end catch
    try {
    while(rs.next()) {
    count++; //I use this variable in order to count the rows because the resultSet doesn't tell me the answer
    countLine=rs.getInt(1);
    }//end while
    }//end try
    catch(Exception exc) {
    createFeatures.PlayErrorSound(registerRenter);
    Optionpane.showMessageDialog(registerRenter,
    "MESSAGE", "ERROR", JOptionPane.ERROR_MESSAGE);
    }//end catch
    count=count++; //i increase it aggain because if the rows are initial 0 i want to make it 1
    temp = String.valueOf(count);//i use this command in order to display the result into a jtextfield
    Any help is appreciated!!!!!

    This program will work just fine against mysql:
    mport java.util.*;
    import java.io.*;
    import java.sql.*;
    public class Test {
    public static void main(String []args) {
    String url= "jdbc:mysql://localhost/adatabase";
    String query = "select count(*) from foo2";
    String createQuery="create table foo2(f1 int)";
    String dropQuery="drop table foo2";
    String insertQuery="insert into foo2 values(1)";
    Properties props = new Properties();
    props.put("user", "auser");
    props.put("password", "xxxxx");
    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    System.out.println("Connecting to the msql server");
    Connection con = DriverManager.getConnection(url, props);
    Statement stmt = con.createStatement();
    try {
    stmt.executeUpdate(dropQuery);
    } catch(SQLException e) {}
    stmt.executeUpdate(createQuery);
    stmt.executeUpdate(insertQuery);
    stmt.executeUpdate(insertQuery);
    PreparedStatement pstmt = con.prepareStatement(query);
    System.out.println("***Executing a select");
    ResultSet rs = pstmt.executeQuery();
    while (rs.next()) {
    System.out.println("RowCount="+ rs.getString(1));
    rs.close();
    pstmt.close();
    stmt.close();
    con.close();
    } catch(Exception e) {
    System.out.println(e.getMessage());
    }

  • Scrolling, Counting (the whole shebang?)

    Bear with me here, this one's causing me headaches and sleepless nights (and I'm not exaggerating. While this issue sits in Bugzilla the boss cannot be silenced.)
    <cliche>The story so far...</cliche>
    An implementation of a scrolling result set I built recently for one of the queries in our system was done like this (The database system in question is Postgresql, btw):
    (1) Count the rows using a SELECT count(*) ...
    (2) Get a cursor with DECLARE CURSOR FOR SELECT ...
    (3) Use FETCH to grab rows, MOVE to move the cursor, etc.
    (4) Finish up at some point in the future with a CLOSE.
    Now...
    The entire reason we went through this horror was because ResultSet in Postgres isn't implemented using native Postgres cursors. This made the development team uneasy (we thought the performance wouldn't be up to scratch) so we eventually decided we would go for a somewhat more brute-force way of using Postgres' built-in commands. The result, a very fast scrolling result set, which works fine, for the one or two pages in our system which use it.
    But (there is ALWAYS a but, right?)...
    Since the DECLARE CURSOR, FETCH, MOVE, and CLOSE statements are not in the SQL standard, the query doesn't work under, say, DB2. Likelihood is it wouldn't work under any other database at all. And for portability's sake, we're considering this to be a bad thing.
    In addition to this, the need to get the count separately from the query itself means that in every place where we do a SELECT, we have to have an equivalent SELECT to do the count (which in some cases can bear little or no resemblance to the one used to do the query.)
    So...
    Question 1: Is doing an 'ordinary' SELECT query, holding the ResultSet in memory and simply reading off row by row only parts of it considered 'safe', where 'safe' is defined by not causing OutOfMemoryError for queries in the order of 10,000-100,000 results?
    Now, we require a count of the number of the results. In our existing system this is the slowest part of the process because the SELECT count(*) takes orders of magnitude more time to execute than the DECLARE CURSOR. That is, the DECLARE CURSOR takes about 10ms, and the SELECT count(*) can take a minute, depending on the size of the result set, presumably because the DECLARE CURSOR doesn't actually do the query, and that the query executes as you fetch more results from it, or in the background. (Beats me, anything involving databases is magic as far as I'm concerned.)
    Since we 'require' the count though (for the user interface only, despite the fact that it cripples the query speed somewhat), we might be able to get away with doing it this way, so...
    Question 2: Would doing the following set of commands likely take the same amount of time to run (or less, or more), compared to doing the SELECT count(*)?
    PreparedStatement ps = /* insert code here ;-) */
    ResultSet rs = ps.executeQuery();
    rs.afterLast();
    int numRows = rs.getRow();
    rs.beforeFirst();I guess another part of Question 2 is, is that sequence of commands considered an 'acceptable' way of counting the number of rows returned? It seems to me they would take a long time to run, but sob I don't know.
    The main thing is, we have an extraordinarily large number of different queries in this system, and all of them pretty much use joins of some sort, and all of them are incredibly complex. The system as a whole is quite monolithic, which is why I'm trying to make this enhancement as simple as possible. The count-and-then-cursor method would not only be incompatible with DB2, Oracle and whatever, but would probably take an order of magnitude more time to roll into the system than something which simply caches the result sets.
    Question 3: Does anyone know of an independent group, which might have implemented cursor-based ResultSet scrolling into Postgres' JDBC driver? This would be a big time saver, as the lack of this in their driver makes the database nearly useless for any sizable system, and 'unfortunately' our company has a heavily Open-Source philosophy (believe me, I love OS too, but in this case it's crippling us.)
    Question 4: Does anyone know of a company in the Sydney region who is looking to recruit a guy who knows a hell of a lot about Jabber and various IM protocols, but not so much about databases? [Disclaimer: if you are my current employer, this is a JOKE.]
    Since this is such a weight on my shoulders at the moment, I'll put up a fairly sizable number of dukes. Let's hope we attract the hot-shots. These problems must have been done 1000 times before, but I've never seen a tutorial on them, and not even the 5kg JDBC book we have here helps at all.

    To have scrollable ResultSet you can use CachedRowSet (you can use it with any DBMS):
    http://developer.java.sun.com/developer/earlyAccess/crs/
    To prevent OutOfMemoryError you can:
    PreparedStatement ps = /* insert code here ;-) */
    PreparedStatement.setMaxRows(int�maxRows);
    ResultSet rs = ps.executeQuery();
    CachedRowSet crs = new CachedRowSet();
    crs.populate(rs);
    crs.afterLast();
    int numRows = crs.getRow();
    crs.beforeFirst();
    and if numRows = maxRows promt users that there are more records and let them redefine query
    or if you don't have to display all rows (who needs 100000 rows once anyway? ;)
    retrieve and cache just keys and display result page by page (there are some other paterns to do paging).
    100000 records is a lot, but it depends on number of users and RAM - it's still can be done.
    there is another OpenSource JDBC driver for PostgreSQL: jxDBCon http://sourceforge.net/projects/jxdbcon/
    you can compare it with PostgreSQL driver

  • Count MySQL rows based off Value in Dynamic Table

    Greetings all. I have 2 MySQL tables; 1 that contains the names of my classes.(Class A, Class, B, etc.) and 1 table that contains the names of students in each Class (for example Class A: John Doe; Class A: Susie Smith.; Class B: Jane Doe). In the 2nd table the Class name is in its own column and the student's name is in a 2nd column.
    I currently have a dynamic repeating table that lists the names of all of the classes from the 1st table. What I'm trying to do is add a second column to this repeating dynamic table that lists the number of students in each class. For example; Row 1 of the dynamic table would say "Class A | 5; Class B | 3; Class C | 7, etc.). The dynamic table works perfectly to list the class names. For the life of me I can't figure out how to perform a count for each class to insert in the repeating table. I will be adding more Classes which as why I'm trying to set up the counting query dynamically. So far I have only been able to figure out how to count the total rows in the 2nd table, or count the rows with a specified class name. Any advice or guidance on how to count the number of rows in the 2nd MySQL table based off the class name in the repeating table is much appreciated. Thank you for any and all help. Have a great day.

    Select count(*), Class from MyTable
    Group by Class
    Time to learn about SQL:
    http://www.w3schools.com/sql/sql_intro.asp

  • Counting HTML Rows

    Is there any way to count the rows in a HTML table?
    The user of the site is able to add and remove rows on the
    page and I need to be able to count the rows so I can update a
    database.
    Is it possible with some JavaScript or VBScript?
    Thanks

    Javascript:
    document.getElementById("yourTableID").rows.length
    "TGuthrie" <[email protected]> wrote in
    message
    news:ejcs0f$o35$[email protected]..
    > Is there any way to count the rows in a HTML table?
    >
    > The user of the site is able to add and remove rows on
    the page and I need
    > to
    > be able to count the rows so I can update a database.
    >
    > Is it possible with some JavaScript or VBScript?
    >
    > Thanks
    >

Maybe you are looking for

  • How to use the cloud for music

    How do I upload part of my music to the cloud to share with my laptop when away from the house?

  • N80 - WLAN at home won't detect, but I've used out...

    Hi guys, I need help... I have a got new N80, and the WLAN feature was one of my most high expectations... I have already successfully used the WLAN feature with my new N80 on Public access points, and it works great. But at home I can't detect my ho

  • Flex - HttpService - how to display an arraylist in flex combobox

    I have written an httpservice which returns an arraylist, in the tomcat logs I can see that an arraylist is being returned properly. However I am not able to display the arraylist Can any one please let me know what wrong i am doing ? Also the httpse

  • Record information of the Objects in OM

    Hi All, can any one tell me if create a relationship between objects, in which table the rec information stored. Appreciate your early response. Regards Rajesh

  • CRYSTAL REPORTS XI  - Version

    Hi,   I have got a Crystal reoprt developer edition how to identitfy the Relaese no (ie) Crystal reports release 1 or 2 Thanks, Prem