Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.reventic.statsapi.mysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.concurrent.ExecutorService;
- import java.util.function.Consumer;
- import org.bukkit.Bukkit;
- import net.reventic.statsapi.main.StatsAPI;
- public class Settings {
- public static ExecutorService executor;
- public static Connection con;
- public static void connect() {
- try {
- if (!isConnected()) {
- con = DriverManager.getConnection("jdbc:mysql://" + StatsAPI.getHostname() + ":" + StatsAPI.getPort()
- + "/" + StatsAPI.getDB() + "?autoReconnect=true" + StatsAPI.getUsername() + StatsAPI.getPW());
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void disconnect() {
- if (isConnected()) {
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static boolean isConnected() {
- return con != null;
- }
- public void query(String qry, Consumer<ResultSet> consumer) {
- executor.execute(() -> {
- ResultSet result;
- try {
- result = prepare(qry).executeQuery();
- Bukkit.getScheduler().runTask(StatsAPI.getInstance(), () -> consumer.accept(result));
- } catch (SQLException e) {
- e.printStackTrace();
- }
- });
- }
- public static PreparedStatement prepare(String qry) {
- try {
- return con.prepareStatement(qry);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement