Advertisement
Guest User

Untitled

a guest
May 22nd, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.35 KB | None | 0 0
  1. package net.zPoxi_.combatapi.sql;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.UUID;
  11.  
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.configuration.file.FileConfiguration;
  15. import org.bukkit.configuration.file.YamlConfiguration;
  16. import org.bukkit.scheduler.BukkitRunnable;
  17.  
  18. import net.zPoxi_.combatapi.Main;
  19. import net.zPoxi_.combatapi.api.Message;
  20.  
  21. public class MySQL
  22. {
  23. public static boolean ativo;
  24. private static FileConfiguration dbConfigFile;
  25. private String ip;
  26. private int porta;
  27. private String usuario;
  28. private String senha;
  29. private String banco;
  30. private Connection connection;
  31.  
  32. static {
  33. MySQL.ativo = false;
  34. }
  35.  
  36. @SuppressWarnings("unused")
  37. public MySQL() throws Exception {
  38. final File file = new File("plugins/CombatAPIsql/", "banco.yml");
  39. final FileConfiguration cfg = MySQL.dbConfigFile = (FileConfiguration)YamlConfiguration.loadConfiguration(file);
  40. final String db = "bancodedados.";
  41. cfg.addDefault("bancodedados.ativo", "true");
  42. cfg.addDefault("bancodedados.ip", "yourip");
  43. cfg.addDefault("bancodedados.porta", 3306);
  44. cfg.addDefault("bancodedados.usuario", "mc");
  45. cfg.addDefault("bancodedados.senha", "senpja");
  46. cfg.addDefault("bancodedados.banco", "mc");
  47. cfg.options().copyDefaults(true);
  48. try {
  49. cfg.save(file);
  50. }
  51. catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. if (isAtivo()) {
  55. MySQL.ativo = true;
  56. }
  57. if (MySQL.ativo) {
  58. this.ip = cfg.getString("bancodedados.ip");
  59. this.porta = cfg.getInt("bancodedados.porta");
  60. this.usuario = cfg.getString("bancodedados.usuario");
  61. this.senha = cfg.getString("bancodedados.senha");
  62. this.banco = cfg.getString("bancodedados.banco");
  63. this.openConnection();
  64. }
  65. }
  66.  
  67. public static boolean isAtivo() {
  68. return !MySQL.dbConfigFile.getString("bancodedados.ativo").equalsIgnoreCase("false");
  69. }
  70.  
  71. public static void sqlConnect() {
  72. try {
  73. Main.setSQL(new MySQL());
  74. if (MySQL.ativo) {
  75. Message.EOQ.send((CommandSender)Bukkit.getConsoleSender(), "Conectado ao Banco de Dados com sucesso!");
  76. }
  77. else {
  78. Message.INFO.send((CommandSender)Bukkit.getConsoleSender(), "Uso do Banco de Dados desligado, desativando MySQL!");
  79. }
  80. }
  81. catch (Exception ex) {
  82. Message.ERROR.send((CommandSender)Bukkit.getConsoleSender(), "Erro ao conectar ao Banco de Dados");
  83. MySQL.ativo = false;
  84. ex.printStackTrace();
  85. }
  86. }
  87.  
  88. public String getData(String data) {
  89. if (!ativo) {
  90. return " ";
  91. }
  92. try
  93. {
  94. PreparedStatement ps = Main.getSQL().getConnection().prepareStatement("SELECT " + data + " FROM combat_data;");
  95. ResultSet rs = ps.executeQuery();
  96. if (rs.next()) {
  97. return rs.getString(data);
  98. }
  99. rs.close();
  100. ps.close();
  101. } catch (SQLException e1) {
  102. e1.printStackTrace();
  103. try {
  104. Main.getSQL().openConnection();
  105. } catch (Exception e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. return " ";
  110. }
  111.  
  112. public static void addPlayerToTables(UUID id, String table, String[] columns, String[] valuesToInsert, boolean useAI) {
  113. new BukkitRunnable() {
  114. public void run() {
  115. if (!MySQL.ativo) {
  116. return;
  117. }
  118. try {
  119. final PreparedStatement ps = Main.getSQL().getConnection().prepareStatement("SELECT uuid FROM " + table + " WHERE uuid='" + id + "'");
  120. final ResultSet rs = ps.executeQuery();
  121. if (!rs.next()) {
  122. String colunas = (useAI ? "id, " : "") + "uuid, ";
  123. String values = (useAI ? "'0', " : "") + id + ", ";
  124. for (int i = 0; i < valuesToInsert.length; ++i) {
  125. String virgula = "";
  126. if (i < valuesToInsert.length - 1) {
  127. virgula = ", ";
  128. }
  129. colunas = colunas + columns[i] + virgula;
  130. values = values + "'" + valuesToInsert[i] + "'" + virgula;
  131. }
  132. final PreparedStatement ps2 = Main.getSQL().getConnection().prepareStatement("INSERT INTO " + table + " (" + colunas + ") VALUES(" + values + ")");
  133. ps2.executeUpdate();
  134. ps2.close();
  135. }
  136. rs.close();
  137. ps.close();
  138. }
  139. catch (SQLException e1) {
  140. e1.printStackTrace();
  141. try {
  142. Main.getSQL().openConnection();
  143. }
  144. catch (Exception e2) {
  145. e2.printStackTrace();
  146. }
  147. }
  148. }
  149. }.runTaskAsynchronously(Main.getPlugin());
  150. }
  151.  
  152. public static boolean addPlayerToTable(UUID id, String table, String[] valuesToInsert, boolean useAI, boolean checkIfNotExists)
  153. {
  154. if (!ativo) {
  155. return false;
  156. }
  157. try
  158. {
  159. PreparedStatement ps = Main.getSQL().getConnection().prepareStatement("SELECT uuid FROM " + table + " WHERE uuid='" + id + "'");
  160.  
  161. ResultSet rs = ps.executeQuery();
  162. if ((!checkIfNotExists) || (!rs.next())) {
  163. String values = (useAI ? "'0', " : "") + "'" + id + "', ";
  164. for (int i = 0; i < valuesToInsert.length; i++) {
  165. String virgula = "";
  166. if (i < valuesToInsert.length - 1) {
  167. virgula = ", ";
  168. }
  169. values = values + "'" + valuesToInsert[i] + "'" + virgula;
  170. }
  171.  
  172. PreparedStatement ps1 = Main.getSQL().getConnection().prepareStatement("INSERT INTO " + table + " VALUES(" + values + ")");
  173. ps1.executeUpdate();
  174. ps1.close();
  175. rs.close();
  176. ps.close();
  177. return true;
  178. }
  179. rs.close();
  180. ps.close();
  181. return false;
  182. } catch (SQLException e1) {
  183. e1.printStackTrace();
  184. try {
  185. Main.getSQL().openConnection();
  186. } catch (Exception e) {
  187. e.printStackTrace();
  188. }
  189. }
  190. return false;
  191. }
  192.  
  193. public Connection openConnection() throws Exception {
  194. Class.forName("com.mysql.jdbc.Driver");
  195. final Connection connection = DriverManager.getConnection("jdbc:mysql://" + this.ip + ":" + this.porta + "/" + this.banco, this.usuario, this.senha);
  196. return this.connection = connection;
  197. }
  198.  
  199. public Connection getConnection() {
  200. return this.connection;
  201. }
  202.  
  203. public boolean hasConnection() {
  204. try {
  205. return this.connection != null || this.connection.isValid(1);
  206. }
  207. catch (SQLException e) {
  208. return false;
  209. }
  210. }
  211.  
  212. public void queryUpdate(final String query) {
  213. new BukkitRunnable() {
  214. public void run() {
  215. final Connection conn = Main.getSQL().getConnection();
  216. PreparedStatement st = null;
  217. try {
  218. st = conn.prepareStatement(query);
  219. st.executeUpdate();
  220. st.close();
  221. }
  222. catch (SQLException e1) {
  223. e1.printStackTrace();
  224. try {
  225. if (Main.getSQL().hasConnection()) {
  226. Main.getSQL().openConnection();
  227. }
  228. }
  229. catch (Exception e2) {
  230. e2.printStackTrace();
  231. }
  232. return;
  233. }
  234. finally {
  235. Main.getSQL().closeResources(null, st);
  236. }
  237. Main.getSQL().closeResources(null, st);
  238. }
  239. }.runTaskAsynchronously(Main.getPlugin());
  240. }
  241.  
  242. public void closeResources(final ResultSet rs, final PreparedStatement st) {
  243. if (rs != null) {
  244. try {
  245. rs.close();
  246. }
  247. catch (SQLException ex) {}
  248. }
  249. if (st != null) {
  250. try {
  251. st.close();
  252. }
  253. catch (SQLException ex2) {}
  254. }
  255. }
  256.  
  257. public void closeConnection() {
  258. try {
  259. if (!MySQL.ativo) {
  260. return;
  261. }
  262. if (this.connection.isClosed()) {
  263. return;
  264. }
  265. this.connection.close();
  266. }
  267. catch (SQLException ex) {
  268. return;
  269. }
  270. finally {
  271. this.connection = null;
  272. }
  273. this.connection = null;
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement