Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.mrgeekk.core.database;
- import org.apache.commons.dbcp2.BasicDataSource;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.function.Consumer;
- import java.util.function.Function;
- public class MySQL {
- private BasicDataSource connectionPool;
- public MySQL(BasicDataSource connectionPool){
- this.connectionPool = connectionPool;
- }
- private Connection getConnection() throws SQLException{
- return connectionPool.getConnection();
- }
- public void createTables(){
- update("CREATE TABLE IF NOT EXISTS reports (" +
- "`#` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
- "pseudo VARCHAR(255), " +
- "uuid VARCHAR(255), " +
- "date VARCHAR(255), " +
- "auteur VARCHAR(255), " +
- "raison VARCHAR(255))");
- }
- public void update(String qry){
- try (Connection c = getConnection();
- PreparedStatement s = c.prepareStatement(qry)) {
- s.executeUpdate();
- } catch(Exception e){
- e.printStackTrace();
- }
- }
- public Object query(String qry, Function<ResultSet, Object> consumer){
- try (Connection c = getConnection();
- PreparedStatement s = c.prepareStatement(qry);
- ResultSet rs = s.executeQuery()) {
- return consumer.apply(rs);
- } catch(SQLException e){
- throw new IllegalStateException(e.getMessage());
- }
- }
- public void query(String qry, Consumer<ResultSet> consumer){
- try (Connection c = getConnection();
- PreparedStatement s = c.prepareStatement(qry);
- ResultSet rs = s.executeQuery()) {
- consumer.accept(rs);
- } catch(SQLException e){
- throw new IllegalStateException(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement