This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Connection Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Connection Object " Watch "Connection Object " New topic

Connection Object

Glenny Dsilva
Ranch Hand

Joined: May 09, 2005
Posts: 42

I am maintaining a project on servlet database is mysql.

I have a connection instance variable con.

I am using the same connection in many methods. (The code is below just typed one of the methods for explanation)

Is this the correct way to use the connection object in all the methods

or should i have seperate connections in all the methods ?

I have observed that the page does not get properly loaded if i use the instance variable con in all the methods.

But if i open and close connections in all the methods there will be lot of overhead so can anyone suggest a better way...

public class DCRDoctorData extends HttpServlet

Connection con;

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
con = ConnectionHandler.getConnection(dbname);



private void add1()
Statement stmt = con.createStatement(); // using the instance variable

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

You probably want to use Connection Pooling, get a connection everytime you need it and close it in finaly block when you are through using it.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link:
subject: Connection Object
It's not a secret anymore!