Advertisement
Guest User

Untitled

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