• Post Reply Bookmark Topic Watch Topic
  • New Topic

Data base logic inside a Java Bean  RSS feed

 
venkata vinay bysani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a plain Java Bean with few variables and the respective setters and getters in it. My objective is to store the bean data into a database table so could you please suggest the best way of doing it.

Can i write a method inside the bean to get the DB connection and create a prepared state to execute the insert query in that method itsself

or

Writing a separate class for all the DBOperations and do all the DB stuff in that class by passing the bean to that method

Which way is good in terms of best practice wise and design wise also suggest if there are any better way to do the same.

And the solution i am looking for is in a plain JDBC implementation not using any ORM frame works.
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing a separate class for all the DBOperations and do all the DB stuff in that class by passing the bean to that method


That's your best bet. You want to encapsulate your BusinessObjects (your bean) from your DatabaseAccessObjects (DAO) as they serve different purposes. This approach has a number of benefits. Many of those benefits are outlined in Oracle's Best Practices information for DAOs here: http://www.oracle.com/technetwork/java/dataaccessobject-138824.html

Cheers!
Chris
 
saidi Bandi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can i write a method inside the bean to get the DB connection and create a prepared state to execute the insert query in that method itsself

it's a bad idea. java beans are specifically for getters and setters only.
or

Writing a separate class for all the DBOperations and do all the DB stuff in that class by passing the bean to that method

Good choice.
 
venkata vinay bysani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Saidi Bandi & Chris R Barrett for your replies,
I too felt keeping the DB logic in a specific class is a good idea, but felt like keeping in a bean will make logic for that particular module centralize in that module itsself. Because of this two way thinking just thought of finding the best way of doing it and found that keeping a separate DAO is the good way.


Find a very good explanation regarind DAO's and factory patterns from this link, thanks for sharing this.

Oracle's Best Practices information for DAOs here: http://www.oracle.com/technetwork/java/dataaccessobject-138824.html


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!