358 lines
7.0 KiB
Java
358 lines
7.0 KiB
Java
|
import java.sql.*;
|
|||
|
import java.util.Vector;
|
|||
|
|
|||
|
enum OperationBDD
|
|||
|
{
|
|||
|
OP_BDD_SELECTION,
|
|||
|
OP_BDD_CREER_TABLE,
|
|||
|
OP_BDD_SUPPR_TABLE
|
|||
|
}
|
|||
|
|
|||
|
public class GestionConnexion
|
|||
|
{
|
|||
|
|
|||
|
private ParametresConnexion paramsConn;
|
|||
|
|
|||
|
private Connection connection = null;
|
|||
|
private Statement statement = null;
|
|||
|
|
|||
|
private String requeteSQL = "";
|
|||
|
private ResultSet resultSet = null;
|
|||
|
|
|||
|
private Vector<String> vs_erreurs = null;
|
|||
|
private Vector<String> vs_anomalies = null;
|
|||
|
|
|||
|
|
|||
|
public GestionConnexion()
|
|||
|
throws ClassNotFoundException, Exception
|
|||
|
{
|
|||
|
|
|||
|
this.statement = null;
|
|||
|
this.resultSet = null;
|
|||
|
this.requeteSQL = "";
|
|||
|
|
|||
|
this.paramsConn = new ParametresConnexion();
|
|||
|
|
|||
|
this.vs_erreurs = new Vector<String>();
|
|||
|
this.vs_anomalies = new Vector<String>();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
Class.forName(ParametresConnexion.CLASSE).newInstance();
|
|||
|
}
|
|||
|
catch (ClassNotFoundException ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public void ouvrir(ParametresConnexion params)
|
|||
|
{
|
|||
|
if (params == null)
|
|||
|
throw new NullPointerException();
|
|||
|
|
|||
|
this.paramsConn.CopierDepuis(params);
|
|||
|
|
|||
|
this.ouvrir();
|
|||
|
}
|
|||
|
|
|||
|
public void ouvrir()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
this.fermerConnexion();
|
|||
|
|
|||
|
this.connection = DriverManager.getConnection(this.paramsConn.getUrl(),
|
|||
|
this.paramsConn.Utilisateur, this.paramsConn.Pwd);
|
|||
|
|
|||
|
this.traiterWarning(this.connection.getWarnings());
|
|||
|
}
|
|||
|
catch (SQLException ex)
|
|||
|
{
|
|||
|
this.traiterSQLException(ex);
|
|||
|
|
|||
|
this.fermerConnexion();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public boolean estOuverte()
|
|||
|
{
|
|||
|
boolean bOuverte = false;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (this.connection != null)
|
|||
|
bOuverte = !this.connection.isClosed();
|
|||
|
}
|
|||
|
catch (SQLException ex)
|
|||
|
{
|
|||
|
this.traiterSQLException(ex);
|
|||
|
}
|
|||
|
|
|||
|
return bOuverte;
|
|||
|
}
|
|||
|
|
|||
|
public void fermerRessourcesRequete()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (this.resultSet != null)
|
|||
|
{
|
|||
|
this.resultSet.close();
|
|||
|
this.resultSet = null;
|
|||
|
}
|
|||
|
|
|||
|
if (this.statement != null)
|
|||
|
{
|
|||
|
this.statement.close();
|
|||
|
this.statement = null;
|
|||
|
}
|
|||
|
|
|||
|
this.requeteSQL = "";
|
|||
|
}
|
|||
|
catch (SQLException ex)
|
|||
|
{
|
|||
|
this.traiterSQLException(ex);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void fermerConnexion()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
this.fermerRessourcesRequete();
|
|||
|
|
|||
|
if (this.connection != null)
|
|||
|
{
|
|||
|
this.connection.close();
|
|||
|
this.connection = null;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (SQLException ex)
|
|||
|
{
|
|||
|
this.traiterSQLException(ex);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public boolean lancerRequeteSelection(String query)
|
|||
|
{
|
|||
|
boolean res = true;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (this.connection != null)
|
|||
|
{
|
|||
|
this.fermerRessourcesRequete();
|
|||
|
|
|||
|
this.statement = this.connection.createStatement();
|
|||
|
this.traiterWarning(this.statement.getWarnings());
|
|||
|
|
|||
|
this.resultSet = this.statement.executeQuery(query);
|
|||
|
this.traiterWarning(this.resultSet.getWarnings());
|
|||
|
|
|||
|
this.requeteSQL = query;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (SQLException ex)
|
|||
|
{
|
|||
|
this.traiterSQLException(ex);
|
|||
|
|
|||
|
this.fermerRessourcesRequete();
|
|||
|
|
|||
|
res = false;
|
|||
|
}
|
|||
|
|
|||
|
return res;
|
|||
|
}
|
|||
|
|
|||
|
public int lancerRequeteModifBDD(String query)
|
|||
|
{
|
|||
|
int res = 0;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (this.connection != null)
|
|||
|
{
|
|||
|
this.fermerRessourcesRequete();
|
|||
|
|
|||
|
this.statement = this.connection.createStatement();
|
|||
|
|
|||
|
res = this.statement.executeUpdate(query);
|
|||
|
this.traiterWarning(this.statement.getWarnings());
|
|||
|
|
|||
|
this.requeteSQL = query;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (SQLException ex)
|
|||
|
{
|
|||
|
this.traiterSQLException(ex);
|
|||
|
|
|||
|
this.fermerRessourcesRequete();
|
|||
|
|
|||
|
res = -1;
|
|||
|
}
|
|||
|
|
|||
|
return res;
|
|||
|
}
|
|||
|
|
|||
|
public ParametresConnexion getParametresConnexion()
|
|||
|
{
|
|||
|
return new ParametresConnexion(this.paramsConn);
|
|||
|
}
|
|||
|
|
|||
|
public String getRequeteSQL()
|
|||
|
{
|
|||
|
return this.requeteSQL;
|
|||
|
}
|
|||
|
|
|||
|
public Vector<String> getInfosConnexion()
|
|||
|
{
|
|||
|
Vector<String> vs_infosConn = new Vector<String>();
|
|||
|
DatabaseMetaData dmd;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (this.estOuverte())
|
|||
|
{
|
|||
|
vs_infosConn.add("Etat de la connexion : ouverte");
|
|||
|
vs_infosConn.add("------------------------------");
|
|||
|
|
|||
|
dmd = this.connection.getMetaData();
|
|||
|
|
|||
|
vs_infosConn.add("Connexion : " + dmd.getURL());
|
|||
|
vs_infosConn.add("Driver : " + dmd.getDriverName());
|
|||
|
vs_infosConn.add("Version : " + dmd.getDriverVersion());
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
vs_infosConn.add("Etat de la connexion : ferm<72>e");
|
|||
|
vs_infosConn.add("------------------------------");
|
|||
|
|
|||
|
vs_infosConn.add("Connexion : " + this.paramsConn.getUrl());
|
|||
|
vs_infosConn.add("Utilisateur : " + this.paramsConn.Utilisateur);
|
|||
|
vs_infosConn.add("Mot de passe : " + this.paramsConn.Pwd.length() + " caract<63>re(s)");
|
|||
|
}
|
|||
|
}
|
|||
|
catch (SQLException ex)
|
|||
|
{
|
|||
|
this.traiterSQLException(ex);
|
|||
|
}
|
|||
|
|
|||
|
return vs_infosConn;
|
|||
|
}
|
|||
|
|
|||
|
public Vector<String> getNomsColonnes()
|
|||
|
{
|
|||
|
Vector<String> vs_cols = new Vector<String>();
|
|||
|
ResultSetMetaData rsmd;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (this.resultSet != null)
|
|||
|
{
|
|||
|
rsmd = this.resultSet.getMetaData();
|
|||
|
int nbrCols = rsmd.getColumnCount();
|
|||
|
|
|||
|
for (int idCol = 1; idCol <= nbrCols; idCol++)
|
|||
|
if (rsmd.isSearchable(idCol))
|
|||
|
vs_cols.add(rsmd.getColumnName(idCol));
|
|||
|
}
|
|||
|
else if (this.statement == null)
|
|||
|
this.vs_anomalies.add("Vous n'avez pas encore lanc<6E> la requ<71>te!!");
|
|||
|
}
|
|||
|
catch (SQLException ex)
|
|||
|
{
|
|||
|
this.traiterSQLException(ex);
|
|||
|
}
|
|||
|
|
|||
|
return vs_cols;
|
|||
|
}
|
|||
|
|
|||
|
public Vector<Vector<Object>> getDataVector()
|
|||
|
{
|
|||
|
Vector<Vector<Object>> v_datas = new Vector<Vector<Object>>();
|
|||
|
ResultSetMetaData rsmd;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
rsmd = this.resultSet.getMetaData();
|
|||
|
if (rsmd.getColumnCount() > 0)
|
|||
|
{
|
|||
|
while (this.resultSet.next())
|
|||
|
{
|
|||
|
Vector<Object> vo_ligne = new Vector<Object>();
|
|||
|
for (int idCol = 1; idCol <= rsmd.getColumnCount(); idCol++)
|
|||
|
vo_ligne.add(this.resultSet.getObject(idCol));
|
|||
|
v_datas.add(vo_ligne);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (v_datas.isEmpty())
|
|||
|
this.vs_anomalies.add("Aucune donn<6E>e trouv<75>e!!");
|
|||
|
}
|
|||
|
catch (SQLException ex)
|
|||
|
{
|
|||
|
this.traiterSQLException(ex);
|
|||
|
}
|
|||
|
|
|||
|
return v_datas;
|
|||
|
}
|
|||
|
|
|||
|
public void traiterSQLException(SQLException ex)
|
|||
|
{
|
|||
|
String str_ex;
|
|||
|
|
|||
|
str_ex = "********* SQLException *********\n";
|
|||
|
while (ex != null)
|
|||
|
{
|
|||
|
str_ex = "SQLState : " + ex.getSQLState() + "\n";
|
|||
|
str_ex += "Message : " + ex.getMessage() + "\n";
|
|||
|
str_ex += "------------------------------\n";
|
|||
|
this.vs_erreurs.add(str_ex);
|
|||
|
|
|||
|
ex = ex.getNextException();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void traiterWarning(SQLWarning warn)
|
|||
|
{
|
|||
|
String str_an;
|
|||
|
|
|||
|
str_an = "********* SQLWarning *********\n";
|
|||
|
while (warn != null)
|
|||
|
{
|
|||
|
str_an = "SQLState : " + warn.getSQLState() + "\n";
|
|||
|
str_an += "Message : " + warn.getMessage() + "\n";
|
|||
|
str_an += "------------------------------\n";
|
|||
|
this.vs_anomalies.add(str_an);
|
|||
|
|
|||
|
warn = warn.getNextWarning();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public Vector<String> getErreurs()
|
|||
|
{
|
|||
|
return new Vector<String>(this.vs_erreurs);
|
|||
|
}
|
|||
|
|
|||
|
public Vector<String> getAnomalies()
|
|||
|
{
|
|||
|
return new Vector<String>(this.vs_anomalies);
|
|||
|
}
|
|||
|
|
|||
|
public void effaceErreurs()
|
|||
|
{
|
|||
|
this.vs_erreurs.clear();
|
|||
|
}
|
|||
|
|
|||
|
public void effaceAnomalies()
|
|||
|
{
|
|||
|
this.vs_anomalies.clear();
|
|||
|
}
|
|||
|
}
|