Advertisement
Guest User

Coins SQL

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