Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Questino with classes

Status
Not open for further replies.

mikedaruke

Technical User
Mar 14, 2005
199
US
I am new to Java. So bare with me. I come from a Perl background.

Java is made up of classes, each having their own methods right?

I am creating an application. Pretty much to learn.

It connects to a database. Well should I make a seperate class that makes the connection? This way I would call it when I need the connection. How would I do that and pass back the connection to the main part of the program? I guess I need to hit the books since I am sure its all basic knowlege that I lack.

Any examples that I can play with to learn would be helpful.
 
I would do it like this :

Code:
public class DbHelper {
  // a static method so you need not instantiate the class
  public static Connection getConnection() {
     // get the connection and return it
  }
}

Then in your client code :

Code:
Connection conn = DbHelper.getConnection();

// .. do stuff

conn.close();

Check out the JDBC tutorial here :
--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top