Guest User

Untitled

a guest
Jan 20th, 2016
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. package fr.Ajeux.MyRank;
  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. this.url_base = url_base;
  14. this.host = host;
  15. this.name = name;
  16. this.username = username;
  17. this.password = password;
  18. this.table = table;
  19. }
  20.  
  21. public void connection(){
  22. if(!isConnected()){
  23. try{
  24. connection = DriverManager.getConnection(url_base + host + "/" + name, username, password);
  25. }catch(SQLException e){
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30.  
  31. public void deconnection(){
  32. if(isConnected()){
  33. try{
  34. connection.close();
  35. }catch(SQLException e){
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40.  
  41. private boolean isConnected(){
  42. try{
  43. if((connection == null) || (connection.isClosed() || (connection.isValid(5)))){
  44. return false;
  45. }else{
  46. return true;
  47. }
  48. }catch(SQLException e){
  49. e.printStackTrace();
  50. }
  51. return false;
  52. }
  53.  
  54. private Connection getConnection(){
  55. return connection;
  56. }
  57.  
  58. public void createAccount(Player player){
  59. try{
  60. PreparedStatement sts = getConnection().prepareStatement("SELECT rank FROM " + table + " WHERE uuid = ?");
  61. sts.setString(1, player.getUniqueId().toString());
  62. ResultSet rs = sts.executeQuery();
  63. if(!rs.next()){
  64. sts.close();
  65. sts = getConnection().prepareStatement("INSERT INTO " + table + " (uuid, rank) VALUES (?, ?)");
  66. sts.setString(1, player.getUniqueId().toString());
  67. sts.setInt(2, 10);
  68. sts.executeUpdate();
  69. sts.close();
  70. }
  71. }catch(SQLException e){
  72. e.printStackTrace();
  73. }
  74. }
  75.  
  76. public Rank getRank(String uuid){
  77. Rank rank = null;
  78. try{
  79. PreparedStatement sts = getConnection().prepareStatement("SELECT rank FROM " + table + " WHERE uuid = ?");
  80. sts.setString(1, uuid);
  81. ResultSet rs = sts.executeQuery();
  82. if(!rs.next()){
  83. return null;
  84. }
  85. rank = Rank.getFromPower(rs.getInt("rank"));
  86. sts.close();
  87. }catch(SQLException e){
  88. e.printStackTrace();
  89. }
  90. return rank;
  91. }
  92.  
  93. public void setRank(Player player, Rank rank){
  94. try{
  95. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + table + " SET rank = ? WHERE uuid = ?");
  96. sts.setInt(1, rank.getPower());
  97. sts.setString(2, player.getUniqueId().toString());
  98. sts.executeUpdate();
  99. sts.close();
  100. }catch(SQLException e){
  101. e.printStackTrace();
  102. }
  103. }
  104.  
  105. public boolean hasPermission(Player player, String permission){
  106. try{
  107. PreparedStatement sts = getConnection().prepareStatement("SELECT permissions FROM " + table + " WHERE uuid = ?");
  108. sts.setString(1, player.getUniqueId().toString());
  109. ResultSet rs = sts.executeQuery();
  110. while(rs.next()){
  111. if(permission.equalsIgnoreCase(rs.getString("permissions"))){
  112. return true;
  113. }
  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 permissions = ?");
  139. sts.setString(1, player.getUniqueId().toString());
  140. sts.setString(2, permission);
  141. sts.executeUpdate();
  142. sts.close();
  143. }catch(SQLException e){
  144. e.printStackTrace();
  145. }
  146. }
  147. }
Add Comment
Please, Sign In to add comment