473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best connection?

20 New Member
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 1701
r035198x
13,262 MVP
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
4645
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 connection strings? I have typically used a single include file and even considered stuffing the string in a document (XML or otherwise) outside of the root directory. I know of and have used methods to store connection strings in the registry...
6
1861
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 contains methods for serialising and de-serialising objects to the database storage. I put this as a shared class as multiple web clients will be using the class to store and retreive data, the problem I'm haivng now is that I think multiple threads...
2
1151
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 be utilise connection pooling, and to be easily maintained and modified. I was thinking of setting up a DSN, which is how I am currently connecting in ASP but I'm not sure how to do it in .NET. So I created a class which opens a connection and...
51
3668
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 their finally statement Try ......
5
12811
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 more incoming connections and does the BeginAccpet() again. It does an infinite loop this way. My question is: What is the best way to stop this? I thought about putting a boolean in there, but then what if it's still waiting for an incoming...
3
1988
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 moment, but am having problems when a 404 error occurs, i cant access sessionstate. Is writing this code in the global.asax file the best way to do this? I have been searching on the net and hear alot about httphandlers? Will a httphanlder...
3
3170
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 we use with only one connection created during start up of application and will be displose/closed when user logs out of application. With the single database connection we have problems when trying to execute more than command at a particular...
4
1340
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 = Connection.Create ) { conn.Open(); //...
4
1534
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 coordinates to a recording service, such as: /Record.ashx?X=123&Y=456
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9177
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.