Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java result set

Reply
Thread Tools

java result set

 
 
Sarath
Guest
Posts: n/a
 
      11-15-2006
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[i] = 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

 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      11-15-2006
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

 
Reply With Quote
 
 
 
 
steve
Guest
Posts: n/a
 
      11-15-2006
On Wed, 15 Nov 2006 15:38:27 +0800, Sarath wrote
(in article <(E-Mail Removed) .com>):

> 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[i] = 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[i] = rset.getObject(1 + i);

to return 1 record

and

record[i] = 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








 
Reply With Quote
 
Sarath
Guest
Posts: n/a
 
      11-16-2006

steve wrote:
> On Wed, 15 Nov 2006 15:38:27 +0800, Sarath wrote
> (in article <(E-Mail Removed) .com>):
>
> > 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[i] = 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[i] = rset.getObject(1 + i);
>
> to return 1 record
>
> and
>
> record[i] = 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i = 10; result = ++i - --i; How result become ZERO Lakshmi Sreekanth C Programming 52 09-23-2010 07:41 AM
Re: i = 10; result = ++i - --i; How result become ZERO Mr. Buffoon C Programming 4 09-23-2010 03:01 AM
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM



Advertisments