Guest User

RankSQL

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