Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. package fr.ck.coins;
  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.  
  25. public void connection() {
  26. if (isConnected()) {
  27. try {
  28. connection = DriverManager.getConnection(urlbase + host + "/" + database, user, pass);
  29. System.out.println("connected ok");
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35.  
  36. public void disconnect() {
  37. if (isConnected()) {
  38. }
  39. try {
  40. connection.close();
  41. System.out.println("connected off");
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46.  
  47. public boolean isConnected() {
  48. return connection != null;
  49. }
  50.  
  51. public void createAccount(Player player) {
  52. if (!hasAccount(player)) {
  53. // INSERT
  54.  
  55. try {
  56. PreparedStatement q = connection
  57. .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. public boolean hasAccount(Player player) {
  71. // SELECT
  72.  
  73. try {
  74. PreparedStatement q = connection.prepareStatement("SELECT uuid FROM joueurs WHERE uuid = ?");
  75. q.setString(1, player.getUniqueId().toString());
  76. ResultSet resultat = q.executeQuery();
  77. boolean hasAccount = resultat.next();
  78. q.close();
  79. return hasAccount;
  80. } catch (SQLException e) {
  81. e.printStackTrace();
  82. }
  83.  
  84. return false;
  85. }
  86.  
  87. public int getBalance(Player player) {
  88. // SELECT
  89.  
  90. try {
  91. PreparedStatement q = connection.prepareStatement("SELECT coins FROM joueurs WHERE uuid = ?");
  92. q.setString(1, player.getUniqueId().toString());
  93.  
  94. int balance = 0;
  95. ResultSet rs = q.executeQuery();
  96.  
  97. while (rs.next()) {
  98. balance = rs.getInt("coins");
  99. }
  100.  
  101. q.close();
  102.  
  103. return balance;
  104.  
  105. } catch (SQLException e) {
  106. e.printStackTrace();
  107. }
  108.  
  109. return 0;
  110. }
  111.  
  112. public void addMoney(Player player, int amount) {
  113. // UPDATE
  114.  
  115. int balance = getBalance(player);
  116. int newbalance = balance + amount;
  117.  
  118. try {
  119. PreparedStatement rs = connection.prepareStatement("UPDATE joueurs SET coins = ? WHERE uuid = ?");
  120. rs.setInt(1, newbalance);
  121. rs.setString(2, player.getUniqueId().toString());
  122. rs.executeUpdate();
  123. rs.close();
  124.  
  125. } catch (SQLException e) {
  126. e.printStackTrace();
  127. }
  128.  
  129. }
  130.  
  131. public void removeMoney(Player player, int amount) {
  132. // UPDATE
  133.  
  134. int balance = getBalance(player);
  135. int newbalance = balance - amount;
  136.  
  137. if (newbalance <= 0) {
  138. return;
  139. }
  140.  
  141. try {
  142. PreparedStatement rs = connection.prepareStatement("UPDATE joueurs SET coins = ? WHERE uuid = ?");
  143. rs.setInt(1, newbalance);
  144. rs.setString(2, player.getUniqueId().toString());
  145. rs.executeUpdate();
  146. rs.close();
  147.  
  148. } catch (SQLException e) {
  149. e.printStackTrace();
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement