Advertisement
Guest User

Untitled

a guest
Mar 26th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. package com.rank.fr;
  2.  
  3. import java.security.interfaces.RSAKey;
  4. import java.sql.*;
  5.  
  6. import org.bukkit.entity.Player;
  7.  
  8. public class RankSQL {
  9.  
  10. private String url_base, host, name, username, password, table;
  11. private Connection connection;
  12.  
  13. public RankSQL(String url_base, String host, String name, String username, String password, String table){
  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.executeQuery();
  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.  
  123. public void addPermission(Player player, String permission){
  124. try{
  125. PreparedStatement sts = getConnection().prepareStatement("INSERT INTO"+table+" (uuid, rank, permissions) VALUES (?,?,?)");
  126. sts.setString(1, player.getUniqueId().toString());
  127. sts.setInt(2, getRank(player.getUniqueId().toString()).getPower());
  128. sts.setString(3, permission);
  129. sts.executeUpdate();
  130. sts.close();
  131. }catch (SQLException e){
  132. e.printStackTrace();
  133. }
  134. }
  135.  
  136. public void removePermission(Player player, String permission){
  137. try{
  138. PreparedStatement sts = getConnection().prepareStatement("DELETE FROM" +table+ " WHERE " + " uuid = ?" + "and permission = ?");
  139. sts.setString(1, player.getUniqueId().toString());
  140. sts.setString(2, permission);
  141. sts.executeUpdate();
  142. sts.close();
  143.  
  144. }catch (SQLException e){
  145. e.printStackTrace();
  146. }
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement