Advertisement
Guest User

Untitled

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