aspose file tools*
The moose likes JDBC and the fly likes how do do this? -> depending on the query, returns different objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how do do this? -> depending on the query, returns different objects" Watch "how do do this? -> depending on the query, returns different objects" New topic
Author

how do do this? -> depending on the query, returns different objects

Yichao Zhu
Greenhorn

Joined: Oct 15, 2009
Posts: 5
hi, i'm not sure if this question goes here.

and I'm learning java by myself. i was trying to make an small application, and i found a situation like following:

let's say I have 3 tables, user(id_usr, name, surname) , sport(id_sport, sport), hobbies(id_usr, id_sport), and 3 classes with the same attribute and only setters and getters (i think they call beans right?)
and i want to make a black box like this:

if I the input is a query like "select * from user;" it will turn the resultset into a list of "user objects",
and if the is a query like "select * from sport" it will return a list of objects sport and so on...


basically it should return a list of objects depending on the the query.

i have been thinking using introspection , depending on what attributes have the class, then what colum will takeout and then build the object...


i dont know if you understand me :S or if i'm on the right way...

but any idea, guidence, help, arctle, website will be appreciate

thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Kossel xxxxx, please check your private messages.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Yichao,
Most people using JDBC just write the code for each query separately. For larger applications, an object/relational mapping framework such as Hibernate does the work of creating Java objects for you.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Yichao Zhu
Greenhorn

Joined: Oct 15, 2009
Posts: 5
thanks!! i think that's exactly what i m looking for

but looks like kind of complicated hehe
Mohamed Khamis
Greenhorn

Joined: Oct 18, 2009
Posts: 3
I am not sure if I totally understand your problem, however when I want to get stuff out of the database into objects I use a DAO,

DAO: DataAccessObject, its a class that takes accesses the database. If we're dealing with Users, you can make a class UserDAO, that has a method which takes a userId as a parameter, and returns an instance of the User Object (also known as UserModel). Or it can have a method getAllUsers(); that returns a List of UserModels.

Usually in applications you'll know that by clicking this button for example, you'll need to get a specific user using his/her id, or get a list of users. I think thats the simplest way, check the MVC and fasade design patterns, they make use of DAOs.

Hope this helps
 
 
subject: how do do this? -> depending on the query, returns different objects