cours0708/P51/connexionBDD/Connexion.java

161 lines
3.3 KiB
Java
Raw Permalink Blame History

package fr.blankoworld.connexionBDD;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author 3dossmanno
*/
public class Connexion {
// Donnees privees
private String serveur;
private String port;
private String bdd;
private String id;
private String mdp;
private Connection connection;
/**
* @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;
}
/**
* @return the port
* @uml.property name="port"
*/
public String getPort(){
return this.port;
}
/**
* @param port the port to set
* @uml.property name="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
// Ceci permet d'avoir des messages en francais ...
public String[] connect(){
String[] tableau = new String[2];
// S'il l'est, nous continuons en preparant notre creme
connection = null;
String url = "jdbc:oracle:thin:@" + this.serveur + ":" + this.port + ":" + this.bdd;
String identifiant = this.id;
String mdp = this.mdp.toString();
// puis nous tentons d'appliquer la creme
try {
connection = DriverManager.getConnection(url, identifiant, mdp);
tableau[0] = "0";
tableau[1] = "Acces a la base: Accepte.\n";
DatabaseMetaData metaData;
metaData = connection.getMetaData();
System.out.println(metaData.getDriverName());
System.out.println(metaData.getDriverVersion());// getVersion
System.out.println(metaData.getConnection().toString());// getConnectionName ???
}
catch(SQLException sqle) {
tableau[0] = "1";
tableau[1] = "Acces a la base: Refuse.\n" + sqle.getMessage();
// TODO: handle exception
}
return tableau;
}
// Deconnexion a la base de donnees
public String[] disconnect(){
String[] tableau = new String[2];
if(connection != null){
try{
connection.close();
tableau[0] = "0";
tableau[1] = "Connexion ferm<72>e.";
} catch(Exception e){
tableau[0] = "1";
tableau[1] = e.getMessage();
}
}
return tableau;
}
}