Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.lyntcraftapi.mysql;
- import java.sql.*;
- import org.bukkit.entity.Player;
- public class LyntMaintenanceAPI {
- private String url_base, host, name, user, pass, table;
- private Connection connection;
- public LyntMaintenanceAPI(String url_base, String host, String name, String user, String pass, String table) {
- this.url_base = url_base;
- this.host = host;
- this.name = name;
- this.user = user;
- this.pass = pass;
- this.table = table;
- }
- public void connection() {
- if (!isConnected()) {
- try {
- connection = DriverManager.getConnection(url_base + host + "/" + name, user, pass);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public void deconnection() {
- if (isConnected()) {
- try {
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- private boolean isConnected() {
- try {
- if ((connection == null) || (connection.isClosed()) || (connection.isValid(5))) {
- return false;
- } else {
- return true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- private Connection getConnection() {
- return connection;
- }
- public void createAccount(Player p) {
- try {
- PreparedStatement sts = getConnection().prepareStatement("SELECT mn FROM " + table + " WHERE uuid = ?");
- sts.setString(1, p.getUniqueId().toString());
- ResultSet rs = sts.executeQuery();
- if (!rs.next()) {
- sts.close();
- PreparedStatement sts2 = getConnection()
- .prepareStatement("INSERT INTO " + table + " (uuid, mn) VALUES (?, ?)");
- sts2.setString(1, p.getUniqueId().toString());
- sts2.setInt(2, 0);
- sts2.executeUpdate();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void createMaintenance() {
- try {
- PreparedStatement sts = getConnection().prepareStatement("SELECT mn FROM " + table + " WHERE uuid = ?");
- sts.setString(1, "maintenance");
- ResultSet rs = sts.executeQuery();
- if (!rs.next()) {
- sts.close();
- PreparedStatement sts2 = getConnection()
- .prepareStatement("INSERT INTO " + table + " (uuid, mn) VALUES (?, ?)");
- sts2.setString(1, "maintenance");
- sts2.setBoolean(2, false);;
- sts2.executeUpdate();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void setMaintenanceOn() {
- try {
- PreparedStatement sts = getConnection()
- .prepareStatement("UPDATE " + table + " SET mn = ? WHERE uuid = ?");
- sts.setBoolean(1, true);
- sts.setString(2, "maintenance");
- sts.executeUpdate();
- sts.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void setMaintenanceOff() {
- try {
- PreparedStatement sts = getConnection()
- .prepareStatement("UPDATE " + table + " SET mn = ? WHERE uuid = ?");
- sts.setBoolean(1, false);
- sts.setString(2, "maintenance");
- sts.executeUpdate();
- sts.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void addMaintenance(String uuid) {
- try {
- PreparedStatement sts = getConnection()
- .prepareStatement("UPDATE " + table + " SET mn = ? WHERE uuid = ?");
- sts.setBoolean(1, true);
- sts.setString(2, uuid);
- sts.executeUpdate();
- sts.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void removeMaintenance(String uuid) {
- try {
- PreparedStatement sts = getConnection()
- .prepareStatement("UPDATE " + table + " SET mn = ? WHERE uuid = ?");
- sts.setBoolean(1, false);
- sts.setString(2, uuid);
- sts.executeUpdate();
- sts.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public boolean isMaintenance() {
- boolean mn = false;
- try {
- PreparedStatement sts = getConnection().prepareStatement("SELECT mn FROM " + table + " WHERE uuid = ?");
- sts.setString(1, "maintenance");
- ResultSet rs = sts.executeQuery();
- if (!rs.next()) {
- return mn;
- }
- mn = rs.getBoolean("mn");
- sts.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return mn;
- }
- public boolean playerIsMaintenance(String uuid) {
- boolean mn = false;
- try {
- PreparedStatement sts = getConnection().prepareStatement("SELECT mn FROM " + table + " WHERE uuid = ?");
- sts.setString(1, uuid);
- ResultSet rs = sts.executeQuery();
- if (!rs.next()) {
- return mn;
- }
- mn = rs.getBoolean("mn");
- sts.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return mn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement