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]
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]
Comment