Trying to find the number of rows in a ResultSet

I am trying to find the number of rows in a ResultSet, using the following code:
rs = st.executeQuery(query);
rs.last(); // to move the cursor to the last row
System.out.println(rs.getRow());
However, I am getting this error:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Unsupported method: ResultSet.last
Whats going wrong??

praveen_potru wrote:
You might have not used scrollable result set..
Try like this:
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);rs = stmt.executeQuery(sqlString);
// Point to the last row in resultset.
rs.last();
// Get the row position which is also the number of rows in the ResultSet.
int rowcount = rs.getRow();
System.out.println("Total rows for the query: "+rowcount);
cheersAnd I hope the OP would read it because the thread is a year old.

Similar Messages

  • Can we find the number of rows in table from the dump file

    Hi All,
    Can we find the number of rows in table from the dump file with out importing the table in to the database?
    Please let me know ,if any option is there.
    Thanks,
    Kumar.

    <s>Try to import with option SHOW=Y, that should skip the number of rows which are into a table from a dump file.</s><br>
    <br>
    Nicolas.<br>
    Oops, sorry, that doesn't show the number of lines...<br>
    Message was edited by: <br>
    N. Gasparotto

  • How to get the number of rows in a ResultSet

    Hello,
    I'm an intern and I'm trying to get the number of rows from result set in oracle using rs.last() and rs.beforeFirst() methods
    but I got an error. Could Below is my sample code:
    import java.sql.*;
    public class SarueckConnect {
    public static void main(String[] args) {
    Connection con = null;
    Statement stmt = null;
    ResultSet re = null;
    String[] ParamArray;
    ParamArray = new String[24];
    //Properties logon;
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver"); //Loading the Oracle Driver.
    con = DriverManager.getConnection
    ("jdbc:oracle:thin:@258.8.159.215:1521:test_DB","data","data"); //making the connection DB.
    stmt = con.createStatement ();// Sending a query string to the database
    //stmt.executeUpdate("UPDATE test_table set steuk = 6 WHERE steuk = 5");
    ResultSet rs = stmt.executeQuery("SELECT mandt,kokrs,werks,arbpl,aufnr,vornr,ile01,"+
    "lsa01,ism01,ile02,lsa02,ism02,ile03,lsa03,ism03,ile04,lsa04,ism04,steuk,matnr,budat,"+
    "kostl,pernr,rueckid FROM test_table where steuk =6");
    //Print the result out.
    rs.last(); //This is the line which gives an error.
    int rows = rs.getRow();
    rs.beforeFirst();// I presume this is wrong to.
    ParamArray = new String[24*rows];
    int counter=0;
    while (rs.next()) {
    for (int i = 1; i <= 24; i++){
    ParamArray[i-1+(counter*24)] = rs.getString(i);
    System.out.print(rs.getString(i) + '\t');
    System.out.println();
    counter++;
    } catch(Exception e) {
    e.printStackTrace();
    } finally {
    try
    if(stmt != null) stmt.close();
    if(con != null) con.close();
    } catch (Exception exception) {
    exception.printStackTrace();
    TryBapi sap = new TryBapi(ParamArray);
    }When I run the code I do have the following ERROR Message:
    java.sql.SQLException: Invalid operation for forward only resultset : last
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
    at oracle.jdbc.driver.BaseResultSet.last(BaseResultSet.java:91)
    at SarueckConnect.main(SarueckConnect.java:28)Please could any body Help me out here to figure out how to correct this?
    Any Help would be highly apprecited.

    make your result set scrollable...Not such a good idea. With Oracle, the JDBC driver will read and cache all the ResultSet in memory (with other DBMSs/drivers the behavior will probably be different, but you will still have some unnecessary overhead).
    You can do the caching yourself if you think it's worth it. If the piece of code you posted is why you need this for, then use a List or Vector and you won't need to know the size upfront.
    Alin,
    The jTDS Project.

  • How to know the number of rows of a ResultSet

    Hello.
    I'm trying to detect the number of rows of a query result before making all the results.next().
    Until know, my way was:
    ResultSet results = sql.executeQuery();
    while( results.next() )
    int i = 0;
    ... do stuff ...
    i++;
    out.println("The number of rows is: " + i);
    But I would like to know the value of 'i' just after 'executeQuery'.
    Does anybody have any experienc?
    Regards.

    How about like this???
    >
    ResultSet results = sql.executeQuery();results.last();
    i = results.getRow();
    results.first();
    while( results.next() )
    int i = 0;
    ... do stuff ...
    i++;
    out.println("The number of rows is: " + i);
    But I would like to know the value of 'i' just after
    'executeQuery'.
    Does anybody have any experienc?
    Regards.

  • 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.

  • Getting the number of rows returned from ResultSet

    Hi,
    Does anyone know a method to get the number of rows returned with a query using the Resultset class?
    Thanks.

    Hi 281080,
    If your database and JDBC driver support it, in order to use the solution that da-alexj has suggested, you need to create a 'scrollable' "ResultSet" -- the javadoc for method "createStatement()" (in class "java.sql.Connection") has more details.
    However, I have found with Oracle 8.1.7.4 database and Oracle (thin) JDBC driver, that part of their implementation of the "last()" method (in class "java.sql.ResultSet") is to actually iterate through the entire "ResultSet" in order to reach the last row. If your "ResultSet" is very large (I tested it with a 100,000 row "ResultSet"), this will take a long time.
    Just wanted to make you aware of that.
    Of-course, this may be irrelevant to you since I didn't see any mention in your post of what database and JDBC driver you are using.
    Hope this has helped you, anyway.
    Good Luck,
    Avi.

  • How do I get the number of rows in a ResultSet?

    I checked the API, but didn't find a method that does this.

    You could try calling last() then getRow() to get the
    no of rows ...You probably need a scrollable resultset to do this, so that you can
    rewind back to the start and read the data in.
    I imagine you want to get a count, to size an array, before reading in the data ? Why not load it in an arraylist, then convert to an array afterwards ?
    regards,
    Owen

  • How do I find out the number of rows in a resultset ?

    Without scrolling it.

    And explain to me, oh sarcastic one, how it wouldn't be a better idea to put that data into a datastructure and pass that back.1. Performance with very large ResultSet objects for which only sparse data is desired (that's what pages are for).
    2. Resource optimization.
    3. Efficient sequential processing.
    4. The ability to intelligently determine based on resource usage whether to page the results or use your implementation for small ResultSet objects.
    Answering your points:
    1. I just showed him a very easy way to get the size.
    2. I don't see any advantage of a collection (if that's what you were envisioning) over a paged ResultSet. Actually the ResultSet is a collection of sorts.
    3. Your data is reliant on the connection initially anyway. The collection would only provide an advantage in the possibly short window while the data was being viewed.
    4. You can always resort a ResultSet by requerying.

  • Find Total Number of Rows in Database

    I am trying to find total number of rows in Database? Thanks in advance

    I'd come back to a question of "why". What possible reason could you have to want to know the number of rows in every table in the database?
    This seems like it is a request from someone non-technical where they're not sure exactly what they need to ask to get the information they want. I'm forseeing something along the lines of
    User: How many rows are in the database
    DBA: Ummm.... <<goes off and spends a lot of resources to count>>> ... 1 billion.
    User: Wow, that's a lot... How big are those rows...
    DBA: Hmmm... <<goes off and spends more time trying to find an average row size>>... 100 bytes
    User: <<Plugs numbers into Excel>>... OK, our database is 93 GB, so we need to order 100 GB of disk for our new environment.
    DBA: <<Months later, after 100 GB of disk has been budgeted, purchased, and delivered>> User, you didn't order enough disk for our disaster recovery environment.
    User: You said our database was only 93 GB!!
    DBA: 93 GB of data. Plus 100 GB of indexes, TEMP, UNDO, space for archived logs, ...
    When "odd" requests come down, it's generally the case that the person asking the question doesn't know what question they really want to ask because they don't have the domain knowledge to know how to ask the question. Walking over and asking them what they're hoping to accomplish/ what they're using the data for generally leads to a case where
    - The user gets exactly the information they need, not the information they think they want
    - The DBA does a lot less work, since the right question is almost always easier to answer
    - The user thinks of the DBA as a really helpful sort of guy and remembers how helpful you were in the past when you have to push back on other things later
    - No one ends up playing the blame game when the user gets just what they asked for and not what they want.
    Justin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

  • How to find the number of Z or Y programs and amount of code lines?

    hi all, I'm trying to find the number of programs on user namespace (Z* or Y*) and the number of coded lines in them. something like report Z_MY_REPORT, lines 582; Include Z_MY_INCLUDE, lines 135.
    Exist any standard report to do it or do I have to develop it by myself?
    thanks for any suggestion.

    If you have to do in urself
    DATA: i_reptx TYPE STANDARD TABLE OF textpool WITH HEADER LINE.
    READ TEXTPOOL 'ZJ_ALV' INTO i_reptx.
    and describe the itab.. and get  the lines.
    In conjunction with TADIR table to get the program repository.

  • Limiting the number of rows returned by SQL or ResultSet

    Hello,
    I'm trying to limit the number of rows that my application receives to a user-specified value. The user is asked how many accounts they want returned and I want to limit my ResultSet to this amount. I'd prefer to set up the SQL to do this if possible rather than waste time returning more records than necessary to the ResultSet.
    If it's not possible to limit the SQL, how can I limit the number when processing the ResultSet?
    Or, is there any way to setup a cursor in JDBC without being able to use Stored Procedures?
    I'm going up against DB2 using DB2Connect.
    Thanks!

    It's not surprising that you are confused because the documentation doesn't bother to explain what the "fetch size" actually is, it just says that setFetchSize sets it and getFetchSize gets it. As I understand it from some other documents I read about JDBC, the fetch size is a number that may be used internally by the JDBC driver. Here's an example of how I understand it (others, I know you will feel free to correct me if you disagree):
    When the driver produces a result set with a very large number of records, it has to generate those records and deliver them to the system that requested them. If the database is not on the same system, then those records must all travel over the network. It could be a performance problem if you had to wait for (say) 80,000 records to travel across the network. Enter the fetch size. If you set the fetch size to 100, then the driver will bring the records across in batches of 100, as the program calls for them. Now, this buffering is transparent to your program; the driver doesn't tell you that it's getting another batch and you can't tell it to get another batch. So it is not a solution to the problem that everybody has here, namely how to display your records 10 per page and allow the user to go back and forth among those pages, like search engines do.

  • XMLIndex: finding indexed XPaths and the number of rows in the path table

    Hi,
    I am storing non-schema-based binary XMLs in an XMLType column in 11g (11.1.0.6.0) and would like to index the XMLs either partially or fully using XMLIndex. I'm expecting to have a large number (tens of millions) of XML documents and have some concerns about the size of the XMLIndex path table.
    In short, I am worried that the path table might grow unmanageable large. In order to avoid this and to plan for table partitioning, I would like to create a report of all indexed XPaths in an XMLIndex and to find out how many times each path is actualized in the path table. I would do this for a representative XML sample.
    I have been creating XMLIndexes with different exclude/include paths, gathering stats with DBMS_STATS (estimate_percent = 100) and selecting the number of rows in the path table through USER_TABLES.
    If anyone knows a more straightforward way of doing this all advice is very much appreciated.
    Best Regards,
    Rasko Leinonen

    Thanks Marco,
    I managed to get out all indexed paths using the following SQL. It took a while to understand how the join the XDB.X$PT39CW6BJR8W4VVE0G0LLGA0OCR5 and XDB.X$QN39CW6BJR8W4VVE0G0LLGA0OCR5 tables together but got there in the end. This helps to clarify which XPaths are being currently indexed by the XMLIndex.
    begin
    for v_row in (select PATH from XDB.X$PT39CW6BJR8W4VVE0G0LLGA0OCR5)
    loop
    declare
    v_i BINARY_INTEGER := 1;
    v_id raw(8);
    v_len BINARY_INTEGER := 2;
    v_skip BINARY_INTEGER := 1;
    begin
    while v_i < utl_raw.length(v_row.path) and
    v_i + v_len <= utl_raw.length(v_row.path)
    loop
    v_i := v_i + v_skip;
    v_id := utl_raw.substr(v_row.path, v_i, v_len);
    --dbms_output.put_line(v_id);
    for v_row2 in (select LOCALNAME, flags from XDB.X$QN39CW6BJR8W4VVE0G0LLGA0OCR5
    where ID = v_id )
    loop
    if rawtohex(v_row2.flags) = '01'
    then
    dbms_output.put('@');
    end if;
    dbms_output.put(v_row2.localname);
    if v_i + v_len < utl_raw.length(v_row.path)
    then
    dbms_output.put('/');
    end if;
    end loop;
    v_i := v_i + v_len;
    end loop;
    dbms_output.put_line('');
    end;
    end loop;
    end;
    Example output:
    RUN
    RUN/@accession
    RUN/@alias
    RUN/@instrument_model
    RUN/@run_date
    RUN/@run_center
    RUN/@total_data_blocks
    RUN/EXPERIMENT_REF
    RUN/EXPERIMENT_REF/@accession
    RUN/EXPERIMENT_REF/@refname
    RUN/DATA_BLOCK
    RUN/DATA_BLOCK/@name
    RUN/DATA_BLOCK/@total_spots
    RUN/DATA_BLOCK/@total_reads
    RUN/DATA_BLOCK/@number_channels
    RUN/DATA_BLOCK/@format_code
    RUN/DATA_BLOCK/@sector
    RUN/DATA_BLOCK/FILES
    RUN/DATA_BLOCK/FILES/FILE
    RUN/DATA_BLOCK/FILES/FILE/@filename
    RUN/DATA_BLOCK/FILES/FILE/@filetype
    RUN/RUN_ATTRIBUTES
    RUN/RUN_ATTRIBUTES/RUN_ATTRIBUTE
    RUN/RUN_ATTRIBUTES/RUN_ATTRIBUTE/TAG
    RUN/RUN_ATTRIBUTES/RUN_ATTRIBUTE/VALUE

  • I have an education version of cs 6 creative suite, I have the box and used in once on my old laptop I am now trying to find serial number to reuse on new laptop but only have product code and product number

    I have an education version of cs 6 creative suite, I have the box and used in once on my old laptop I am now trying to find serial number to reuse on new laptop but only have product code and product number

    Hi Sophie,
    Normally you can find your serial registered to your AdobeId. To find it please login to the Adobe website and check if you did register it to your account.
    Serial number, Redemption, and Product codes | Student & Teacher Editions
    In general you will go true a educational verification afterwards you get the serial. So its in your email or account. If you still have issues get in contact with the chat team to get help.
    Good luck!

  • How do find out the number of rows

    how to find out the number of rows of as all tables in a schema with one sql statement

    Hi,
    What u can try doing is...
    declare
    numrows number;
    begin
    for i in (select tname from tab)
    loop
    execute immideate 'select count(1) into numrows from ' &#0124; &#0124; i.tname ;
    dbms_output.put_line(numrows);
    end loop;
    end;
    Hope this helps. This works only in 8i. So if u need to do the same in 8.0 u have to use dbms_sql pkg.
    The logic is same but the way u open ands execute the code is a bit diff.
    Best Of Luck.
    Regards,
    Ganesh R

  • Need to know how to limit the number of rows returned on Oracle

    MS SQL Server has a command called 'set row count'.
    We are trying to find similar one on Oracle.
    What we are trying to do is that instead of using rownum in the query statement, we would like to find way to limit the number of rows returned. I understand that we can use JDBC resultSet object, but that's not what we want.
    I know Oracle has one called arraysize, but this would not limit the number of rows returned either.
    Pease help.
    Thanks

    I understand that we can use JDBC resultSet object, but that's not what we want.I'm not sure which feature of ResultSet you use and which not.
    But if this question has anything to do with JDBC (that's the forum where you put it), I'd recommend to use Statement.setMaxRows(). This will limit the count of rows which your statement will fetch into it's ResultSet.

Maybe you are looking for

  • Project Server 2013 on Windows Server 2012 R2

    Can we install Project Server 2013 on Windows Server 2012 R2? Is it supported? I searched a bit but couldn't find any concrete information if it is supported or not.

  • How do you stop a mac from broadcasting its presence

    I have a imac on my network how can I make it invisible on the network so it does not show in the side bar of other macs on the network?

  • Writing in between a text File

    Hi all, During a task, I am facing an interesting problem, this came first time since my 2 years development in Java. In always use BufferedReader in = new BufferedReader(new FileReader("/home/oracle/init1.txt")); to read from file and, PrintWriter o

  • Macbook Pro Retina 17 inch crashing on resume - talagent

    My MBP Retina 17" (Mid 2012, 2.3 GHz i7, 16GB RAM) is crashing frequently on wakeup. What seems to be happening is that the Mac will wake up okay, but when running apps (e.g. Chrome) it will get "confused" and show me content I'd seen in the previous

  • Problem with NFS file adapter using a network share as source of files

    Hello, I have set up a sender cc with NFS format file adapter, with at the source directory a network share ( server\sharename ). The share is on another system and not mapped to a local drivename, but it is open for everybody. The problem is, the ad