• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Making arraylist from resultset

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Why don't you try using iBatis (SQL Mapper framework).

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic