1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
* Class ConnectDAO pour me connecter à ma DB.
*
* @author Fred
*/
public class ConnectDAO {
private final String urlDB;
private final String passwordDB;
private final String userDB;
private static Object INSTANCE;
//*******************Constructeur********************************************
public ConnectDAO() {
this.userDB = "xxxx";
this.passwordDB = "xxxx";
this.urlDB = "localhost:3306/vente_articles?useLegacyDatetimeCode=false&serverTimezone=Europe/Brussels";
}
/**
* getInstance, crée une instance unique de ConnectDAO et renvoie cet instance.
* @return
*/
public static ConnectDAO getInstance() {
if (ConnectDAO.INSTANCE == null) {
INSTANCE = new ConnectDAO();
}
return (ConnectDAO) INSTANCE;
}
public Connection connection() throws SQLException {
Connection con;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(("jdbc:mysql://") + this.urlDB, this.userDB, this.passwordDB);
return con;
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, "Erreur driver mysql: " + ex);
return null;
}
}
} |
Partager