Best connection?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oaklander
    New Member
    • Aug 2007
    • 20

    #1

    Best connection?

    What is the best way to connect to a database in Java Class?
    I have used 3 different ways and would like to know what is the most efficient one.

    1- Put the connection as a data member:

    [code=java]
    public class DatabaseHelperC lass
    {
    private Connection connection = new DbConnectionMan ager().getConne ction();

    public void insertDb(DbBean db) throws SQLException {
    PreparedStateme nt stmt = connection.prep areStatement("i nsert ..");
    ...
    }

    public void updateDb(DbBean db) throws SQLException {
    PreparedStateme nt stmt = connection.prep areStatement("u pdate ..");
    ...
    }[/code]

    2 - Put connection in Constructor:

    [code=java]
    public class DatabaseHelperC lass
    {
    public DatabaseHelperC lass() {
    Connection connection = new DbConnectionMan ager().getConne ction();
    }

    public void insertDb(DbBean db) throws SQLException {
    PreparedStateme nt stmt = connection.prep areStatement("i nsert ..");
    ...
    }

    public void updateDb(DbBean db) throws SQLException {
    PreparedStateme nt stmt = connection.prep areStatement("u pdate ..");
    ...
    }[/code]

    3- Put connection in every method that needs a connection:

    [code=java]
    public class DatabaseHelperC lass
    {
    public void insertDb(DbBean db) throws SQLException {
    Connection connection = new DbConnectionMan ager().getConne ction();
    PreparedStateme nt stmt = connection.prep areStatement("i nsert ..");
    ...
    }

    public void updateDb(DbBean db) throws SQLException {
    Connection connection = new DbConnectionMan ager().getConne ction();
    PreparedStateme nt stmt = connection.prep areStatement("u pdate ..");
    ...
    }[/code]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by oaklander
    What is the best way to connect to a database in Java Class?
    I have used 3 different ways and would like to know what is the most efficient one.

    1- Put the connection as a data member:

    [code=java]
    public class DatabaseHelperC lass
    {
    private Connection connection = new DbConnectionMan ager().getConne ction();

    public void insertDb(DbBean db) throws SQLException {
    PreparedStateme nt stmt = connection.prep areStatement("i nsert ..");
    ...
    }

    public void updateDb(DbBean db) throws SQLException {
    PreparedStateme nt stmt = connection.prep areStatement("u pdate ..");
    ...
    }[/code]

    2 - Put connection in Constructor:

    [code=java]
    public class DatabaseHelperC lass
    {
    public DatabaseHelperC lass() {
    Connection connection = new DbConnectionMan ager().getConne ction();
    }

    public void insertDb(DbBean db) throws SQLException {
    PreparedStateme nt stmt = connection.prep areStatement("i nsert ..");
    ...
    }

    public void updateDb(DbBean db) throws SQLException {
    PreparedStateme nt stmt = connection.prep areStatement("u pdate ..");
    ...
    }[/code]

    3- Put connection in every method that needs a connection:

    [code=java]
    public class DatabaseHelperC lass
    {
    public void insertDb(DbBean db) throws SQLException {
    Connection connection = new DbConnectionMan ager().getConne ction();
    PreparedStateme nt stmt = connection.prep areStatement("i nsert ..");
    ...
    }

    public void updateDb(DbBean db) throws SQLException {
    Connection connection = new DbConnectionMan ager().getConne ction();
    PreparedStateme nt stmt = connection.prep areStatement("u pdate ..");
    ...
    }[/code]
    2.) Won't compile
    3.) Wastes resources by creating a connection everytime even when one is already available. Google connection pooling for more details.

    Comment

    Working...