Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Yichao Zhu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kossel xxxxx, please check your private messages.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yichao Zhu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks!! i think that's exactly what i m looking for

but looks like kind of complicated hehe
 
Mohamed Khamis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic