Created
October 12, 2018 10:40
-
-
Save oflarcade/f4fcb5d625c04720f3347c2573fd9757 to your computer and use it in GitHub Desktop.
SingleTon Database Connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package utils; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
/** | |
* | |
* @author oflcad | |
*/ | |
public class DataSource { | |
private static DataSource instance; | |
private Connection connection; | |
private String urlString = "jdbc:mysql://localhost:3306/esprit"; | |
private String username = "root"; | |
private String password = "mysql"; | |
private DataSource() throws SQLException { | |
try { | |
//Class.forName("com.mysql.jdbc.Driver"); | |
this.connection = DriverManager.getConnection(urlString, username, password); | |
} catch (SQLException ex) { | |
System.out.println("Database Connection Creation Failed : " + ex.getMessage()); | |
} | |
} | |
public Connection getConnection() { | |
return connection; | |
} | |
public static DataSource getInstance() throws SQLException { | |
if (instance == null) { | |
instance = new DataSource(); | |
} else if (instance.getConnection().isClosed()) { | |
instance = new DataSource(); | |
} | |
return instance; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment