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 52 53 54 55 56 57 58 59
|
package piggybank;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.controlsfx.control.action.Action;
import org.controlsfx.dialog.Dialogs;
/**
*
* @author Pascal
*/
public class DBConnect {
private static Connection conn;
private static String url = "XXXXX";
private static String user = "XXXXX";
private static String pass = "XXXXX";
public static Connection connect() throws SQLException{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(ClassNotFoundException cnfe){
System.err.println("Error cnfe: "+cnfe.getMessage());
Action response = Dialogs.create()
.title("Erreur")
.masthead("Exception rencontrée")
.message("Error ie: "+cnfe.getMessage())
.showException(cnfe);
}catch(InstantiationException ie){
System.err.println("Error ie: "+ie.getMessage());
Action response = Dialogs.create()
.title("Erreur")
.masthead("Exception rencontrée")
.message("Error ie: "+ie.getMessage())
.showException(ie);
}catch(IllegalAccessException iae){
System.err.println("Error iae: "+iae.getMessage());
Action response = Dialogs.create()
.title("Erreur")
.masthead("Exception rencontrée")
.message("Error iae: "+iae.getMessage())
.showException(iae);
}
conn = DriverManager.getConnection(url,user,pass);
System.out.println("SQL - Nouvelle connexion en cours");
return conn;
}
public static Connection getConnection() throws SQLException, ClassNotFoundException{
if(conn !=null && !conn.isClosed()) {
System.out.println("SQL - Connexion en cours toujours active");
return conn;
}
connect();
return conn;
} |
Partager