cours0708/P51/connexionBDD/Connexion.java~

141 lines
2.7 KiB
Java

package fr.blankoworld.connexionBDD;
public class Connexion {
// Donnees privees
private String serveur;
private String port;
private String bdd;
private String id;
private String mdp;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
// Constructeur
public Connexion(String server, String port, String database, String login, String password)
{
this.serveur = server;
this.port = port;
this.bdd = database;
this.id = login;
this.mdp = password;
}
// Ascesseurs, etc ...
public String getServer(){
return this.serveur;
}
public void setServer(String chaine){
this.serveur = chaine;
}
public String getPort(){
return this.port;
}
public void setPort(String chaine){
this.port = chaine;
}
public String getDatabase(){
return this.bdd;
}
public void setDatabase(String chaine){
this.bdd = chaine;
}
public String getLogin(){
return this.id;
}
public void setLogin(String chaine){
this.id = chaine;
}
public String getPassword(){
return this.mdp;
}
public void setPassword(String chaine){
this.mdp = chaine;
}
// Verification presence du pilote
public String[] driverPresent(){
String[] tableau = new String[2];
try{
// On verifie que le pilote Oracle est disponible
Class.forName( "oracle.jdbc.driver.OracleDriver" );
tableau[0] = "0";
tableau[1] = "Pilote Oracle trouve";
}
catch(ClassNotFoundException ex){
tableau[0] = "1";
tableau[1] = "Pilote pas trouve";
}
return tableau;
}
// Connexion a la base
// Doit pouvoir retourner un tableau avec une chaine expliquant si oui ou non nous sommes connectes, et une autre donnant l'erreur
public String[] connect(){
String[] tableau = new String[2];
tableau[0] = "0";
tableau[1] = "Je suis sense me connecter !";
// S'il l'est, nous continuons en preparant notre creme
Connection connection = null;
String url = "jdbc:oracle:thin:@" + this.serveur + ":" + this.port + ":" + this.bdd;
String identifiant = this.id;
String mdp = this.mdp;
// puis nous tentons d'appliquer la creme
try {
connection = DriverManager.getConnection(url, identifiant, mdp);
tableau[1] = "Acces a la base: Accepte.";
}
catch(SQLException sqle) {
tableau[1] = "Accès à la base: Refuse.";
// TODO: handle exception
} finally {
if(connection!=null){
try{connection.close();
tableau[1] = "Connexion fermee.";
} catch(Exception e){
e.printStackTrace();
}
}
}
}
return tableau;
}
// Deconnexion a la base de donnees
public String[] disconnect(){
String[] tableau = new String[2];
tableau[0] = "0";
tableau[1] = "Je suis sense me deconnecter !";
return tableau;
}
}