java result set

Discussion in 'Java' started by Sarath, Nov 15, 2006.

  1. Sarath

    Sarath Guest

    Hi,

    I have a doubt.
    Is there a way that allows me to get all the values of a column from a
    java.sql.ResultSet in an array .
    I can loop on the resultset and add each of the values to an array.
    But is there method to do this by default i.e., a method from
    java.sql.ResultSet ???

    right now what i m doing is
    rs i my ResultSet containing the output of the query.

    i =0;
    int arr[] = new int [];
    while(rs.next()){
    int val = rs.getInt(1);
    arr = val;
    i++;
    }

    I checked in javadoc but i didnt find one.
    what i want is a method that will return me the array if i pass a
    resultset and columnIndex to it.


    Thank you,
    Sarath.B
    Sarath, Nov 15, 2006
    #1
    1. Advertising

  2. JDBC ResultSet essentially works off of a single row pointer. What that
    means is, at a given point in time, you can only operate on a "single"
    row in a resultset. Sure you can move the pointer back and forth based
    on the scrollable settings, but thats pretty much it. You will have to
    keep doing rs.next() to iterate through the whole result set.

    -cheers,
    Manish
    Manish Pandit, Nov 15, 2006
    #2
    1. Advertising

  3. Sarath

    steve Guest

    On Wed, 15 Nov 2006 15:38:27 +0800, Sarath wrote
    (in article <>):

    > Hi,
    >
    > I have a doubt.
    > Is there a way that allows me to get all the values of a column from a
    > java.sql.ResultSet in an array .
    > I can loop on the resultset and add each of the values to an array.
    > But is there method to do this by default i.e., a method from
    > java.sql.ResultSet ???
    >
    > right now what i m doing is
    > rs i my ResultSet containing the output of the query.
    >
    > i =0;
    > int arr[] = new int [];
    > while(rs.next()){
    > int val = rs.getInt(1);
    > arr = val;
    > i++;
    > }
    >
    > I checked in javadoc but i didnt find one.
    > what i want is a method that will return me the array if i pass a
    > resultset and columnIndex to it.
    >
    >
    > Thank you,
    > Sarath.B
    > , but you had better keep it generic

    then I am afraid you will have to write one.
    , but you had better keep it generic, do not assume that you will always
    play with 'int'
    use an array of objects and return rs.object(n) as in

    record = rset.getObject(1 + i);

    to return 1 record

    and

    record = rset.getObject(1 + i);

    // COPY THE DATA TO A LOCAL ARRAY OF THE RIGHT SIZE
    }

    loop++;

    Arraydata.add(record);

    to return an array of records.

    Do not type convert in your get routines. if later you need to code reuse or
    modify your table, you will seriously be buggered up, ESP. if you change the
    order of your select statement.

    keep your record data & type intact, and only convert for input or display,
    then when you go to put it back into the database , it will slide right in,
    because you have not "type" converted your data.
    Do not assume that just because you can get an int from the database, that
    you can automatically put an int back.

    Steve
    steve, Nov 15, 2006
    #3
  4. Sarath

    Sarath Guest

    steve wrote:
    > On Wed, 15 Nov 2006 15:38:27 +0800, Sarath wrote
    > (in article <>):
    >
    > > Hi,
    > >
    > > I have a doubt.
    > > Is there a way that allows me to get all the values of a column from a
    > > java.sql.ResultSet in an array .
    > > I can loop on the resultset and add each of the values to an array.
    > > But is there method to do this by default i.e., a method from
    > > java.sql.ResultSet ???
    > >
    > > right now what i m doing is
    > > rs i my ResultSet containing the output of the query.
    > >
    > > i =0;
    > > int arr[] = new int [];
    > > while(rs.next()){
    > > int val = rs.getInt(1);
    > > arr = val;
    > > i++;
    > > }
    > >
    > > I checked in javadoc but i didnt find one.
    > > what i want is a method that will return me the array if i pass a
    > > resultset and columnIndex to it.
    > >
    > >
    > > Thank you,
    > > Sarath.B
    > > , but you had better keep it generic

    > then I am afraid you will have to write one.
    > , but you had better keep it generic, do not assume that you will always
    > play with 'int'
    > use an array of objects and return rs.object(n) as in
    >
    > record = rset.getObject(1 + i);
    >
    > to return 1 record
    >
    > and
    >
    > record = rset.getObject(1 + i);
    >
    > // COPY THE DATA TO A LOCAL ARRAY OF THE RIGHT SIZE
    > }
    >
    > loop++;
    >
    > Arraydata.add(record);
    >
    > to return an array of records.
    >
    > Do not type convert in your get routines. if later you need to code reuse or
    > modify your table, you will seriously be buggered up, ESP. if you change the
    > order of your select statement.
    >
    > keep your record data & type intact, and only convert for input or display,
    > then when you go to put it back into the database , it will slide right in,
    > because you have not "type" converted your data.
    > Do not assume that just because you can get an int from the database, that
    > you can automatically put an int back.
    >
    > Steve


    Yes , I agree.

    The method has to be generic.

    Thanks for the comments.
    Sarath, Nov 16, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.Ram
    Replies:
    7
    Views:
    649
  2. Pavel
    Replies:
    7
    Views:
    524
    Pavel
    Sep 19, 2010
  3. Lakshmi Sreekanth

    i = 10; result = ++i - --i; How result become ZERO

    Lakshmi Sreekanth, Sep 21, 2010, in forum: C Programming
    Replies:
    52
    Views:
    1,173
    Nick Keighley
    Sep 23, 2010
  4. Mr. Buffoon
    Replies:
    4
    Views:
    291
    Eric Sosman
    Sep 23, 2010
  5. Michael Tan
    Replies:
    32
    Views:
    960
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page