Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.ulmc.UnleashedTokensAPI.util;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.UUID;
- import net.ulmc.UnleashedTokensAPI.Main;
- public class SQLUtil {
- private static Connection connection;
- public static void openConnection() {
- try {
- connection = DriverManager.getConnection("jdbc:mysql://" + Main.getPlugin().getConfig().getString("mysql.address") + ":" + Integer.toString(Main.getPlugin().getConfig().getInt("port")) + "/" + Main.getPlugin().getConfig().getString("mysql.database"), Main.getPlugin().getConfig().getString("mysql.username"), Main.getPlugin().getConfig().getString("mysql.password"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void closeConnection() {
- try {
- if (!connection.isClosed()) {
- connection.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void checkTables() {
- try {
- if (connection.isClosed()) {
- openConnection();
- }
- Statement s = connection.createStatement();
- s.execute("CREATE TABLE IF NOT EXISTS tokens (id VARCHAR(32) PRIMARY KEY, tokens INTEGER NOT NULL);");
- s.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void insertPlayer(UUID uuid) {
- try {
- if (connection.isClosed()) {
- openConnection();
- }
- Statement s = connection.createStatement();
- s.executeUpdate("INSERT INTO tokens (id,tokens) VALUES ('" + uuid.toString() + "', 0);");
- s.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static boolean containsPlayer(UUID uuid) {
- try {
- if (connection.isClosed()) {
- openConnection();
- }
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM tokens WHERE uuid=?;");
- ps.setString(1, uuid.toString());
- ResultSet rs = ps.executeQuery();
- boolean cp = rs.next();
- ps.close();
- rs.close();
- return cp;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- public static int getTokens(UUID uuid) {
- try {
- if (connection.isClosed()) {
- openConnection();
- }
- int t;
- if (containsPlayer(uuid)) {
- Statement s = connection.createStatement();
- ResultSet rs = s.executeQuery("SELECT * FROM tokens WHERE id=" + uuid.toString());
- t = rs.getInt("tokens");
- rs.close();
- s.close();
- return t;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return -1;
- }
- public static void setTokens(UUID uuid, int tokens) {
- try {
- if (connection.isClosed()) {
- openConnection();
- }
- if (containsPlayer(uuid)) {
- Statement s = connection.createStatement();
- s.executeUpdate("UPDATE tokens SET tokens=" + tokens + " WHERE id='" + uuid.toString() + "';");
- s.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void addTokens(UUID uuid, int tokens) {
- try {
- if (connection.isClosed()) {
- openConnection();
- }
- if (containsPlayer(uuid)) {
- Statement s = connection.createStatement();
- s.executeUpdate("UPDATE tokens SET tokens=tokens+" + tokens + " WHERE id='" + uuid.toString() + "';");
- s.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void deductTokens(UUID uuid, int tokens) {
- try {
- if (connection.isClosed()) {
- openConnection();
- }
- if (containsPlayer(uuid)) {
- Statement s = connection.createStatement();
- s.executeUpdate("UPDATE tokens SET tokens=tokens-" + tokens + " WHERE id='" + uuid.toString() + "';");
- s.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement