Skip to content

Instantly share code, notes, and snippets.

@jasoet
Created October 6, 2012 04:16
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jasoet/3843797 to your computer and use it in GitHub Desktop.
Save jasoet/3843797 to your computer and use it in GitHub Desktop.
Database Connection Singleton
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.secondstack.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author Deny Prasetyo
*/
public class DatabaseConnection {
private static DatabaseConnection instance;
private Connection connection;
private String url = "jdbc:postgresql://localhost:5432/jdbc";
private String username = "root";
private String password = "localhost";
private DatabaseConnection() throws SQLException {
try {
Class.forName("org.postgresql.Driver");
this.connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException ex) {
System.out.println("Database Connection Creation Failed : " + ex.getMessage());
}
}
public Connection getConnection() {
return connection;
}
public static DatabaseConnection getInstance() throws SQLException {
if (instance == null) {
instance = new DatabaseConnection();
} else if (instance.getConnection().isClosed()) {
instance = new DatabaseConnection();
}
return instance;
}
}
@cdne
Copy link

cdne commented May 2, 2020

In another class:
DatabaseConnection databaseConnection = DatabaseConnection.getInstance();
databaseConnection.getConnection();

or

Connection databaseConnection = DatabaseConnection.getInstance().getConnection();

@vishnu-developer
Copy link

vishnu-developer commented May 2, 2020 via email

@Rashu1991
Copy link

Thanks

@sLasHee
Copy link

sLasHee commented May 30, 2020

Why need use Class.forName ?

@vishnu-developer
Copy link

It is used to load the database specific Driver . Only then we can use JDBC operations.

@vishnu-developer
Copy link

In the above example it loads postgresql Driver.

@sLasHee
Copy link

sLasHee commented May 31, 2020

But I still have to load the driver through the Maven dependencies.

@sLasHee
Copy link

sLasHee commented May 31, 2020

Using the Class.forName, the postgresql driver will not appear in the project structure.

@shalguev
Copy link

And how it supposed to work once connection is closed? I don't see how to reopen this connection without restarting the app.

@TechUn-GT
Copy link

Great!!!

@DejanPerovic
Copy link

DejanPerovic commented Aug 10, 2021

thanks!
But you need a private constructor.

private DatabaseConnection() { }

@jasoet
Copy link
Author

jasoet commented Sep 22, 2021

Calling getInstance will create a new connection if the existing connection is closed.

@vinvin04
Copy link

vinvin04 commented May 8, 2022

is there any way not to hardcode URL, username, and password?

@elon-fask
Copy link

Thanks , It really helped me.

@technical-mindset
Copy link

you can create an other method public DatabaseConnection createCon(String url, uName, pswrd){}

@technical-mindset
Copy link

is there any way not to hardcode URL, username, and password?

So now which method should I call to get the data base connection ?

DBConnection db = DBConnection.getInstance();
Connection conn = db.getConnection();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment