Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making arraylist from resultset

 
jacob deiter
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are describing is essentially an ORM. If that is what you need, why don't you want to use one?
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jacob deiter
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Why don't you try using iBatis (SQL Mapper framework).

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic