Advertisement
Guest User

MySQL.java

a guest
Aug 11th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. package fr.irazon.honoration.utils;
  2.  
  3. import com.irazon.api.rank.RankUtils;
  4. import org.bukkit.entity.Player;
  5.  
  6. import java.io.IOException;
  7. import java.sql.*;
  8.  
  9. /**
  10.  * The MIT License (MIT)
  11.  * Created on 10/08/2016.
  12.  * Copyright (c) 2016 @author iRaZoN_
  13.  */
  14. public class MySQL {
  15.  
  16.     private static Connection connection;
  17.     private static String url_base = "jdbc:mysql://", host = "localhost/test", username = "irazon_", password = "mdp";
  18.  
  19.     public static Connection getConnection() {
  20.         return connection;
  21.     }
  22.  
  23.     public static boolean isConnected() {
  24.         try {
  25.             if ((connection == null) || (connection.isClosed())) {
  26.                 return false;
  27.             } else {
  28.                 return true;
  29.             }
  30.         } catch (SQLException e) {
  31.             e.printStackTrace();
  32.         }
  33.         return false;
  34.     }
  35.  
  36.     public static void connections() {
  37.         if(!isConnected()) {
  38.             try {
  39.                 connection = DriverManager.getConnection(url_base + host, username, password);
  40.             } catch (SQLException e) {
  41.                 e.printStackTrace();
  42.             }
  43.         }
  44.     }
  45.  
  46.     public static void deconnection() {
  47.         if(isConnected()) {
  48.             try {
  49.                 connection.close();
  50.             } catch (SQLException e) {
  51.                 e.printStackTrace();
  52.             }
  53.         }
  54.     }
  55.  
  56.     public static void createAccount(Player player) {
  57.         MySQL.connections();
  58.         try {
  59.             if(!hasAccount(player)) {
  60.                 PreparedStatement sts = MySQL.getConnection().prepareStatement("INSERT INTO sanctions (uuid, rankname, bantime, banreason, banowner, mutetime, mutereason, muteowner) VALUES(?, ?, ?, ?, ?, ?, ?, ?");
  61.                 sts.setString(1, player.getUniqueId().toString());
  62.                 sts.setString(2, RankUtils.getRank(player).getName());
  63.                 sts.setInt(3, 0);
  64.                 sts.setString(4, null);
  65.                 sts.setString(5, null);
  66.                 sts.setInt(6, 0);
  67.                 sts.setString(7, null);
  68.                 sts.setString(8, null);
  69.                 sts.executeUpdate();
  70.                 sts.close();
  71.             }
  72.         } catch (SQLException e) {
  73.             e.printStackTrace();
  74.         }
  75.         MySQL.deconnection();
  76.     }
  77.  
  78.     public static boolean hasAccount(Player player) {
  79.         MySQL.connections();
  80.         try {
  81.             PreparedStatement sts = MySQL.getConnection().prepareStatement("SELECT uuid FROM sanctions");
  82.             ResultSet result = sts.executeQuery();
  83.             if(result.next()) {
  84.                 System.out.println("HASACCOUNT OKEY");
  85.                 return true;
  86.             }
  87.         } catch (SQLException e) {
  88.             e.printStackTrace();
  89.         }
  90.         return false;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement