Vinetos

CoinsAPI (Requete)

Sep 20th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. import org.bukkit.entity.Player;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6. * @author VINETOS on 20/09/2015.
  7. */
  8. public class API {
  9.  
  10. private String url_base, host, name, username, password, table;
  11. private Connection connection;
  12.  
  13. public API(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. //Fonction pour se connecter à la base de données
  24. if(!isConnected()){//On vérifie qu'on est pas déjà connecter !
  25. try {
  26. //On charge les drivers pour établir la connection
  27. connection = DriverManager.getConnection(url_base + host + "/" + name, username, password);
  28. } catch (SQLException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33.  
  34. public void deconnection(){//Fonction de deconexion
  35. if(isConnected()){//On vérifie qu'on est connecté
  36. try {
  37. connection.close();//On ferme la connexion
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43.  
  44. private boolean isConnected(){
  45. try {
  46. //On vérifie si la connexion est null, fermée ou Valide
  47. if((connection == null) || (connection.isClosed()) || (!connection.isValid(5))){
  48. //Si non, on return false
  49. return false;
  50. }else{
  51. return true;
  52. }
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56. return false;
  57. }
  58.  
  59. private Connection getConnection(){//FOnction pour récupérer la connexion
  60. return connection;
  61. }
  62.  
  63. public void createPlayerIfNotExist(Player player){
  64. try {
  65. PreparedStatement sts = getConnection().prepareStatement("SELECT COUNT(*) FROM "+table+" WHERE uuid = ?");
  66. sts.setString(1, player.getUniqueId().toString());
  67. ResultSet rs = sts.executeQuery();
  68. if(!rs.next()){
  69. sts.close();
  70. sts = getConnection().prepareStatement("INSERT INTO "+table+" (uuid, coins) VALUES (?, ?)");
  71. sts.setString(1, player.getUniqueId().toString());
  72. sts.setInt(2, 0);
  73. sts.executeUpdate();
  74. sts.close();
  75. }
  76. } catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80.  
  81. public void addCoins(String uuid, int coins){//On ajoute des points au joueurs avec l'udid du joueur + le nombre de points à rajouter
  82. try {
  83. PreparedStatement sts = getConnection().prepareStatement("UPDATE "+table+" SET coins = coins + ? WHERE uuid = ?");//Requete pour ajouté des points
  84. sts.setInt(1, coins);
  85. sts.setString(2, uuid);
  86. sts.executeUpdate();
  87. sts.close();
  88. } catch (SQLException e) {
  89. e.printStackTrace();
  90. }
  91.  
  92. }
  93.  
  94. public void removeCoins(String uuid, int coins){
  95. try {
  96. PreparedStatement sts = getConnection().prepareStatement("UPDATE "+table+" SET coins = coins - ? WHERE uuid = ?");//Requete pour supprimer des coins
  97. sts.setInt(1, coins);
  98. sts.setString(2, uuid);
  99. sts.executeUpdate();
  100. sts.close();
  101. } catch (SQLException e) {
  102. e.printStackTrace();
  103. }
  104.  
  105. }
  106. public Integer getPoints(String uuid){
  107. try {
  108. PreparedStatement sts = getConnection().prepareStatement("SELECT coins FROM "+table+" WHERE uuid = ?");//Requete pour récupérer des coins
  109. sts.setString(1, uuid);
  110. ResultSet rs = sts.executeQuery();
  111. if(!rs.next()){
  112. return 0;
  113. }
  114. sts.close();
  115. return rs.getInt("coins");
  116. } catch (SQLException e) {
  117. e.printStackTrace();
  118. }
  119. return 0;
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment