This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes ArrayList output from Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "ArrayList output from Database" Watch "ArrayList output from Database" New topic
Author

ArrayList output from Database

Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
I have an output that I get from my Oracle database where it takes parameters from form entries and queries the database. It list the records where it works with an ArrayList to get the output. Everything works great but was wondering if there is a more efficient way to do this?

subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
Hi,

You can use single arraylist which contains the dto/vo objects.
e.g


where ever you wanna access it
...


This will solve your problem and the code will be easier to maintain. Here you are using some valueable patterns also which you need to findout as i am not telling you.


[BPSouther: Added code tags]
[ July 24, 2007: Message edited by: Ben Souther ]

http://subodh-gupta.blogspot.com
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not really sure what you're doing with your code.
The calls to getParameter suggest that you're only dealing with one person.
If so, I don't know why you need to store the fields in array lists.
Or.. is this a search type page that shows all people with first name 'Bill' etc...?


In any case, I prefer to use either an array of row beans or a nested list of lists. In this case, the row bean would have firstName, lastName, and city properties. While iterating through the resultset, build a row bean for each record, populate it, and then add it to the list. To me this is a more logical way to group records/objects than in parallel vertical arrays or lists.

When you say 'efficient' did you mean CPU efficiency or maintenance efficiency?
[ July 24, 2007: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
Thank you, both your posts are really helpful.


In any case, I prefer to use either an array of row beans or a nested list of lists. In this case, the row bean would have firstName, lastName, and city properties. While iterating through the resultset, build a row bean for each record, populate it, and then add it to the list. To me this is a more logical way to group records/objects than in parallel vertical arrays or lists.


The below is like what I am now using thanks to you guys, and I assume this example of Data Transfer Objects is what you mean by iterating through the resultset and building a row bean for each record and populating and adding it to the list? I want to make sure so I can understand the terminology and how DTO work.




Or.. is this a search type page that shows all people with first name 'Bill' etc...?


Yes this is a search type page.
[ July 24, 2007: Message edited by: Joseph Smithern ]
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
yes you are right this is DTO.
Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
Thanks, this is all now "starting" to make sense to me and need just one final clarification on this part:


This translates as DTO (name of destination class) casting the recordList object reference where it allows me to take a value of DTO type and "cast" it to another type, in the sense of molding or reforming the recordList object?
Which in this case the DTO type is casting the recordList.get(i) object to a DTO type which uses the DTO getName() method to fetch the value?
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
No, buddy i am taking out the object from arraylist like
recordList.get(i)
type casting into its class type in your case DTO
and lastly calling the getter method on the DTO object

and you need to iterarte over arraylist to get each record

for avioding type casting you can use genrics of jdk 1.5



one thing more i like to mention here is in your arraylist each record will be stored as object which is ORM.

Now some more to read right? ;)
[ July 25, 2007: Message edited by: subodh gupta ]
 
Don't get me started about those stupid light bulbs.
 
subject: ArrayList output from Database