183 lines
3.8 KiB
Java
183 lines
3.8 KiB
Java
package fr.blankoworld.connexionBDD;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.DatabaseMetaData;
|
|
import java.sql.DriverManager;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
|
|
/**
|
|
* @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;
|
|
|
|
private ResultSet result;
|
|
|
|
// 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";
|
|
|
|
tableau[1] += "Acces: " + url + "\n";
|
|
|
|
DatabaseMetaData metaData;
|
|
|
|
metaData = connection.getMetaData();
|
|
tableau[1] += "Driver: " + metaData.getDriverName() + "\n";
|
|
tableau[1] += "Version: " + metaData.getDriverVersion() + "\n";
|
|
|
|
}
|
|
catch(SQLException sqle) {
|
|
tableau[0] = "1";
|
|
tableau[1] = "Acces a la base: Refuse.\n" + sqle.getMessage();
|
|
}
|
|
|
|
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 fermee.";
|
|
} catch(Exception e){
|
|
tableau[0] = "1";
|
|
tableau[1] = e.getMessage();
|
|
}
|
|
}
|
|
|
|
return tableau;
|
|
}
|
|
|
|
public String[] sendQuery(String query){
|
|
String[] tableau = new String[2];
|
|
|
|
try {
|
|
// Creation d'une requete
|
|
Statement requete = connection.createStatement();
|
|
// Recuperation du resultat de la requete
|
|
this.result = requete.executeQuery(query); // executeQuery ne retourne jamais null !
|
|
|
|
// Requete reussie
|
|
tableau[0] = "0";
|
|
tableau[1] = "Requete: Envoyee et recue.";
|
|
|
|
} catch (Exception ex){
|
|
tableau[0] = "1";
|
|
tableau[1] = "Requete: Non reussie.";
|
|
}
|
|
|
|
return tableau;
|
|
}
|
|
|
|
public ResultSet getQueryResult(){
|
|
return this.result;
|
|
}
|
|
|
|
}
|