File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Reuse database connection class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Reuse database connection class" Watch "Reuse database connection class" New topic
Author

Reuse database connection class

Grishma Dube
Ranch Hand

Joined: Jul 01, 2003
Posts: 273
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

Joined: Apr 14, 2004
Posts: 10336

Ehm...You could just instantiate an instance of your class and call the method....

Or am I missunderstanding you?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Grishma Dube
Ranch Hand

Joined: Jul 01, 2003
Posts: 273
But how to create an instance of the class if i'm not writing any main method.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 26496
    
  78

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
tommy k. lee
Greenhorn

Joined: Jun 16, 2004
Posts: 20
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


SCJP,SCJD SCEA, SCWCD, SCBCD<br />OCP DBA 8i / 9i<br />CISSP, PMP, PHR<br />CCNA, CCDA <br /><a href="http://www.certgear.com" target="_blank" rel="nofollow">http://www.certgear.com</a>
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Reuse database connection class
 
Similar Threads
Stupid question (if any could be such)
Datasource lookup
Java Database connection
Use only 1 connection to SQL Server (newbie ?)
Socket connection performance