Database single connection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chicago1985
    New Member
    • Oct 2007
    • 9

    #1

    Database single connection

    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 ConnectionManag er Class:
    Code:
    public class ConnectionManager {
        private static Connection activeConnection = null;
        public static Connection getConnection() {
            if (activeConnection = null) {
                Class.forName("OracleThinInfoHere...");
                activeConnection = DriverManager.getConnection("jdbc:oracle:thin:@myname:1234:orcl", "scott", "tiger");
            }
            return activeConnection;
        }
    }
    Now how would I access the ConnectionManag er in each one of my classes?

    For example here is one:
    Code:
    public class MainClass
    {
     
    public ConnectionManager.getConnection();
    public Connection connection;
     
    //I tried my db connection as this and it didnt return any results
    public MainClass(connection)
    {
         this.connection = ConnectionManager.getConnection();   
    }
     
    public int matcher(BeanClass abc)
    {
         try
         {
             new OtherDbClass(connection).insertDbMethod(abc);
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
         finally
         { 
            //closing statements here
         }

    Another Class looks like this:
    Code:
    public class OtherDbClass {
       private Connection connection;
       public OtherDbClass(Connection connection)
       { 
          this.connection = ConnectionManager.getConnection();
       }
     
    public int insertDbMethod(BeanClass abc) 
    { 
    ...
    ...
    }
    Please advise.
  • heat84
    New Member
    • Nov 2007
    • 118

    #2
    1.
    public class OtherDbClass {
    2.
    private Connection connection;
    3.
    public OtherDbClass(Co nnection connection)
    4.
    {
    5.
    this.connection = ConnectionManag er.getConnectio n();
    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.

    Comment

    • chicago1985
      New Member
      • Oct 2007
      • 9

      #3
      Originally posted by heat84
      1.
      public class OtherDbClass {
      2.
      private Connection connection;
      3.
      public OtherDbClass(Co nnection connection)
      4.
      {
      5.
      this.connection = ConnectionManag er.getConnectio n();
      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?

      Comment

      • heat84
        New Member
        • Nov 2007
        • 118

        #4
        Hope its working out

        Comment

        Working...