Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:09:20+00:00 2026-06-17T13:09:20+00:00

I am developing a web application using JSP & Servlet (IDE: Eclipse, Database: Oracle10).

  • 0

I am developing a web application using JSP & Servlet (IDE: Eclipse, Database: Oracle10).

I have developed java class which returns a static connection, and that connection will be used by my entire web application.

public class DBConnection 
{
    private static Connection con = null;

    static Connection getConnection(String str)
    {
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("MyValuesHere");
            System.out.println("New instance of Connection is created for: "+str);
        }
        catch(ClassNotFoundException cnfe)
        {
            System.out.println("Error loading class!");
            cnfe.printStackTrace();
        }
        catch(SQLException sqle)
        {
            System.out.println("Error connecting to the database!");
            sqle.printStackTrace();
        }
        return con;
    }//getConnection()
}//class

Above class is working fine. Then I have another 4 java classes for

  1. Inserting
  2. Updating
  3. Deleting
  4. Selecting

data from database using the above connection. So in those 4 classes I am getting connection which is created in my DBConnection class, and those four classes are also working fine. This four classes are used in my all Servlet’s.

To get Connection in those 4 classes I am writing following line:

private static Connection con = DBConnection.getConnection("UpdateQuery.java");

But problem is that I want to share the same connection in all four classes, but connection is created separately in those 4 classes. So how should I share the same connection in those four classes? is there better way of doing this? and if I use this approach will there be any issues in web application because of sharing the connection for whole application?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:09:21+00:00Added an answer on June 17, 2026 at 1:09 pm

    You should save the created connection instance into a private static field in DBConnection, and when getConnection is called, you check if the field is null, then create the connection, then return it:
    if (connection == null) {
    connection = createConnection();
    }
    return connection;

    where connection is a private static Connection connection field of DBConnection class.

    However I strongly suggest to not use this approach as sharing a connection between concurrent threads will cause serious problems. I suggest to use connection pooling

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I developing a web application using JSP & Servlet (IDE: Eclipse, Database: Oracle10). I
I am developing a web application using JSP & Servlet (IDE: Eclipse, Database: Oracle10).
I am developing a web application using JSP & Servlet (IDE: Eclipse, Database: Oracle10).
I am developing a web application using JSP & Servlet.(IDE: Eclipse, Container: Tomcat7.0) To
I am developing web application using JSP and Servlet (IDE: Eclipse, Container: Tomcat7.0, DB:
I am developing a web application using Java/JSP. One class, say Student, has a
I am developing a web application. I am using Eclipse as IDE. I was
I'm developing a web application using Java EE6 and JSF 2.0. I have a
I am using eclipse Indigo IDE ..I am developing a jsp application ( myProject
I am developing a web application using JSP and Servlet. I am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.