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

  • SEARCH
  • Home
  • 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 8361689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:45:07+00:00 2026-06-09T11:45:07+00:00

I wrote a Singleton class for getting connections. However, I cannot get connections using

  • 0

I wrote a Singleton class for getting connections. However, I cannot get connections using Singleton.

I want to use my Singleton for getting several connections and to make query to database, using the connection Singleton. I always try several ways for this, and no success.

This is my Singleton class:

import java.sql.*;

public class ConnectDB {

private static Connection connect;
private static ConnectDB instance;

private ConnectDB()
{

    try {

        Class.forName("com.mysql.jdbc.Driver");
        //connect DB
        connect = DriverManager.getConnection("jdbc:mysql://ip/database","root","password");

    }

    catch(SQLException e)
    {
        System.err.println(e.getMessage());

    }

    catch(ClassNotFoundException e)
    {

        System.err.println(e.getMessage());

    }   
}

  public static ConnectDB getInstance()
  {

      if(instance == null) {

          instance = new ConnectDB();

      }

      return instance;

  }

}

Now, I get the connections:

 public class NameClass {




public void getInfoDatabase()
{   

       Connection cnn = ConnectDB.getConnection();
       PreparedStatement ps;
       ResultSet rs;

       try {

           ps = cnn.prepareStatement("select *  from tables");

           rs = ps.executeQuery();

           while(rs.next())
           {

               String tables = rs.getString("table1");
               System.out.println(tables);
           }
  • 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-09T11:45:09+00:00Added an answer on June 9, 2026 at 11:45 am

    If you want to use multiple connections efficiently you might be after a connection pool:

    In software engineering, a connection pool is a cache of database
    connections maintained so that the connections can be reused when
    future requests to the database are required. Connection pools are
    used to enhance the performance of executing commands on a database.

    Having a Singleton which will return many connections defies the purpose of the Singleton, whose task is to provide the same instance whenever it is called.

    I recommend you take a look at this previous SO thread where various connection pool libraries are discussed.

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

Sidebar

Related Questions

I wrote a singleton class for obtaining a database connection. Now my question is
FWIW I'm using SimpleTest 1.1alpha. I have a singleton class, and I want to
If I have to write a singleton class in C++ I will be using
I wrote a process explorer using C with GUI interface. I want to add
I created a Singleton class in c#, with a public property that I want
Is there a difference using a singleton class and an application scoped managed bean
I want to write a little Singleton class that looks like: #include <vector> class
I wrote a non-static Generic class instantiator for my Abstract Factory design, and use
I wrote a singleton class to keep track of some variables across my application.
I wrote a singleton class to keep track of some variables across my application.

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.