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
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Reply 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
internet detective
Marshal

Joined: May 26, 2003
Posts: 26496
    
  78

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how do do this? -> depending on the query, returns different objects
 
Similar Threads
Hibernate Hell
How to create prepared statement for query with clause where in
Need help with an ArrayList
hibernate string search question
building a search feature in my application, what is the best approach