473,508 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Database single connection

9 New Member
I would like to create a single Database connection point that I can use for several of my classes in my Java Web Application.

Here is my ConnectionManager Class:
Expand|Select|Wrap|Line Numbers
  1. public class ConnectionManager {
  2.     private static Connection activeConnection = null;
  3.     public static Connection getConnection() {
  4.         if (activeConnection = null) {
  5.             Class.forName("OracleThinInfoHere...");
  6.             activeConnection = DriverManager.getConnection("jdbc:oracle:thin:@myname:1234:orcl", "scott", "tiger");
  7.         }
  8.         return activeConnection;
  9.     }
  10. }
Now how would I access the ConnectionManager in each one of my classes?

For example here is one:
Expand|Select|Wrap|Line Numbers
  1. public class MainClass
  2. {
  3.  
  4. public ConnectionManager.getConnection();
  5. public Connection connection;
  6.  
  7. //I tried my db connection as this and it didnt return any results
  8. public MainClass(connection)
  9. {
  10.      this.connection = ConnectionManager.getConnection();   
  11. }
  12.  
  13. public int matcher(BeanClass abc)
  14. {
  15.      try
  16.      {
  17.          new OtherDbClass(connection).insertDbMethod(abc);
  18.      }
  19.      catch(Exception e)
  20.      {
  21.          e.printStackTrace();
  22.      }
  23.      finally
  24.      { 
  25.         //closing statements here
  26.      }

Another Class looks like this:
Expand|Select|Wrap|Line Numbers
  1. public class OtherDbClass {
  2.    private Connection connection;
  3.    public OtherDbClass(Connection connection)
  4.    { 
  5.       this.connection = ConnectionManager.getConnection();
  6.    }
  7.  
  8. public int insertDbMethod(BeanClass abc) 
  9. ...
  10. ...
  11. }
Please advise.
Oct 18 '07 #1
3 1598
heat84
118 New Member
1.
public class OtherDbClass {
2.
private Connection connection;
3.
public OtherDbClass(Connection connection)
4.
{
5.
this.connection = ConnectionManager.getConnection();
6.
}
7.

8.
public int insertDbMethod(BeanClass abc)
9.
{
10.
...
11.
...
12.
}

There is no need for passing the connection object to the OtherDB constructor so you can remove that Connection parameter. When you instantiate an object of the OtherDB class , the connection will be created.
Nov 12 '07 #2
chicago1985
9 New Member
1.
public class OtherDbClass {
2.
private Connection connection;
3.
public OtherDbClass(Connection connection)
4.
{
5.
this.connection = ConnectionManager.getConnection();
6.
}
7.

8.
public int insertDbMethod(BeanClass abc)
9.
{
10.
...
11.
...
12.
}

There is no need for passing the connection object to the OtherDB constructor so you can remove that Connection parameter. When you instantiate an object of the OtherDB class , the connection will be created.
Thanks, I wasnt sure if it is okay to put a Database connection in a constructor?
Nov 12 '07 #3
heat84
118 New Member
Hope its working out
Nov 13 '07 #4

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

Similar topics

16
7451
by: noah | last post by:
Does PHP have a feature to associate Cookie sessions with a persistent database connection that will allow a single transaction across multiple HTTP requests? Here is how I imagine my process: I...
6
5046
by: Graham Mattingley | last post by:
Hello group, I have a question hope it does not turn out to be a silly on.... On everyone of my pages I have an a include page at the top and at the bottom of the page one is the database...
5
1822
by: Ranier Dunno | last post by:
Hi, I'm writing a data layer, and would like all components to have a single place for retrieving a connection to the (SQL Server) database. Currently, I'm treating the connection as somewhat...
14
4779
by: Nick Gilbert | last post by:
Hi, I have an asp.net application which runs from a CD-ROM using Cassini. As such, it is single user only. The application connects to an Access database when it is loaded, and keeps the same...
7
2454
by: Lau Lei Cheong | last post by:
Hello, Actually I think I should have had asked it long before, but somehow I haven't. Here's the scenerio: Say we have a few pages in an ASP.NET project, each of them needs to connect to...
35
4806
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
9
2194
by: Jerim79 | last post by:
I am no PHP programmer. At my current job I made it known that I was no PHP programmer during the interview. Still they have given me a script to write with the understanding that it will take me a...
0
4982
bartonc
by: bartonc | last post by:
With one small change to the view/control: self.staticText3 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT3, label='ODBC Data Source Name', name='staticText3',...
221
366977
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
9
2326
by: Peter Duniho | last post by:
Is there a straightfoward API in .NET that allows for inspection of a database? That is, to look at the structure of the database, without knowing anything in advance about it? For example,...
0
7223
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,...
0
7321
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
7377
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
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
412
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...

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.