Ajouts des Hexagon + De la base de données + Ajout de variable d'environnement pour la sécurité
This commit is contained in:
52
TestV2/src/fr/monkhanny/dorfromantik/utils/Database.java
Normal file
52
TestV2/src/fr/monkhanny/dorfromantik/utils/Database.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class Database {
|
||||
// Chargement des variables d'environnement
|
||||
private static final String URL = Environnement.getEnv("DATABASE_URL_IUT");
|
||||
private static final String LOGIN = Environnement.getEnv("DATABASE_LOGIN_IUT");
|
||||
private static final String PASSWORD = Environnement.getEnv("DATABASE_PASSWORD_IUT");
|
||||
|
||||
// Variable de passerelle entre le programme et la base de données
|
||||
private Connection database;
|
||||
|
||||
/**
|
||||
* Ouvre la connexion avec la base de données
|
||||
*/
|
||||
public Database() throws SQLException {
|
||||
try {
|
||||
// Chargement du driver MariaDB
|
||||
Class.forName("org.mariadb.jdbc.Driver");
|
||||
|
||||
try {
|
||||
// Connexion à la base de données
|
||||
this.database = DriverManager.getConnection(URL, LOGIN, PASSWORD);
|
||||
}catch (SQLException e) {
|
||||
// Gestion de l'erreur de connexion
|
||||
throw new SQLException("Échec de la connexion à la base de données: " + e.getMessage(), e);
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
// Si le driver n'est pas trouvé
|
||||
throw new SQLException("Driver MariaDB introuvable dans le classpath", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Connection getDatabase() {
|
||||
return this.database;
|
||||
}
|
||||
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
if (this.database != null && !this.database.isClosed()) {
|
||||
this.database.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lors de la fermeture de la base de données : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user