Advertisement
Ajeux

[GameBooster] SQL

Mar 4th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. package com.ekronia.api.mysql.gamebooster;
  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 java.sql.Timestamp;
  9.  
  10. import com.ekronia.api.bukkit.utils.game.GameUtils;
  11. import com.ekronia.api.bungee.utils.GameUtilsBungee;
  12.  
  13. public class GameBoosterSQL {
  14.  
  15. private String url_base, host, name, username, password, table;
  16. private Connection connection;
  17.  
  18. public GameBoosterSQL(String url_base, String host, String name, String username, String password, String table) {
  19. this.url_base = url_base;
  20. this.host = host;
  21. this.name = name;
  22. this.username = username;
  23. this.password = password;
  24. this.table = table;
  25. }
  26.  
  27. private Connection getConnection() {
  28. return connection;
  29. }
  30.  
  31. public void connection() {
  32.  
  33. if (!isConnected()) {
  34.  
  35. try {
  36. connection = DriverManager.getConnection(url_base + host + "/" + name, username, password);
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40.  
  41. }
  42.  
  43. }
  44.  
  45. public void deconnection() {
  46.  
  47. if (isConnected()) {
  48. try {
  49. connection.close();
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53.  
  54. }
  55.  
  56. }
  57.  
  58. private boolean isConnected() {
  59.  
  60. try {
  61.  
  62. if ((connection == null) || (connection.isClosed()) || (connection.isValid(5))) {
  63. return false;
  64. } else {
  65. return true;
  66. }
  67.  
  68. } catch (SQLException e) {
  69. e.printStackTrace();
  70. }
  71. return false;
  72. }
  73.  
  74. public void addGameBooster(GameUtilsBungee game, Timestamp time, int multiplicator){
  75. try {
  76. PreparedStatement sts = getConnection().prepareStatement("SELECT game FROM " + table + " WHERE game = ?");
  77. sts.setString(1, game.getName());
  78. ResultSet rs = sts.executeQuery();
  79.  
  80. if (!rs.next()) {
  81. sts = getConnection().prepareStatement("INSERT INTO " + table + " (game, time, multiplicator) VALUES (?, ?, ?)");
  82. sts.setString(1, game.getName());
  83. sts.setTimestamp(2, time);
  84. sts.setInt(3, multiplicator);
  85. sts.executeUpdate();
  86. }
  87. sts.close();
  88. } catch (SQLException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92.  
  93. public boolean asBooster(GameUtilsBungee game){
  94. try {
  95. PreparedStatement sts = getConnection().prepareStatement("SELECT * FROM " + table + " WHERE game = ?");
  96. sts.setString(1, game.getName());
  97. ResultSet rs = sts.executeQuery();
  98. if (!rs.next()) {
  99. return false;
  100. }
  101. int booster = rs.getInt("multiplicator");
  102. sts.close();
  103. if(booster == 1){
  104. return false;
  105. }else{
  106. return true;
  107. }
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. }
  111. return false;
  112. }
  113.  
  114. public boolean asBoosterBukkit(GameUtils game){
  115. try {
  116. PreparedStatement sts = getConnection().prepareStatement("SELECT * FROM " + table + " WHERE game = ?");
  117. sts.setString(1, game.getName());
  118. ResultSet rs = sts.executeQuery();
  119. if (!rs.next()) {
  120. return false;
  121. }
  122. int booster = rs.getInt("multiplicator");
  123. sts.close();
  124. if(booster == 1){
  125. return false;
  126. }else{
  127. return true;
  128. }
  129. } catch (SQLException e) {
  130. e.printStackTrace();
  131. }
  132. return false;
  133. }
  134.  
  135. public int getMultiplicator(GameUtilsBungee game){
  136. int multi = 1;
  137. try {
  138. PreparedStatement sts = getConnection().prepareStatement("SELECT * FROM " + table + " WHERE game = ?");
  139. sts.setString(1, game.getName());
  140. ResultSet rs = sts.executeQuery();
  141. if (!rs.next()) {
  142. return multi;
  143. }
  144. multi = rs.getInt("multiplicator");
  145. sts.close();
  146.  
  147. } catch (SQLException e) {
  148. e.printStackTrace();
  149. }
  150. return multi;
  151. }
  152.  
  153. public Timestamp getDate(GameUtilsBungee game){
  154. Timestamp Timestamp = null;
  155. try {
  156. PreparedStatement sts = getConnection().prepareStatement("SELECT * FROM " + table + " WHERE game = ?");
  157. sts.setString(1, game.getName());
  158. ResultSet rs = sts.executeQuery();
  159. if (!rs.next()) {
  160. return Timestamp;
  161. }
  162. Timestamp = rs.getTimestamp("time");
  163. sts.close();
  164.  
  165. } catch (SQLException e) {
  166. e.printStackTrace();
  167. }
  168. return Timestamp;
  169. }
  170.  
  171. public void removeGameBooster(GameUtilsBungee game){
  172. try {
  173. PreparedStatement sts = getConnection().prepareStatement("DELETE FROM " + table + " WHERE game = ?");
  174. sts.setString(1, game.getName());
  175. sts.executeUpdate();
  176. sts.close();
  177.  
  178. } catch (SQLException e) {
  179. e.printStackTrace();
  180. }
  181. }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement