• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Dynamically build a javaBean

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to build a JavaBean dynamically based on the DB table column names and assoicated data? For example, if I get back of 5 columns in a db table, I need to create a javaBean with those 5 column names as variables and their getters/setters as well. Next time, if I get 7 columns, I can create a javaBean with 7 variables.

Thanks in advance.


David
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The magic you're asking about is called "Object-Relational Mapping." We've got a whole forum devoted to this topic here. There are many tools -- commercial as well as free -- that do this for you. Perhaps the most popular Java ORM tool is Hibernate.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is there a way to build a JavaBean dynamically based on the DB table column names and assoicated data? For example, if I get back of 5 columns in a db table, I need to create a javaBean with those 5 column names as variables and their getters/setters as well. Next time, if I get 7 columns, I can create a javaBean with 7 variables.



You may use reflection to accomplish this. It is going to be messy because to use the so created java beans the programmer don't have a pre-knowledge of various setters and getters.

If you are not keen about the getters and setters, you may do the following.

Keep the properties in a map (key value pair) where
key is the field name/variable name/column name
Value is the value of the field

Define getValue(String fieldName)
setValue(String fieldName, Object value)

value could be of the type String or primitive wrappers.

One advantage is that if you get the keys of the properties, you can know various fieldnames.

This kind of beans is difficult to maintain especially debugging is tough.

From my experience I like the proper getters and setters. If the structure of your table is not changing dynamically, you can assume the name of the accessors before using the beans.
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic