473,395 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Best connection?

What is the best way to connect to a database in Java Class?
I have used 3 different ways and would like to know what is the most efficient one.

1- Put the connection as a data member:

Expand|Select|Wrap|Line Numbers
  1. public class DatabaseHelperClass
  2.     private Connection connection = new DbConnectionManager().getConnection();
  3.  
  4.     public void insertDb(DbBean db) throws SQLException  {
  5.     PreparedStatement stmt = connection.prepareStatement("insert ..");
  6.     ...
  7.     }
  8.  
  9.    public void updateDb(DbBean db) throws SQLException  {
  10.      PreparedStatement stmt = connection.prepareStatement("update ..");
  11.      ...
  12.     }
2 - Put connection in Constructor:

Expand|Select|Wrap|Line Numbers
  1. public class DatabaseHelperClass
  2.     public DatabaseHelperClass() {
  3.     Connection connection = new DbConnectionManager().getConnection();
  4.     }
  5.  
  6.      public void insertDb(DbBean db) throws SQLException  {
  7.      PreparedStatement stmt = connection.prepareStatement("insert ..");
  8.      ...
  9.     }
  10.  
  11.     public void updateDb(DbBean db) throws SQLException  {
  12.      PreparedStatement stmt = connection.prepareStatement("update ..");
  13.      ...
  14.     }
3- Put connection in every method that needs a connection:

Expand|Select|Wrap|Line Numbers
  1. public class DatabaseHelperClass
  2.      public void insertDb(DbBean db) throws SQLException  {
  3.      Connection connection = new DbConnectionManager().getConnection();
  4.      PreparedStatement stmt = connection.prepareStatement("insert ..");
  5.      ...
  6.     }
  7.  
  8.  public void updateDb(DbBean db) throws SQLException  {
  9.      Connection connection = new DbConnectionManager().getConnection();
  10.      PreparedStatement stmt = connection.prepareStatement("update ..");
  11.      ...
  12.     }
Nov 15 '07 #1
1 1670
r035198x
13,262 8TB
What is the best way to connect to a database in Java Class?
I have used 3 different ways and would like to know what is the most efficient one.

1- Put the connection as a data member:

Expand|Select|Wrap|Line Numbers
  1. public class DatabaseHelperClass
  2.     private Connection connection = new DbConnectionManager().getConnection();
  3.  
  4.     public void insertDb(DbBean db) throws SQLException  {
  5.     PreparedStatement stmt = connection.prepareStatement("insert ..");
  6.     ...
  7.     }
  8.  
  9.    public void updateDb(DbBean db) throws SQLException  {
  10.      PreparedStatement stmt = connection.prepareStatement("update ..");
  11.      ...
  12.     }
2 - Put connection in Constructor:

Expand|Select|Wrap|Line Numbers
  1. public class DatabaseHelperClass
  2.     public DatabaseHelperClass() {
  3.     Connection connection = new DbConnectionManager().getConnection();
  4.     }
  5.  
  6.      public void insertDb(DbBean db) throws SQLException  {
  7.      PreparedStatement stmt = connection.prepareStatement("insert ..");
  8.      ...
  9.     }
  10.  
  11.     public void updateDb(DbBean db) throws SQLException  {
  12.      PreparedStatement stmt = connection.prepareStatement("update ..");
  13.      ...
  14.     }
3- Put connection in every method that needs a connection:

Expand|Select|Wrap|Line Numbers
  1. public class DatabaseHelperClass
  2.      public void insertDb(DbBean db) throws SQLException  {
  3.      Connection connection = new DbConnectionManager().getConnection();
  4.      PreparedStatement stmt = connection.prepareStatement("insert ..");
  5.      ...
  6.     }
  7.  
  8.  public void updateDb(DbBean db) throws SQLException  {
  9.      Connection connection = new DbConnectionManager().getConnection();
  10.      PreparedStatement stmt = connection.prepareStatement("update ..");
  11.      ...
  12.     }
2.) Won't compile
3.) Wastes resources by creating a connection everytime even when one is already available. Google connection pooling for more details.
Nov 15 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ryan N. | last post by:
Hello, I saw a brief blurb on this somewhere and am unable to recall where... In the context of Security, what are some best practices for handling -storing, locating, retrieving- database OLEDB...
6
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which...
2
by: Steve | last post by:
Hi, I am a relatively new user to vs.NET, and currently developing a ASP.NET web site using MS access, with the view to moving it to SQLServer. My problem is, how do I set up the connection to...
51
by: bigHairy | last post by:
Hello. I have been teaching myself .NET over the last few months and have had some success. I would like to ask a question though... A number of examples I have followed have the following in...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
3
by: Nemisis | last post by:
Guys, I would like to write a error handler, or something, that will allow me to write to a database when an error occurs on my site. I am trying to implement this in the global.asax file a the...
3
by: Venkat | last post by:
Hi, We have a windows application developed in c# and SQL Server 2005. Our application need to execute more than one command (ExecuteReader and ExecuteScalar) at a single time. Till now we have...
4
by: laziers | last post by:
Hi there Anybody knows what is the best way to manage creation 2000 of the database connections at the same time? Now Im doing it somethink like this: using ( Connection conn =...
4
by: trullock | last post by:
Hi, Can anyone suggest the best way to go about the following... I'm tracking clicks (mouse down x,y coordinates) on a web page by using some javascript to create an XHR which sends the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.