Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Better appraoch needed

 
Felix Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I want to know if there is any better approach to setting java objects whith the values returned from DATABASE using jdbc.

For example i have written a generic function which returns a collection of Vector objects which i use and pass back in an array list.

Below is the function i have written



My problem is that i have a A class named person which contains attributes like name,sex,age etc etc.

Now for setting the values in an object i have to know which object is in the vector (column) then accordingly set the property which is quite un ethical.

Currently i am keeping the order of the columns which where added in the vector and setting them in the same order.

Does anyone have a better approach to get around this problem ??. So that i can set the corresponding property depending upon the value in the vector.








[ Edited to use code tags - Paul Sturrock ]
[ June 11, 2007: Message edited by: Paul Sturrock ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll move this to our JDBC forum.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way, you can use the order of columns in the Select query itself so that the resultset will contain the data in the same order and you can fetch and populate the object accordingly.

Say for example, your query is like this:



Then you can retrieve and populate a collection (in your case its a Vector) having individual employee objects as follows.



Some Notes:
  • Just to have a good coding practice, You can use the Constants everywhere when referring to the table column or field so that maintenance will be easy. In such case,

  • you will be referring the column while retrieving from the resultset as,



    Note: You should have referred the same column name in your Select query! You can even use the same in your SQL Query also as it will get inturn substituted.

  • Referring the columns with their names rather than numbers is easy to deal with.
  • You can also deal with "rs.getObject()" method which takes either "int" or "String" attribute. In that case, you need to typecast every value accordingly.



  • HtH.
     
    Felix Thomas
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi,

    Thanks for your suggestion but won't the function will become specific for Employee class which you have used in your example. Then i will have to write a function for every class object to be set.

    :shocked: :p
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, in that case, you should have to the appopriate setObject methods and pass the Object of the class which needs to be populated as arguments to this method.

    Inside the respective class whose object is gonna be populated, you need to have a work around to populate the appropriate field. You can actually pass the field name (as a constant as in my example) to the setObject method.

    But then where will you get all fields of the objects? You should have an array declared for every corresponding object which represents the database table (like String[] fields={"NAME","AGE","SEX"};) etc. This loop you should pass in the while loop where you fetch the values from the resultset and pass the index of fields[] to get it and pass the same name to setObject() method as well. Inside the setObject() method, you need to do a lookup for the appropriate setter method to invoke.

    HtH.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic