Advertisement
Guest User

SqlConnection

a guest
Aug 12th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. package ca.thecow.ecownomie;
  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("Bien connecte a la base de donnee");
  30. } catch (SQLException e) {
  31.  
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. }
  37.  
  38. public void disconnect() {
  39.  
  40. if(isConnected()){
  41.  
  42. try {
  43. connection.close();
  44. System.out.println("Bien deconnecte de la base de donnee");
  45. } catch (SQLException e) {
  46.  
  47. e.printStackTrace();
  48. }
  49. }
  50.  
  51. }
  52.  
  53. public boolean isConnected(){
  54. return connection != null;
  55. }
  56.  
  57. public void createAccount(Player player){
  58. if(!hasAccount(player)){
  59. //INSERT
  60.  
  61. try {
  62. PreparedStatement q = connection.prepareStatement("INSERT INTO joueurs(uuid, coins, grade) VALUES (?, ?, ?)");
  63.  
  64. q.setString(1, player.getUniqueId().toString());
  65. q.setInt(2, 100);
  66. q.setString(3, "joueur");
  67. q.execute();
  68. q.close();
  69. } catch (SQLException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74.  
  75. public boolean hasAccount(Player player){
  76. //SELECT
  77.  
  78. try {
  79. PreparedStatement q = connection.prepareStatement("SELECT uuid FROM joueurs WHERE uuid = ?");
  80.  
  81. q.setString(1, player.getUniqueId().toString());
  82. ResultSet resultat = q.executeQuery();
  83. boolean hasAccount = resultat.next();
  84. q.close();
  85. return hasAccount;
  86. } catch (SQLException e) {
  87. e.printStackTrace();
  88. }
  89.  
  90.  
  91. return false;
  92. }
  93.  
  94. public int getMoney(Player player){
  95. //SELECT
  96. try {
  97. PreparedStatement q = connection.prepareStatement("SELECT coins FROM joueurs WHERE uuid = ?");
  98. q.setString(1, player.getUniqueId().toString());
  99.  
  100. int balance = 100;
  101. ResultSet rs = q.executeQuery();
  102.  
  103. while(rs.next()){
  104. balance = rs.getInt("coins");
  105. }
  106.  
  107. q.close();
  108.  
  109. return balance;
  110. } catch (SQLException e) {
  111. e.printStackTrace();
  112. }
  113.  
  114.  
  115.  
  116.  
  117. return 0;
  118.  
  119. }
  120.  
  121. public void addMoney(Player player, int amount){
  122. //UPDATE
  123.  
  124. int balance = getMoney(player);
  125. int newbalance = balance + amount;
  126.  
  127. try {
  128. PreparedStatement rs = connection.prepareStatement("UPDATE joueurs SET coins = ? WHERE uuid = ?");
  129. rs.setInt(1, newbalance);
  130. rs.setString(2, player.getUniqueId().toString());
  131. rs.executeUpdate();
  132. rs.close();
  133. } catch (SQLException e) {
  134. // TODO Auto-generated catch block
  135. e.printStackTrace();
  136. }
  137.  
  138.  
  139.  
  140. }
  141.  
  142. public void removeMoney(Player player, int amount){
  143. //UPDATE
  144.  
  145. int balance = getMoney(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 coins = ? 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. // TODO Auto-generated catch block
  160. e.printStackTrace();
  161. }
  162.  
  163. }
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement