Skip to content
Snippets Groups Projects
Commit 6a8a6803 authored by Kwuray's avatar Kwuray
Browse files

auto init connection and handle no user db

parent cab1f375
No related branches found
No related tags found
No related merge requests found
package fr.univlille.iutinfo.sql;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
......@@ -30,8 +31,7 @@ public class DBConnection<E>
* @return un résultat de type E.
* @throws SQLException en cas d'erreur SQL, et ferme la connexion de manière automatique.
*/
public E executeRequest() throws SQLException
{
public E executeRequest() throws SQLException, IOException, ClassNotFoundException {
try (Connection c = DBConnector.getConnection())
{
return this.request.execute(c);
......@@ -48,8 +48,7 @@ public class DBConnection<E>
* @return un résultat de type E.
* @throws SQLException en cas d'erreur SQL, ferme la connexion et réalise un rollback de manière automatique.
*/
public E executeTransaction(int isolationLevel) throws SQLException
{
public E executeTransaction(int isolationLevel) throws SQLException, IOException, ClassNotFoundException {
Connection c = null;
try
{
......
......@@ -52,17 +52,28 @@ public class DBConnector
initOk = true;
}
private static void initialiser() throws IOException, ClassNotFoundException {
if (System.getProperties().containsKey("db_config"))
{
initialiser(System.getProperty("db_config"));
}
}
/**
* Cette méthode permet d'initier une connexion à la base de donnée.
* @return un objet {@code Connection}, qui représente la connexion à la base de données.
* @throws SQLException en cas d'erreur SQL.
*/
public static Connection getConnection() throws SQLException
public static Connection getConnection() throws SQLException, IOException, ClassNotFoundException
{
if (!initOk)
{
throw new SQLException("Vous n'êtes pas connecté à la base de données.");
initialiser();
}
if (login != null)
{
return DriverManager.getConnection(DBConnector.url, DBConnector.login, DBConnector.password);
}
return DriverManager.getConnection(DBConnector.url);
}
}
package fr.univlille.iutinfo.sql;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
......@@ -21,8 +22,7 @@ public abstract class Utils
* @return Un String représentant le résultat.
* @throws SQLException en cas d'erreur SQL.
*/
public static String selectUnique(String sqlRequest) throws SQLException
{
public static String selectUnique(String sqlRequest) throws SQLException, IOException, ClassNotFoundException {
DBConnection<String> DBConnection = new DBConnection<String>((stmt) ->
{
Statement statement = stmt.createStatement();
......@@ -41,8 +41,7 @@ public abstract class Utils
* @param sqlRequest, la requête à exécuter.
* @throws SQLException en cas d'erreur SQL.
*/
public static Void request(String sqlRequest) throws SQLException
{
public static Void request(String sqlRequest) throws SQLException, IOException, ClassNotFoundException {
DBConnection<Void> DBConnection = new DBConnection<Void>((stmt) ->
{
Statement statement = stmt.createStatement();
......@@ -58,8 +57,7 @@ public abstract class Utils
* @return un tableau associatif contenant le résultat de la requête.
* @throws SQLException en cas d'erreur SQL.
*/
public static List<Map<String, String>> selectMultiple(String sqlRequest) throws SQLException
{
public static List<Map<String, String>> selectMultiple(String sqlRequest) throws SQLException, IOException, ClassNotFoundException {
DBConnection<List<Map<String, String>>> DBConnection = new DBConnection<List<Map<String, String>>>((stmt) ->
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment