File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Making arraylist from resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Making arraylist from resultset" Watch "Making arraylist from resultset" New topic
Author

Making arraylist from resultset

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
I am returning a result from database using JDBC,

IS there any opensource for converting the each row of the result set into object and put those objects into arraylist and return to java application as arraylist.

I have simply need this functionality I do not want use any ORM
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What you are describing is essentially an ORM. If that is what you need, why don't you want to use one?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
If you do not want to use an ORM such as Hibernate, then just create and object that maps to the data retrieved, populate each atribute with the columns of your resultset, and add them to a list. Again, as Paul mentioned, this is exactly what an ORM is for. Not sure why you do not want to use one.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
Bosun Bello wrote:If you do not want to use an ORM such as Hibernate, then just create and object that maps to the data retrieved, populate each atribute with the columns of your resultset, and add them to a list. Again, as Paul mentioned, this is exactly what an ORM is for. Not sure why you do not want to use one.


is means that i have to create a hash map ,and take each data of column and populate to the map as follow

map <String,String> a =new HashMap();

a,put(row column Name or number ,coulmn data);


once map is populated ,convert it into arraylist

for example if the result set contains 5 rows and 6 columns ,then 30 string object exist in the MAp in the value parameter?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

jacob deiter wrote:... is means that i have to create a hash map ,and take each data of column and populate to the map as follow
map <String,String> a =new HashMap();
a,put(row column Name or number ,coulmn data);

Can't you convert that row(s) in to an object of some kind instead of using String values separately ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You can just create a Data Transfer Object (DTO) for exanple, that maps to your table rows, then just add each one to an array list. So, for example, if you have in your table say employee information such as Name, salary, age, etc. You would have an employee object or so that has those attributes, then populate an employee object for each row in your resultset and then add it to an arraylist.

Hope that helps.

jacob deiter wrote:
Bosun Bello wrote:If you do not want to use an ORM such as Hibernate, then just create and object that maps to the data retrieved, populate each atribute with the columns of your resultset, and add them to a list. Again, as Paul mentioned, this is exactly what an ORM is for. Not sure why you do not want to use one.


is means that i have to create a hash map ,and take each data of column and populate to the map as follow

map <String,String> a =new HashMap();

a,put(row column Name or number ,coulmn data);


once map is populated ,convert it into arraylist

for example if the result set contains 5 rows and 6 columns ,then 30 string object exist in the MAp in the value parameter?
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
Bosun Bello wrote:You can just create a Data Transfer Object (DTO) for exanple, that maps to your table rows, then just add each one to an array list. So, for example, if you have in your table say employee information such as Name, salary, age, etc. You would have an employee object or so that has those attributes, then populate an employee object for each row in your resultset and then add it to an arraylist.



Yes ,I agree with you,

but if the query is decided at run time ,so I do not know what will be the number of cloumn of the result set ,then How to map it to object?
The Gooter
Greenhorn

Joined: Oct 28, 2009
Posts: 1
The following code should get you going in the right direction. It is also possible to utilize Java's powerful sorting algorithms on the resulting ArrayList matrix using Collections.sort() since Strings are Comparable. Strings are also Immutable, so I canonicalize them with String.intern() to save memory. If the ResultSet supports it, I jump to the last row and build an array sized for the number of rows in the ResultSet to avoid the ArrayList having to resize itself.

amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 515
Hi,
Why don't you try using iBatis (SQL Mapper framework).


Regards,
Amit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making arraylist from resultset