• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reuse database connection class

 
Grishma Dube
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am writing a simple class to make a connection with the database. I want to reuse the class through out my application. Can anyone tell me, how should i do it? I am using JSP and JavaBeans. I want to reuse the method of the class inside the Bean.

I dont want to use any comple pattern.

TIA

Grishma
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehm...You could just instantiate an instance of your class and call the method....

Or am I missunderstanding you?
 
Grishma Dube
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how to create an instance of the class if i'm not writing any main method.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main method is only required as an access point for the JVM. If you are writing a class as a stand alone app, then you need a main method to run it. If your class is part of an application however, it would be called by another class. And course if you are using JSP there is no main method there either.

This is pretty basic stuff - if you are unsure about how this works I'd spend a little time reading up on the basics before trying any JDBC.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Grishma,
Just create an instance of the object from the JSP. Since it is an object it is reusable.

Note: I strongly advise you not to do do database access from the JSP for any real system. It's better to go through a servlet.
 
tommy k. lee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to reuse the data access code is to use the DAO (data access object) design pattern.

Take a look: http://java.sun.com/blueprints/patterns/DAO.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic