Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.76 KB | None | 0 0
  1. package fr.koalak.skywars.database;
  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.Statement;
  9. import java.util.HashMap;
  10. import java.util.UUID;
  11.  
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.entity.Player;
  14.  
  15. import fr.tigger.elycoinsapi.util.PlayerUtil;
  16.  
  17. /**
  18. * Created by Tigger_San on 29/08/2016.
  19. */
  20. public class SQL
  21. {
  22.  
  23. private Connection connection;
  24.  
  25. @SuppressWarnings("unused")
  26. private PreparedStatement preparedStatement;
  27. private Statement statement;
  28. private ResultSet result;
  29.  
  30.  
  31. private String url;
  32. private String host;
  33. private String database;
  34. private String user;
  35. private String pass;
  36.  
  37.  
  38.  
  39. public void connect(String url, String host, String database, String user, String pass){
  40. this.url = url;
  41. this.host=host;
  42. this.database=database;
  43. this.user=user;
  44. this.pass=pass;
  45. try{
  46. this.connection = DriverManager.getConnection(this.url+ this.host+"/"+this.database,this.user,this.pass);
  47. this.statement = connection.createStatement();
  48. System.out.println("OK");
  49. }
  50. catch(SQLException e){
  51. e.printStackTrace();
  52. }
  53.  
  54.  
  55. }
  56.  
  57. public void connectTo() throws ClassNotFoundException, SQLException {
  58. Class.forName("com.mysql.jdbc.Driver");
  59. connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, user, pass);
  60. statement = connection.createStatement();
  61. }
  62.  
  63.  
  64.  
  65. private Connection connect() {
  66. try {
  67. if (statement != null && connection != null) {
  68. return connection;
  69. } else {
  70. connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database,user, pass);
  71. statement = connection.createStatement();
  72. return connection;
  73. }
  74. }catch (SQLException e) {
  75. e.printStackTrace();
  76. return connection;
  77. }
  78. }
  79.  
  80. private Statement statement() {
  81. try {
  82. if (statement != null && connection != null) {
  83. return statement;
  84. } else {
  85. connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, user, pass);
  86. statement = connection.createStatement();
  87. return statement;
  88. }
  89. } catch (SQLException e) {
  90. e.printStackTrace();
  91. return statement;
  92. }
  93. }
  94.  
  95. public void close() {
  96. try {
  97. if (result != null)
  98. result.close();
  99. if (statement != null)
  100. statement.close();
  101. if (connect() != null)
  102. connect().close();
  103. } catch (SQLException e) {
  104. }
  105. }
  106. public void initTable(){
  107. try{
  108. statement().executeUpdate("create table if not exists Skywars_stats (UUID VARCHAR(64) INT,WIN INT, PLAYED INT,TIME_PLAYED INT, LOOSE INT, KILLS INT, DEATH INT, COINS INT, ELYCOINS INT, TIME_PLAYED INT)");
  109. System.out.println("FAIT");
  110. }catch (SQLException e) {
  111. try {
  112. close();
  113. connectTo();
  114. }catch (ClassNotFoundException | SQLException err) {
  115. err.printStackTrace();
  116. }
  117. }
  118. }
  119.  
  120.  
  121.  
  122.  
  123. public void addPlayedGame(UUID playerID){
  124. try {
  125. statement().executeUpdate("update Skywars_stats set PLAYED=PLAYED+"+1+" where UUID='"+playerID+"'");
  126. } catch (SQLException e) {
  127. try{
  128. close();
  129. connectTo();
  130. }
  131. catch(ClassNotFoundException| SQLException e1){
  132. e1.printStackTrace();
  133. }
  134. }
  135.  
  136. }
  137.  
  138. public void addKill(UUID playerID){
  139. try {
  140. statement().executeUpdate("update Skywars_stats set KILLS=KILLS+"+1+" where UUID='"+playerID+"'");
  141. } catch (SQLException e) {
  142. try{
  143. close();
  144. connectTo();
  145. }
  146. catch(ClassNotFoundException| SQLException e1){
  147. e1.printStackTrace();
  148. }
  149. }
  150.  
  151. }
  152.  
  153. public void addDeath(UUID playerID){
  154. try {
  155. statement().executeUpdate("update Skywars_stats set DEATH=DEATH+"+1+" where UUID='"+playerID+"'");
  156. } catch (SQLException e) {
  157. try{
  158. close();
  159. connectTo();
  160. }
  161. catch(ClassNotFoundException| SQLException e1){
  162. e1.printStackTrace();
  163. }
  164. }
  165.  
  166. }
  167.  
  168. public void addCoins(UUID playerID){
  169. try {
  170. for(Player pls : Bukkit.getOnlinePlayers()){
  171. statement().executeUpdate("update Skywars_stats set COINS=COINS+"+PlayerUtil.getEarnedCoins(pls.getUniqueId())+" where UUID='"+playerID+"'");
  172. }
  173. } catch (SQLException e) {
  174. try{
  175. close();
  176. connectTo();
  177. }
  178. catch(ClassNotFoundException| SQLException e1){
  179. e1.printStackTrace();
  180. }
  181. }
  182.  
  183. }
  184.  
  185. public void addElyCoins(UUID playerID){
  186. try {
  187. for(Player pls : Bukkit.getOnlinePlayers()){
  188. statement().executeUpdate("update Skywars_stats set ELYCOINS=ELYCOINS+"+PlayerUtil.getEarnedElyCoins(pls.getUniqueId())+" where UUID='"+playerID+"'");
  189. }
  190. } catch (SQLException e) {
  191. try{
  192. close();
  193. connectTo();
  194. }
  195. catch(ClassNotFoundException| SQLException e1){
  196. e1.printStackTrace();
  197. }
  198. }
  199.  
  200. }
  201.  
  202. public void addWin(UUID playerID){
  203. try {
  204. statement().executeUpdate("update Skywars_stats set WIN=WIN+"+1+" where UUID='"+playerID+"'");
  205. } catch (SQLException e) {
  206. try{
  207. close();
  208. connectTo();
  209. }
  210. catch(ClassNotFoundException| SQLException e1){
  211. e1.printStackTrace();
  212. }
  213. }
  214. }
  215.  
  216. public void addLoose(UUID playerID){
  217. try {
  218. statement().executeUpdate("update Skywars_stats set LOOSE=LOOSE+"+1+" where UUID='"+playerID+"'");
  219. } catch (SQLException e) {
  220. try{
  221. close();
  222. connectTo();
  223. }
  224. catch(ClassNotFoundException| SQLException e1){
  225. e1.printStackTrace();
  226. }
  227. }
  228. }
  229.  
  230.  
  231.  
  232. public int getKill(UUID playerID){
  233. int KILLS=0;
  234. try {
  235. result = statement().executeQuery("select KILLS from Skywars_stats where UUID='"+playerID+"'");
  236. while(result.next()){
  237. KILLS = result.getInt("KILLS");
  238. }
  239. } catch (SQLException e) {
  240. try {
  241. close();
  242. connectTo();
  243. }catch (ClassNotFoundException | SQLException err) {
  244. err.printStackTrace();
  245. }
  246. }
  247. return KILLS;
  248. }
  249.  
  250. public int getWin(UUID playerID){
  251. int WIN=0;
  252. try {
  253. result = statement().executeQuery("select WIN from Skywars_stats where UUID='"+playerID+"'");
  254. while(result.next()){
  255. WIN = result.getInt("WIN");
  256. }
  257. } catch (SQLException e) {
  258. try {
  259. close();
  260. connectTo();
  261. }catch (ClassNotFoundException | SQLException err) {
  262. err.printStackTrace();
  263. }
  264. }
  265. return WIN;
  266. }
  267.  
  268. public int getDeath(UUID playerID){
  269. int DEATH=0;
  270. try {
  271. result = statement().executeQuery("select DEATH from Skywars_stats where UUID='"+playerID+"'");
  272. while(result.next()){
  273. DEATH = result.getInt("WIN");
  274. }
  275. } catch (SQLException e) {
  276. try {
  277. close();
  278. connectTo();
  279. }catch (ClassNotFoundException | SQLException err) {
  280. err.printStackTrace();
  281. }
  282. }
  283. return DEATH;
  284. }
  285.  
  286. public void sendTimePlayed(HashMap<UUID,Integer> map ){
  287. for(UUID id : map.keySet()){
  288. try {
  289. statement().executeUpdate("update Skywars_stats set TIME_PLAYED=TIME_PLAYED+"+map.get(id)+" where UUID='"+id+"'");
  290. } catch (SQLException e) {
  291. try{
  292. close();
  293. connectTo();
  294. }
  295. catch(ClassNotFoundException| SQLException e1){
  296. e1.printStackTrace();
  297. }
  298. }
  299. }
  300. }
  301.  
  302. public void createStats(UUID playerID){
  303. try {
  304. statement().execute("insert into Skywars_stats values('"+ playerID.toString()+"',0,0,0,0,0,0,0)");
  305. } catch (SQLException e) {
  306. try{
  307. close();
  308. connectTo();
  309. }
  310. catch(ClassNotFoundException| SQLException e1){
  311. e1.printStackTrace();
  312. }
  313. }
  314. }
  315.  
  316.  
  317.  
  318. public boolean hasAccount(UUID playerID){
  319.  
  320. boolean exist=false;
  321. try {
  322. result = statement.executeQuery("select TIME_PLAYED from Skywars_stats where UUID='"+playerID+"'");
  323. if(result.next()){
  324. exist=true;
  325. }
  326. } catch (SQLException e) {
  327. try{
  328. close();
  329. connectTo();
  330. }
  331. catch(ClassNotFoundException| SQLException e1){
  332. e1.printStackTrace();
  333. }
  334. }
  335. return exist;
  336. }
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement