Trying to get number of rows in recordset but getRow( returning -3)

Hi all,
I really hope someone here will be able to help me with this, it is driving me mad.
I have a query that is returning records within SQL Server, and i am trying to get the number of records returned for checks within my code. this is what i have at the moment:
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs_item;
rs_item = stmt.executeQuery("select TOP 1 ColA, ColB from DATA_TBL_9");
rs_item.last();
int num_recs = rs_item.getRow();
out.println(num_recs); <-- this returns -3 everytime!!!!
I've tried doing:
while (rs_item.next()) {
out.println(rs_item.getRow() + "<br />");
and this increments as expected.
I've read somepeople saying to do a count(*) before each query and to use that, but i am going to have 9 queries running on this page, so would like to use the above method if available to minimize hits on the database, i don't really want to have 18 queries running.
Any ideas?
TIA
Tony

Sarky78 wrote:
Thanks for the quick response, but i'm a novice with jsp. It is not that much related to JSP.
Can you explain what you mean here? Convert ResultSet to List<RowObject>, where RowObject is a javabean representing, well, a single row.
Would this mapping have a performance implication?Not shocking. Maybe a few ms. The benefits are bigger. Clear separation of data and business/view layers, a shorter timespan that the connection has to be open, better and more easy postprocessing in the business and view layers, etcetera.

Similar Messages

  • How to get number of rows and columns in a two dimensional array ?

    Hello,
    What would be the simplest way to get number of rows and columns in a two dimensional array represented as integers ?
    I'm looking for another solution as For...Each loop in case of large arrays.
    Regards,
    Petri

    Hi Petri,
    See a attached txt file for obtaining two arrays with upper and lower index values
    Regards
    Ray
    Regards
    Ray Farmer
    Attachments:
    Get2DArrayIndex.txt ‏2 KB

  • How to get number of rows return in SELECT query

    i'm very new in java, i have a question:
    - How to get number of rows return in SELECT query?
    (i use SQL Server 2000 Driver for JDBC and everything are done, i only want to know problems above)
    Thanks.

    make the result set scroll insensitve, do rs.last(), get the row num, and call rs.beforeFirst(), then you can process the result set like you currently do.
             String sql = "select * from testing";
             PreparedStatement ps =
              con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
             ResultSet rs = ps.executeQuery();
             rs.last();
             System.out.println("Row count = " + rs.getRow());
             rs.beforeFirst();~Tim
    NOTE: Ugly, but does the trick.

  • I am trying to get into my Game Center app but every time I tap on the app it opens too a blank white screen. I have tried several times to reset my phone but I get the same result.

    I am trying to get into my Game Center app but every time I tap on the app it opens too a blank white screen. I have tried several times to reset my phone but I get the same result.

    Hello there Sweebs44,
    It sounds like you are tapping the Game Center app to open it, but the screen is blank. I recommend signing out of your account:
    Go to Settings > Game Center, where you can:
    Sign out (tap your Apple ID)
    From: iPhone User Guide
              http://help.apple.com/iphone/7/#/iph6c493cac
    Then close all the running apps:
    iOS: Force an app to close
    http://support.apple.com/kb/ht5137
    Double-click the Home button.
    Swipe left or right until you have located the app you wish to close.
    Swipe the app up to close it.
    When you have done that restart the phone sign back into your account and try again:
    iOS: Turning off and on (restarting) and resetting
    http://support.apple.com/kb/ht1430
    Thank you for using Apple Support Communities.
    Take care,
    Sterling

  • Have been trying to get a simple rollover tooltip going but haven't had any success...need some help

    Have been trying to get a simple rollover tooltip going but haven't had any success...need some help please

    start watching at 11min
    http://www.youtube.com/watch?v=6_FJYN36_94
    hope this helps

  • I'm trying to get an appointment for 2 months but not get  an appointment.you help me.

    I'm trying to get an appointment for 2 months but not get  an appointmen
    My name's Burak. I'm writting you from Istanbul.I have a problem. My phone's not work.It's corrupt. I'm trying to get an appointment for 2 months but not get  an appointment.I went to apple store zorlu center. They did't help me. They tell me, you make an appointment to come. I want to sue them.

    You want to sue them?  Good luck, pal.  You can make an appointment up to 7 days in advance.  Here's what you need to do.  Keep checking the reservation page for the Apple Store you want to go to, and as soon as an appointment slot is available, take it.  Or go to the store as soon as it opens, and tell them you want the first available slot if someone cancels, and wait.
    Suing someone for being so busy that you can't make an appointment at their business is ridiculous.  You have no right to be seen before someone that A) Made an appointment or B) Has been waiting longer than you to be seen.

  • I am trying to get the free trial to cc but it said there seems to be a problem with the files (Error code: 205)

    I am trying to get the free trial to cc but it said there seems to be a problem with the files (Error code: 205) please help

    BOILERPLATE TEXT:
    Note that this is boilerplate text.
    If you give complete and detailed information about your setup and the issue at hand,
    such as your platform (Mac or Win),
    exact versions of your OS, of Photoshop (not just "CS6", but something like CS6v.13.0.6) and of Bridge,
    your settings in Photoshop > Preference > Performance
    the type of file you were working on,
    machine specs, such as total installed RAM, scratch file HDs, total available HD space, video card specs, including total VRAM installed,
    what troubleshooting steps you have taken so far,
    what error message(s) you receive,
    if having issues opening raw files also the exact camera make and model that generated them,
    if you're having printing issues, indicate the exact make and model of your printer, paper size, image dimensions in pixels (so many pixels wide by so many pixels high). if going through a RIP, specify that too.
    etc.,
    someone may be able to help you (not necessarily this poster, who is not a Windows user).
    a screen shot of your settings or of the image could be very helpful too.
    Please read this FAQ for advice on how to ask your questions correctly for quicker and better answers:
    http://forums.adobe.com/thread/419981?tstart=0
    Thanks!

  • How to get number of rows

    Hi!
    Can someone tell me how to obtain the number of rows returned by a query?
    I tried to use getFetchSize() but it seems to always return 0 for my query.
    THANKS in advance!
    ResultSet testRS = Stmt.executeQuery(testStmt);
    int size = testRS.getFetchSize();
    System.out.println("size = " + size);
    Wo Lay

    To get the number of rows returned, you have to loop
    through the result set, or do a "select count" (which
    is more efficient since it doesn't have to pass the
    rows across the network) ...Looping around the entire result set is not very performance efficient. Then agin if you use count (*) you actually execute an extra query.
    The best thing you can do is:// assume res is your result set.
    res.last();
    int rows = res.getRow();This way you only have to execute one query and you don't really need to loop around the entire result set. Of course you will have to have a scrollable result set, if this is supported by your DB.
    Hope that helped.
    afotoglidis

  • Get Number of Rows?

    Is there a simple way with ADDT to get the NUMBER of rows in a table that contain a particular ID or other value? Would QueryBuilder be the place to do this, or is there a snippet of code?
    For example, to find out how many students have registered for a class with an ID of "100", I would need to query the classes table to find out how many student IDs fall within a row containing the class ID "100"...

    Hi Brian,
    assuming that you´ve established a standard recordset that´s based on such a "WHERE ID = 100" criterion, you can always use DW´s native "Display Record Count -> Display Total Records" behaviour to echo the number of records which match that criterion.
    What´s getting evaluated by this behaviour, is the "mysql_num_rows" value, which returns the number of rows in a recordset:
    Cheers,
    Günter Schenk
    Adobe Community Expert, Dreamweaver

  • Trying to get the last row from a resultset

    Hi,
    I'm trying to do a query to postgreSQL and have it return the last updated value, (last row).
    My prepared statement is returning the correct results, but i'm having a problem getting the latest value.
    I'm using a comboBox to drive a textfield, to load the last entered values in depending on which item in the comboBox is selected.
    I've tried a variety of things and most seem to return the first row, not showing the updated values.
    Or, if it does work, it takes to long to load, and i get an error.
    here is the working code;
    Object m = machCBX.getSelectedItem():
    try { PreparedStatment last = conn.prepareStatement("SELECT part, count FROM production WHERE machine = ?",
    ResultSet.TYPE_SCROLL_INSENSITIVE,  //tried both INSENSITIVE and SENSITIVE
    ResultSet.CONCUR_READ_ONLY);
    last.setString(1, String.valueOf(m));
    rs. = last.executeQuery();
    if(rs.isAfterLast) == false ) {
    rs.afterLast();
    while(rs.previous()) {
    String p = rs.getString("part");
    int c = rs.getInt("count");
    partJTX.setText(p);
    countJTX.setText(c);
    }this grabs values, but they are not the last entered values.
    Now if i try to use rs.last() it returns the value i'm looking for but takes to long, and i get:
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space I also know using ra.last() isn't the best way to go.
    I'm just wondering if there is another way other than getting into vectors and row count? or am i better off to go with the later?
    thanks
    -PD

    OK, you've got a major misunderstanding...
    The relational database model is built on the storage of sets - UNORDERED sets. In other words, when you hand a database a SELECT statement without an ORDER BY clause, the database is free to return the results in any order.
    Now it so happens that most databases will happen to return data retrieved by an unordered SELECT, at least for a while, in the same order that it was inserted, especially if no UPDATE or DELETE activity has occured, and no database maintenance has occured. However, eventually most tables have some operation that creates a "space" in the underlying storage, or causes a row to expand and have to be moved or extended, or something. Then the database will start returning unordered results in a different order. If you (or other people) never ever ever UPDATE or DELETE a table, then on some databases the data might well come out in insertion order for a very very long time; given human nature and the way projects tend to work, relying on that is a sucker's bet, IMHO.
    In other words, if you want the "most recent" something, you need to store a timestamp with your data. (With some databases, you might be able to take advantage of some non-standard feature to get "last updates" or "row change timestamps", but I know of no such for Postgres.
    While this won't solve your major problem, above, your issue with rs.last is probably occuring because Postgres by default will prefetch your entire ResultSet. Use Statement.setFetchSize() to change that (PreparedStatement inherits the method, of course).

  • How to get number of rows in tableview

    Hello,
    Could anybody help me on how to get the number of rows in a tableview? Sort of using DESCRIBE TABLE in classic ABAP.
    Thanks,
    Ricky

    There is an attribute of the class CL_HTMLB_TABLEVIEW named
    ROWCOUNT.
    Inside an event u need to get this info use this code:
    Replace 'Monitor'  for the ID of your tableview
    DATA: tv TYPE REF TO CL_HTMLB_TABLEVIEW,
          r_count  type i,
          tv ?= CL_HTMLB_MANAGER=>GET_DATA(
          request = runtime->server->request
          name = 'tableView'
          id = 'Monitor' ).
          IF tv IS NOT INITIAL.
            DATA: tv_data TYPE REF TO CL_HTMLB_EVENT_TABLEVIEW.
            tv_data = tv->data.
            r_count = tv_data->rowcount.
          ENDIF.

  • Get number of rows in ResultSet object

    Can anybody guide me on how to get the total number of rows in a result set object without iterating through all rows? Thanks.

    Hi Soon,
    You mention that you want to get the row count without iterating through the "ResultSet". I don't know what database you are using, but I want to inform you that with my Oracle 8i (8.1.7.4) database, using JDK 1.3.1 and the matching JDBC (thin) driver, the implementation of the "last()" method (that others have recommended that you use), actually iterates through the whole "ResultSet".
    What I do is first execute a "SELECT COUNT(1)" query (using a literal "1") to get the number of rows -- this is much faster than executing the "last()" method.
    Then I execute my actual query.
    Hope this helps you.
    Good Luck,
    Avi.

  • Resukt:Get number of rows from 3 different table...?

    Hi All,
    I need to find out number of rows from 3 different table for the same conditions. Instead of writing 3 queries, is it possible to get it thru one query?
    For example, i need to find out number of rows in tables where name = 'Ameet' from 3 different table, i will end writing 3 queries.
    1. select count(1) from table_a where name = 'Ameet';
    1. select count(1) from table_b where name = 'Ameet';
    1. select count(1) from table_c where name = 'Ameet';
    Is it possible to write a single query to get result of all above 3 queries?
    result:
    table_a table_b table_c
    34 44 2

    One way:
    SELECT      (SELECT     COUNT(*) AS CNT FROM TABLE_A WHERE name='Ameet') AS TABLE_A
    ,     (SELECT     COUNT(*) AS CNT FROM TABLE_B WHERE name='Ameet') AS TABLE_B
    ,     (SELECT     COUNT(*) AS CNT FROM TABLE_C WHERE name='Ameet') AS TABLE_C
    FROM DUALIf you want the total:
    SELECT     SUM(CNT)
    FROM
         SELECT     COUNT(*) AS CNT FROM TABLE_A WHERE name='Ameet'
         UNION ALL
         SELECT     COUNT(*) FROM TABLE_B WHERE name='Ameet'
         UNION ALL
         SELECT     COUNT(*) FROM TABLE_C WHERE name='Ameet'
    )

  • Get Number of rows from a sql query.

    I am reading data from a sql query in a BLS transaction and I would like to know the number of rows returned.
    Is there an easy way to do this without looping through the table?
    Thanks Jasper

    Hi Jasper,
    You can use the XPATH count function similar to  this:
    GetTagList.Results{count(/Rowsets/Rowset/Row)}
    Kind Regards,
    Diana Hoppe

  • Getting number  of rows in a resultset

    I am moving my application from JDBC 1.0 to JDBC 2.0. In JDBC 1.0, I used to fire separate query for count(*) to get the no. of rows in results but with JDBC 2.0 is there way that I dont need to execute a separte query to get no. or rows in a resultset.
    Thanks
    Sushil

    There is Sushil if your driver supports it. It can be done with scrollable ResultSets.
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = stmt.executeQuery("sql");
    if (rs != null) {
    rs.last();
    int numRows = rs.getRow();
    // be sure to set rs.beforeFirst() if you want to process the ResultSet
    }

Maybe you are looking for