EloxFire

SQLConnection

Jun 22nd, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. package fr.eloxfire.SimplifiedCoins;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import org.bukkit.entity.Player;
  9.  
  10. public class SQLConnection {
  11.  
  12. private Connection connection;
  13. private String urlbase, host, database, user, mdp;
  14.  
  15. public SQLConnection(String urlbase, String host, String database, String user, String mdp) {
  16. this.urlbase = urlbase;
  17. this.host = host;
  18. this.database = database;
  19. this.user = user;
  20. this.mdp = mdp;
  21. }
  22.  
  23. public void connection(){
  24. if(!isConnected()){
  25. try {
  26. connection = DriverManager.getConnection(urlbase + host + "/" + database, user, mdp);
  27. System.out.println("Connected ok !");
  28. } catch (SQLException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33.  
  34. /*
  35. * INSERT
  36. * UPDATE
  37. * DELETE
  38. * SELECT
  39. *
  40. * PREPARER ?,?
  41. * REMPLACER LES ? PAR DES VALEURS
  42. * EXECUTER
  43. *
  44. *
  45. */
  46.  
  47. public void disconnect(){
  48. if(isConnected()){
  49. try {
  50. connection.close();
  51. System.out.println("Disconnected ok !");
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57.  
  58. public boolean isConnected(){
  59. return connection != null;
  60. }
  61.  
  62. public void createAccount(Player player){
  63. if(!hasAccount(player)){
  64. //INSERT
  65.  
  66. try {
  67. PreparedStatement q = connection.prepareStatement("INSERT INTO joueurs(uuid,argent,grade) VALUES (?,?,?)");
  68. q.setString(1, player.getUniqueId().toString());
  69. q.setInt(2, 1000);
  70. q.setString(3, "Membre");
  71. q.execute();
  72. q.close();
  73. } catch (SQLException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. }
  78.  
  79. public boolean hasAccount(Player player){
  80. //SELECT
  81.  
  82. try {
  83. PreparedStatement q = connection.prepareStatement("SELECT uuid FROM joueurs WHERE uuid = ?");
  84. q.setString(1, player.getUniqueId().toString());
  85. ResultSet resultat = q.executeQuery();
  86. boolean hasAccount = resultat.next();
  87. q.close();
  88. return hasAccount;
  89.  
  90. } catch (SQLException e) {
  91. // TODO Auto-generated catch block
  92. e.printStackTrace();
  93. }
  94.  
  95. return false;
  96. }
  97.  
  98. public int getBalance(Player player){
  99. //SELECT
  100.  
  101. try {
  102. PreparedStatement q = connection.prepareStatement("SELECT uuid FROM joueurs WHERE uuid = ?");
  103. q.setString(1, player.getUniqueId().toString());
  104.  
  105. int balance = 0;
  106. ResultSet rs = q.executeQuery();
  107.  
  108. while(rs.next()){
  109. balance = rs.getInt("argent");
  110. }
  111.  
  112. q.close();
  113.  
  114. return balance;
  115.  
  116. } catch (SQLException e) {
  117. // TODO Auto-generated catch block
  118. e.printStackTrace();
  119. }
  120.  
  121. return 0;
  122. }
  123.  
  124. public void addMoney(Player player, int amount){
  125. //UPDATE
  126.  
  127. int balance = getBalance(player);
  128. int newbalance = balance + amount;
  129.  
  130. try {
  131. PreparedStatement rs = connection.prepareStatement("UPDATE joueurs SET argent = ? WHERE uuid = ?");
  132. rs.setInt(1, newbalance);
  133. rs.setString(2, player.getUniqueId().toString());
  134. rs.executeUpdate();
  135. rs.close();
  136. } catch (SQLException e) {
  137. e.printStackTrace();
  138. }
  139.  
  140. }
  141.  
  142. public void removeMoney(Player player, int amount){
  143. //UPDATE
  144.  
  145. int balance = getBalance(player);
  146. int newbalance = balance - amount;
  147.  
  148. if(newbalance <= 0){
  149. return;
  150. }
  151.  
  152. try {
  153. PreparedStatement rs = connection.prepareStatement("UPDATE joueurs SET argent = ? WHERE uuid = ?");
  154. rs.setInt(1, newbalance);
  155. rs.setString(2, player.getUniqueId().toString());
  156. rs.executeUpdate();
  157. rs.close();
  158. } catch (SQLException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162.  
  163. }
Add Comment
Please, Sign In to add comment