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 8237595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:31:15+00:00 2026-06-07T19:31:15+00:00

I am working on an existing Java EE based application. This features the following

  • 0

I am working on an existing Java EE based application. This features the following method for connecting to a database :

public static java.sql.Connection connectionToDataBase(String jndiName,boolean flag)throws Exception 
{
DataSource ds =(javax.sql.DataSource) initCtx.lookup(jndiName);
return ds.getConnection();
    } catch (NamingException ne) {
            throw ne;
        } finally {
            try {
                if (initCtx != null)
                    initCtx.close();
            } catch (NamingException ne) {

                throw ne;
            }
        }
}

My question is whether using a static method for connecting to a database is correct?

  • 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-07T19:31:17+00:00Added an answer on June 7, 2026 at 7:31 pm

    Why have you defined the function as static?

    It is not incorrect nor is there any convention that would prohibit you in calling a static method from a non-static one. By definition, a non-static method belongs to an instance of the class, whereas a static method belongs to the class itself.

    Having a static method simply means you don’t need an instance of the class to connect to the DB.

    To answer your question, you probably want to consider what the class encapsulates. Do you only want an instance of the class to be able to connect to the DB? Or would you like to be able to connect to the DB without a reference to an instance of the class?

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

Sidebar

Related Questions

I'm working on an existing Java web application (HTML/CSS/JS/JSP/Servlets and Java classes in this
I am working on a existing Java EE based application . Somehow the functionality
I have an existing and working java web application. I want to make this
I'm working on a project to convert an existing Java web application to use
I am working on porting an existing VB.Net application to Java, and could not
I'm working with an existing database that has first name and last name seperated
I am working to configure Spring-Security with an existing application, for just basic security
I'm working on an existing Java EE project with various maven modules that are
I am working on a web application with an existing code base that has
I am working on a practical scenario related with Java;a socket program. The existing

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.