Advertisement
Guest User

Untitled

a guest
Apr 7th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. package fr.smaak.groups;
  2.  
  3. import java.sql.*;
  4.  
  5. import org.bukkit.entity.Player;
  6.  
  7. public class RankSQL {
  8.    
  9.     private String url_base, host, name, username, password, table;
  10.     private Connection connection;
  11.    
  12.     public RankSQL(String url_base, String host, String name, String username, String password, String table){
  13.        
  14.         this.url_base = url_base;
  15.         this.host = host;
  16.         this.name = name;
  17.         this.username = username;
  18.         this.password = password;
  19.         this.table = table;
  20.     }
  21.    
  22.     public void Connection(){
  23.         if(!isConnected()){
  24.             try{
  25.                 connection = DriverManager.getConnection(url_base + host + "/" + name, username, password);
  26.             }catch (SQLException e){
  27.                 e.printStackTrace();
  28.             }
  29.         }
  30.     }
  31.    
  32.     public void deconnection(){
  33.         if(isConnected()){
  34.             try{
  35.                 connection.close();
  36.             }catch (SQLException e){
  37.                 e.printStackTrace();
  38.             }
  39.         }
  40.     }
  41.    
  42.     private boolean isConnected(){
  43.         try{
  44.             if((connection == null) || (connection.isClosed()) || (connection.isValid(5))){
  45.                 return false;
  46.             } else {
  47.                 return true;
  48.             }
  49.         }catch (SQLException e){
  50.             e.printStackTrace();
  51.         }
  52.         return false;
  53.     }
  54.    
  55.     private Connection getConnection(){
  56.         return connection;
  57.     }
  58.    
  59.     public void createAccount(Player player){
  60.         try{
  61.              PreparedStatement sts = getConnection().prepareStatement("SELECT rank FROM "+table+" WHERE uuid = ?");
  62.              sts.setString(1, player.getUniqueId().toString());
  63.              ResultSet rs = sts.executeQuery();
  64.              if(!rs.next()){
  65.                  sts.close();
  66.                  sts = getConnection().prepareStatement("INSERT INTO " + table + " (uuid, rank) VALUES (?, ?)");
  67.                  sts.setString(1, player.getUniqueId().toString());
  68.                  sts.setInt(2, 10);
  69.                  sts.executeUpdate();
  70.                  sts.close();
  71.              }
  72.         }catch (SQLException e){
  73.             e.printStackTrace();
  74.         }
  75.     }
  76.    
  77.     public Rank getRank(String uuid){
  78.         Rank rank = null;  
  79.         try{
  80.             PreparedStatement sts = getConnection().prepareStatement("SELECT rank FROM "+table+" WHERE uuid = ?");
  81.             sts.setString(1, uuid);
  82.             ResultSet rs = sts.executeQuery();
  83.             if(!rs.next()){
  84.                 return null;
  85.             }
  86.             rank = Rank.getFromPower(rs.getInt("rank"));
  87.             sts.close();
  88.         }catch (SQLException e){
  89.             e.printStackTrace();
  90.         }
  91.         return rank;
  92.     }
  93.    
  94.     public void setRank(Player player, Rank rank){
  95.         try{
  96.             PreparedStatement sts = getConnection().prepareStatement("UPDATE " + table + " SET rank = ? WHERE uuid = ?");
  97.             sts.setInt(1, rank.getPower());
  98.             sts.setString(2, player.getUniqueId().toString());
  99.             sts.executeUpdate();
  100.             sts.close();
  101.         }catch (SQLException e){
  102.             e.printStackTrace();
  103.         }
  104.     }
  105.    
  106.     public boolean hasPermission(Player player, String permission){
  107.         try{
  108.             PreparedStatement sts = getConnection().prepareStatement("SELECT permissions FROM " + table + " WHERE uuid = ? ");
  109.             sts.setString(1, player.getUniqueId().toString());
  110.             ResultSet rs = sts.executeQuery();
  111.             while(rs.next()){
  112.                 if(permission.equalsIgnoreCase(rs.getString("permissions")));
  113.                     return true;
  114.                 }
  115.                 sts.close();
  116.         }catch (SQLException e){
  117.             e.printStackTrace();
  118.         }
  119.         return false;
  120.     }
  121.    
  122.     public void addPermission(Player player, String permission){
  123.         try{
  124.             PreparedStatement sts = getConnection().prepareStatement("INSERT INTO " + table + " (uuid, rank, permissions) VALUES (?, ?, ?)");
  125.             sts.setString(1, player.getUniqueId().toString());
  126.             sts.setInt(2, getRank(player.getUniqueId().toString()).getPower());
  127.             sts.setString(3, permission);
  128.             sts.executeUpdate();
  129.             sts.close();
  130.         }catch (SQLException e){
  131.             e.printStackTrace();
  132.         }
  133.     }
  134.    
  135.     public void removePermission(Player player, String permission){
  136.         try{
  137.             PreparedStatement sts = getConnection().prepareStatement("DELETE FROM " + table + " WHERE " + " uuid = ? "+"and permissions = ?");
  138.             sts.setString(1, player.getUniqueId().toString());
  139.             sts.setString(2, permission);
  140.             sts.executeUpdate();
  141.             sts.close();
  142.         }catch (SQLException e){
  143.             e.printStackTrace();
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement