38 lines
1.1 KiB
Java
38 lines
1.1 KiB
Java
|
|
package fr.iutfbleau.papillon.model;
|
||
|
|
import org.mariadb.jdbc.MariaDbDataSource;
|
||
|
|
|
||
|
|
import java.sql.Connection;
|
||
|
|
import java.sql.SQLException;
|
||
|
|
|
||
|
|
public class BaseDeDonnees {
|
||
|
|
|
||
|
|
private static Connection connexion;
|
||
|
|
|
||
|
|
public static Connection getConnexion() {
|
||
|
|
if (connexion == null) {
|
||
|
|
try {
|
||
|
|
MariaDbDataSource dataSource = new MariaDbDataSource();
|
||
|
|
dataSource.setUrl("jdbc:mariadb://localhost:3307/papillon");
|
||
|
|
dataSource.setUser("root");
|
||
|
|
dataSource.setPassword("mdp");
|
||
|
|
|
||
|
|
connexion = dataSource.getConnection();
|
||
|
|
System.out.println(" Connexion réussie !");
|
||
|
|
} catch (SQLException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return connexion;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void fermer() {
|
||
|
|
try {
|
||
|
|
if (connexion != null && !connexion.isClosed()) {
|
||
|
|
connexion.close();
|
||
|
|
System.out.println(" Connexion fermée.");
|
||
|
|
}
|
||
|
|
} catch (SQLException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|