Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. package com.massivecraft.factions;
  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.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Map.Entry;
  11.  
  12. import org.bukkit.Bukkit;
  13.  
  14.  
  15. public class SqlConnection
  16. {
  17. static List<String> list = new ArrayList<>();
  18. private static Connection connection;
  19. private String urlbase,host,base,user,pass;
  20. public SqlConnection(String urlbase, String host, String base, String user, String pass)
  21. {
  22. this.urlbase = urlbase;
  23. this.host = host;
  24. this.base = base;
  25. this.user = user;
  26. this.pass = pass;
  27. }
  28. public void connection()
  29. {
  30. try {
  31. connection = DriverManager.getConnection(urlbase + host + "/" + base, user, pass);
  32. } catch (SQLException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. }
  36. }
  37. public void disconnect()
  38. {
  39. if(isConnected())
  40. {
  41. try {
  42. connection.close();
  43. } catch (SQLException e) {
  44. // TODO Auto-generated catch block
  45. e.printStackTrace();
  46. }
  47. }
  48. }
  49. public static boolean isConnected()
  50. {
  51. return connection != null;
  52. }
  53. public static void updateBase()
  54. {
  55. if(isConnected())
  56. {
  57. try {
  58. PreparedStatement q2 = connection.prepareStatement("SELECT nom FROM fclassement");
  59. ResultSet res = q2.executeQuery();
  60. while(res.next())
  61. {
  62. list.add(res.getString(1));
  63. }
  64. q2.close();
  65. } catch (SQLException e1) {
  66. // TODO Auto-generated catch block
  67. e1.printStackTrace();
  68. }
  69.  
  70. for(Entry<String, Object> s : FClassementSort.section.getValues(false).entrySet())
  71. {
  72. if(s.getKey().equalsIgnoreCase("�eNature") || s.getKey().equalsIgnoreCase("WarZone") || s.getKey().equalsIgnoreCase("SafeZone") || s.getKey().equalsIgnoreCase("Staff") || s.getKey().equalsIgnoreCase("Staff2"))
  73. continue;
  74. if(!factionExists(s.getKey()))
  75. {
  76. if(Factions.i.getByTag(s.getKey()) != null)
  77. {
  78. try {
  79. PreparedStatement q = connection.prepareStatement("INSERT INTO fclassement(nom,joueurs,claims,kills,morts,money,totem,koth,lastsurvive,points) VALUES (?,?,?,?,?,?,?,?,?,?)");
  80. q.setString(1, s.getKey());
  81. q.setInt(2, Factions.i.getByTag(s.getKey()).getFPlayers().size());
  82. q.setInt(3, Factions.i.getByTag(s.getKey()).getLandRounded());
  83. int kills = 0;
  84. for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
  85. {
  86. kills += P.p.classementConfig.getInt("classement-faction-joueurs."+fp.getName()+".kill");
  87. }
  88. q.setInt(4, kills);
  89. int morts = 0;
  90. for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
  91. {
  92. morts += P.p.classementConfig.getInt("classement-faction-joueurs."+fp.getName()+".death");
  93. }
  94. q.setInt(5, morts);
  95. int money = 0;
  96. for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
  97. {
  98. money += P.economy.getBalance(fp.getName());
  99. }
  100. q.setInt(6, money);
  101. q.setInt(7, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.totem"));
  102. q.setInt(8, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.koth"));
  103. q.setInt(9, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.lastsurvive"));
  104. q.setInt(10, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".point"));
  105. q.execute();
  106. q.close();
  107. } catch (SQLException e) {
  108. // TODO Auto-generated catch block
  109. e.printStackTrace();
  110. }
  111. }
  112. else
  113. {
  114. P.p.classementConfig.set("classement-faction-factions."+s.getKey(), null);
  115. FClassementSort.section.set(s.getKey(), null);
  116. P.p.classementConfig.set("classement-faction-factions-entry."+s.getKey(), null);
  117. P.p.saveLaConfig();
  118. }
  119. }
  120. else
  121. {
  122. if(Factions.i.getByTag(s.getKey()) != null)
  123. {
  124. try {
  125. PreparedStatement q = connection.prepareStatement("UPDATE fclassement SET joueurs = ?, claims = ?, kills = ?, morts = ?, money = ?, totem = ?, koth = ?, lastsurvive = ?, points = ? WHERE nom = ?");
  126. q.setInt(1, Factions.i.getByTag(s.getKey()).getFPlayers().size());
  127. q.setInt(2, Factions.i.getByTag(s.getKey()).getLandRounded());
  128. int kills = 0;
  129. for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
  130. {
  131. kills += P.p.classementConfig.getInt("classement-faction-joueurs."+fp.getName()+".kill");
  132. }
  133. q.setInt(3, kills);
  134. int morts = 0;
  135. for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
  136. {
  137. morts += P.p.classementConfig.getInt("classement-faction-joueurs."+fp.getName()+".death");
  138. }
  139. q.setInt(4, morts);
  140. int money = 0;
  141. for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
  142. {
  143. money += P.p.economy.getBalance(fp.getName());
  144. }
  145. q.setInt(5, money);
  146. q.setInt(6, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.totem"));
  147. q.setInt(7, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.koth"));
  148. q.setInt(8, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.lastsurvive"));
  149. q.setInt(9, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".point"));
  150. q.setString(10, s.getKey());
  151. q.executeUpdate();
  152. q.close();
  153. } catch (SQLException e) {
  154. // TODO Auto-generated catch block
  155. e.printStackTrace();
  156. }
  157. }
  158. else
  159. {
  160. try {
  161. PreparedStatement q = connection.prepareStatement("DELETE FROM fclassement WHERE nom = ?");
  162. q.setString(1, s.getKey());
  163. q.executeUpdate();
  164. q.close();
  165. } catch (SQLException e) {
  166. // TODO Auto-generated catch block
  167. e.printStackTrace();
  168. }
  169. P.p.classementConfig.set("classement-faction-factions."+s.getKey(), null);
  170. FClassementSort.section.set(s.getKey(), null);
  171. P.p.classementConfig.set("classement-faction-factions-entry."+s.getKey(), null);
  172. P.p.saveLaConfig();
  173. }
  174. }
  175. }
  176. for(String s : list)
  177. {
  178. if(!FClassementSort.section.getValues(false).containsKey(s))
  179. {
  180. try {
  181. PreparedStatement q = connection.prepareStatement("DELETE FROM fclassement WHERE nom = ?");
  182. q.setString(1, s);
  183. q.executeUpdate();
  184. q.close();
  185. } catch (SQLException e) {
  186. // TODO Auto-generated catch block
  187. e.printStackTrace();
  188. }
  189. }
  190. }
  191. }
  192. }
  193. public static boolean factionExists(String f)
  194. {
  195. try {
  196. PreparedStatement q = connection.prepareStatement("SELECT nom FROM fclassement WHERE nom = ?");
  197. q.setString(1, f);
  198. ResultSet res = q.executeQuery();
  199. boolean factionExists = res.next();
  200. q.close();
  201. return factionExists;
  202.  
  203. } catch (SQLException e) {
  204. // TODO Auto-generated catch block
  205. e.printStackTrace();
  206. }
  207. return false;
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement