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
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.
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