java : Using Static Method for obtaining Database Connection

Viewed 2783

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?

2 Answers
Related