Last active
December 14, 2015 06:39
-
-
Save alexbonhomme/5044265 to your computer and use it in GitHub Desktop.
Classe de gestion de base de données.
Initialement conçue pour SQLite, elle peut potentiellement être étendu pour d'autre BDD.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Surcouche chargée de la gestion de l'accès à une base de donnée SQLite | |
* !!! Attention !!! | |
* Cette classe nécessite le driver JDBC pour fonctionner | |
*/ | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
/** | |
* @author Alexandre Bonhomme | |
* | |
*/ | |
public class DataBase { | |
private String dbname = null; | |
private Connection connection = null; | |
private Statement statement = null; | |
/** | |
* | |
* @param dbname | |
*/ | |
public DataBase(String dbname) { | |
this.dbname = dbname; | |
// Chargement du driver sqlite-JDBC | |
try { | |
Class.forName("org.sqlite.JDBC"); | |
} catch (ClassNotFoundException e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
/** | |
* | |
* @return True si la connection à pu être établie, False sinon | |
*/ | |
public boolean connect() { | |
try { | |
connection = DriverManager.getConnection("jdbc:sqlite:" + dbname); | |
statement = connection.createStatement(); | |
statement.setQueryTimeout(30); // set timeout to 30 sec. | |
return true; | |
} catch (SQLException e) { | |
System.err.println(e.getMessage()); | |
return false; | |
} | |
} | |
/** | |
* | |
* @param queryTimeout | |
* @return True si la connection à pu être établie, False sinon | |
*/ | |
public boolean connect(int queryTimeout) { | |
try { | |
connection = DriverManager.getConnection("jdbc:sqlite:" + dbname); | |
statement = connection.createStatement(); | |
statement.setQueryTimeout(queryTimeout); | |
return true; | |
} catch (SQLException e) { | |
System.err.println(e.getMessage()); | |
return false; | |
} | |
} | |
/** | |
* | |
* @return True si la connection à pu être fermée correctement, False sinon | |
*/ | |
public boolean close() { | |
try { | |
if(connection != null) | |
connection.close(); | |
//TODO doit-on vraiment retourner Vrai si la connection est == null ? | |
return true; | |
} catch (SQLException e) { | |
System.err.println(e.getMessage()); | |
return false; | |
} | |
} | |
/** | |
* | |
* @param sql Requete SQL | |
* @throws SQLException | |
*/ | |
public void exec(String sql) throws SQLException { | |
statement.executeUpdate(sql); | |
} | |
/** | |
* | |
* @param sql Requete SQL | |
* @return Un objet contenant les résultat de la requete | |
* @throws SQLException | |
*/ | |
public ResultSet query(String sql) throws SQLException { | |
return statement.executeQuery(sql); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment