Created
May 12, 2020 12:34
-
-
Save bastienapp/42a25075c5de3107947d6befed4ae497 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.wildcodeschool.wildmail; | |
import java.sql.*; | |
public class JDBCSingleton { | |
public static final String DATABASE_URL = "jdbc:mysql://localhost:3306/wild_mail?serverTimezone=Europe/Paris"; | |
public static final String MYSQL_USER = "wildadmin"; | |
public static final String MYSQL_PASSWORD = "w1ld$fun"; | |
private JDBCSingleton() {} | |
private static JDBCSingleton instance = null; | |
private Connection connection = null; | |
public static JDBCSingleton getInstance() { | |
if (instance == null) { | |
instance = new JDBCSingleton(); | |
} | |
return instance; | |
} | |
public Connection getConnection() throws SQLException { | |
if (connection == null || connection.isClosed()) { | |
connection = DriverManager.getConnection(DATABASE_URL, MYSQL_USER, MYSQL_PASSWORD); | |
} | |
return connection; | |
} | |
public ResultSet executeQuery(String request) throws SQLException { | |
PreparedStatement statement = connection.prepareStatement(request); | |
ResultSet resultSet = statement.executeQuery(); | |
return resultSet; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment