Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. package fr.zeltiamc.shop.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.Bukkit;
  10. import org.bukkit.entity.Player;
  11.  
  12. public class SqlConnection {
  13.  
  14. private Connection connection;
  15. private String urlbase,host,database,user,pass;
  16.  
  17. public SqlConnection(String urlbase, String host, String database, String user, String pass) {
  18. this.urlbase = urlbase;
  19. this.host = host;
  20. this.database = database;
  21. this.user = user;
  22. this.pass = pass;
  23. }
  24.  
  25. public void connection(){
  26. if(!isConnected()){
  27. try {
  28. connection = DriverManager.getConnection(urlbase + host + "/" + database, user, pass);
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34.  
  35. public void disconnect(){
  36. if(isConnected()){
  37. try {
  38. connection.close();
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44.  
  45. public boolean isConnected(){
  46. return connection != null;
  47. }
  48.  
  49. public int getID(Player player){
  50. this.connection();
  51.  
  52. try {
  53. PreparedStatement q = connection.prepareStatement("SELECT id FROM luminacy__faction WHERE uuid = ?");
  54. q.setString(1, player.getUniqueId().toString());
  55.  
  56. int id = 0;
  57. ResultSet rs = q.executeQuery();
  58.  
  59. while(rs.next()){
  60. id = rs.getInt("id");
  61. }
  62.  
  63. q.close();
  64.  
  65. return id;
  66.  
  67. } catch (SQLException e) {
  68. e.printStackTrace();
  69. }
  70.  
  71. return 0;
  72. }
  73.  
  74. public boolean hasAccount(Player player){
  75. //SELECT
  76.  
  77. this.connection();
  78.  
  79. try {
  80. PreparedStatement q = connection.prepareStatement("SELECT uuid FROM luminacy__faction WHERE uuid = ?");
  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. return false;
  91. }
  92.  
  93. public void createAccount(Player player){
  94.  
  95. this.connection();
  96.  
  97. if(!hasAccount(player)){
  98.  
  99. try {
  100. PreparedStatement q = connection.prepareStatement("INSERT INTO luminacy__faction(uuid,coins,bank) VALUES (?,?,?)");
  101. q.setString(1, player.getUniqueId().toString());
  102. q.setDouble(2, 0);
  103. q.setDouble(3, 100);
  104. q.execute();
  105. q.close();
  106. } catch (SQLException e) {
  107. e.printStackTrace();
  108. }
  109.  
  110. Bukkit.broadcastMessage("§7Bienvenue à §6" + player.getName() + " §7sur Luminacy ! §7Tu est notre §6 " + this.getID(player) + "ème §7joueur !");
  111.  
  112. }
  113. }
  114.  
  115. public double getBalance(Player player){
  116. //SELECT
  117.  
  118. this.connection();
  119.  
  120. try {
  121. PreparedStatement q = connection.prepareStatement("SELECT coins FROM luminacy__faction WHERE uuid = ?");
  122. q.setString(1, player.getUniqueId().toString());
  123.  
  124. double balance = 0.0;
  125. ResultSet rs = q.executeQuery();
  126.  
  127. while(rs.next()){
  128. balance = rs.getDouble("coins");
  129. }
  130.  
  131. q.close();
  132.  
  133. return balance;
  134.  
  135. } catch (SQLException e) {
  136. e.printStackTrace();
  137. }
  138.  
  139. return 0;
  140. }
  141.  
  142. public double getBank(Player player){
  143. //SELECT
  144.  
  145. this.connection();
  146.  
  147. try {
  148. PreparedStatement q = connection.prepareStatement("SELECT bank FROM luminacy__faction WHERE uuid = ?");
  149. q.setString(1, player.getUniqueId().toString());
  150.  
  151. double balance = 0.0;
  152. ResultSet rs = q.executeQuery();
  153.  
  154. while(rs.next()){
  155. balance = rs.getDouble("bank");
  156. }
  157.  
  158. q.close();
  159.  
  160. return balance;
  161.  
  162. } catch (SQLException e) {
  163. e.printStackTrace();
  164. }
  165.  
  166. return 0;
  167. }
  168.  
  169.  
  170. public void addMoney(Player player,double amount){
  171.  
  172. this.connection();
  173.  
  174. double balance = getBalance(player);
  175. double newbalance = balance + amount;
  176.  
  177. try {
  178. PreparedStatement rs = connection.prepareStatement("UPDATE luminacy__faction SET coins = ? WHERE uuid = ?");
  179. rs.setDouble(1, newbalance);
  180. rs.setString(2, player.getUniqueId().toString());
  181. rs.executeUpdate();
  182. rs.close();
  183.  
  184. } catch (SQLException e) {
  185. e.printStackTrace();
  186. }
  187.  
  188. }
  189.  
  190. public void setMoney(Player player, double montant){
  191.  
  192. this.connection();
  193.  
  194. try {
  195. PreparedStatement rs = connection.prepareStatement("UPDATE luminacy__faction SET coins = ? WHERE uuid = ?");
  196. rs.setDouble(1, montant);
  197. rs.setString(2, player.getUniqueId().toString());
  198. rs.executeUpdate();
  199. rs.close();
  200.  
  201. } catch (SQLException e) {
  202. e.printStackTrace();
  203. }
  204.  
  205. }
  206.  
  207. public void setBank(Player player, double montant){
  208.  
  209. this.connection();
  210.  
  211. try {
  212. PreparedStatement rs = connection.prepareStatement("UPDATE luminacy__faction SET bank = ? WHERE uuid = ?");
  213. rs.setDouble(1, montant);
  214. rs.setString(2, player.getUniqueId().toString());
  215. rs.executeUpdate();
  216. rs.close();
  217.  
  218. } catch (SQLException e) {
  219. e.printStackTrace();
  220. }
  221.  
  222. }
  223.  
  224. public void removeMoney(Player player, double montant){
  225.  
  226. this.connection();
  227.  
  228. double balance = getBalance(player);
  229. double newbalance = balance - montant;
  230.  
  231. if(newbalance < 0){
  232. return;
  233. }
  234.  
  235. try {
  236. PreparedStatement rs = connection.prepareStatement("UPDATE luminacy__faction SET coins = ? WHERE uuid = ?");
  237. rs.setDouble(1, newbalance);
  238. rs.setString(2, player.getUniqueId().toString());
  239. rs.executeUpdate();
  240. rs.close();
  241.  
  242. } catch (SQLException e) {
  243. e.printStackTrace();
  244. }
  245.  
  246. }
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement