Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

java result set

Thread view: 
Sarath - 15 Nov 2006 07:38 GMT
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
Manish Pandit - 15 Nov 2006 08:19 GMT
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
steve - 15 Nov 2006 11:47 GMT
> Hi,
>
[quoted text clipped - 23 lines]
> 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
Sarath - 16 Nov 2006 08:39 GMT
> > Hi,
> >
[quoted text clipped - 56 lines]
>
> Steve

Yes , I agree.

The method has to be generic.

Thanks for the comments.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.