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 / Databases / June 2004

Tip: Looking for answers? Try searching our database.

ResultSet within ResultSet

Thread view: 
James - 14 Jun 2004 15:30 GMT
Hi,

I want to do something, I'm sure it must be fairly straight forward,
however I suspect there is a better way(s) to do it that I don't know
about.

I need to retrieve data from a database. So far so good :-)

The data is linked with a one-to-many relationship. There is a
customer table and then each customer can have many accounts. Now what
I want to have in my Java is an object model where I have a Customer
object and as a member variable a Vector of Account objects belong to
that customer. Now when I am doing my database load I could do
something like this:

Vector customers = null;
Vector customerAccounts = null;

Statment stmt = "SELECT * FROM CUSTOMERS;"
rsc = stmt.execute();  \\ get all the customer data

customers = new Vector();

while(rsc.next()) {
 Customer c = new Customer();
 customer.setName(rs.get("NAME"));
 customer.setAddress(rs.get("ADDRESS")); \\ loop through each
customer
 ...
 Statment stmt = "SELECT * FROM ACCOUNTS WHERE CUST_ID =" +
rs.get("CS_ID");
 rsa = stmt.execute();  \\ get all the accounts for the current
customer
 customerAccounts = new Vector();

 while(rsa.next()) {

     Account a = new Account();
     a.setNumber(rsa.get("ACCOUNT_NUMBER"))
     ... // etc for other properties

     customerAccounts.add(a);
   }
 customer.addAccounts(customerAccounts);

 customers.add(customer);
}

First of all I am wondering if this is reasonable?

Secondly it seems a bit of a waste to execute a query through JDBC and
have it executing a sub query for every row. It might be better to
place such data intensive operations onto the server as PL/SQL but if
I did that how would I get my object model built up in the Java?

I am using EJB so perhaps a CMR would be of some use here? Again
however I am not 100% sure how that would work. Although I am not sure
how well that would work as the database is actually a bit messy, I
have simplified the relationships above, in reality there are lots of
Views and other tables involved. Also the existing beans in same app
use BMP.

Any advice appreciated. Thanks.
John - 14 Jun 2004 15:40 GMT
> Hi,
>
[quoted text clipped - 59 lines]
>
> Any advice appreciated. Thanks.

Just retrieve a big f.ck-off resultset (technical term) that tells you
which customer is associated with which account. Now assuming you have
already created all your customer beans and account beans, you can just
do one pass of the aforementioned table and set the references for
customers and accounts simultaneously.

If you're all sorted with BMP, I wouldn't start buggering about with CMR
if I were you.

John
Chris Smith - 14 Jun 2004 15:55 GMT
> First of all I am wondering if this is reasonable?

Well, it works, if that's what you're asking.  You do need to be careful
to use a different Statement for the inner query, since a new
executeQuery on the outer statement would close the outer ResultSet.

If you've got a potentially unlimited number of accounts for a customer,
then you could of course run into problems trying to call them all into
memory at once, but that's your call to make.

Another alternative would be to run the query:

   select c.*, a.* from customers c, accounts a
       where c.cs_id = a.cust_id
       order by c.cs_id;

Then your loop would look something like:

   int thisCustomerID = -1;
   Customer thisCustomer = null;

   while (next)
   {
       if (c.cs_id != thisCustomerID) thisCustomer = new customer;
       customer.addAccount(new account);
   }

That's pseudocode, of course.  The trade-off is that you have more total
data coming over the wire, but fewer round-trips.  That's likely to be a
win, unless your customer records contain very large amounts of data.

Incidentally, it's pretty much *never* a good idea to use *-style select
in code.  I'd definitely replace it with explicit field names, as soon
as possible.

> It might be better to
> place such data intensive operations onto the server as PL/SQL but if
> I did that how would I get my object model built up in the Java?

You can't get PL/SQL to build Java objects for you on the client.  You
could, of course, encapsulate either of the original queries or the
query I just write into a stored procedure, but this doesn't solve your
problem.

> I am using EJB so perhaps a CMR would be of some use here? Again
> however I am not 100% sure how that would work.

It's certainly not worth resorting to CMP over something like this.  
That would be extremely limiting on your use of the database, and the
container couldn't really do a better job than you can in this instance.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



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



©2008 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.