Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.alwan.hypnosiaplugin.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;
- }
- public Connection getConnection() throws SQLException {
- return connectionPool.getConnection();
- }
- public void createTables() {
- update("CREATE TABLE IF NOT EXISTS accounts (" +
- "`#` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
- "playername VARCHAR(255), " +
- "grade VARCHAR(255), " +
- "grade_end BIGINT, " +
- "coins BIGINT)");
- }
- 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