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
| public class ExempleBDD {
private String driver="org.apache.derby.jdbc.ClientDriver";
private String bdd="jdbc:derby://localhost:1527/Mots";
private String login="nono";
private String pass="nono";
private Connection co=null;
public ExempleBDD(){
try{
//Charge le pilote
this.loadDriver();
//Crée la connection
co=this.connectBDD();
}
catch(DriverBugException e){
System.out.println(e);
}
catch(ConnectionBugException e){
System.out.println(e);
}
}
public void loadDriver() throws DriverBugException{
try{
Class.forName(driver);
System.out.println("Chargement du driver...");
}
catch (Exception e){
throw new DriverBugException("Pilote de BDD manquant!");
}
}
public Connection connectBDD() throws ConnectionBugException{
Connection cotemp=null;
try{
cotemp=DriverManager.getConnection(bdd,login,pass);
System.out.println("Connection à la BDD");
}
catch (Exception e){
throw new ConnectionBugException("Problème de connection à la BDD");
}
return cotemp;
}
} |
Partager