• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 24207
46
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.
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic