Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. package com.server.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9.  
  10. import org.bukkit.Bukkit;
  11.  
  12. import com.server.Main;
  13.  
  14. public class MysqlConnection {
  15. private Connection _con;
  16.  
  17. public MysqlConnection() {
  18. }
  19.  
  20. public synchronized void trySQLConnection() {
  21. try {
  22. Main.getInstance().getLogger().info("Conectando ao Banco de dados...");
  23. Class.forName("com.mysql.jdbc.Driver").newInstance();
  24. String conn = "jdbc:mysql://" + "localhost" + ":" + 3306 + "/" + "kitpvp"
  25. + "?autoReconnect=true&failOverReadOnly=false&maxReconnects=2147483647";
  26. this._con = DriverManager.getConnection(conn, "root", "");
  27.  
  28. } catch (ClassNotFoundException ex) {
  29. Main.getInstance().getLogger().warning("MySQL Driver nao encontrado!");
  30. Bukkit.shutdown();
  31. } catch (SQLException ex) {
  32. Main.getInstance().getLogger().warning("Erro enquanto tentava conectar ao Mysql!");
  33. Bukkit.shutdown();
  34. } catch (Exception ex) {
  35. Main.getInstance().getLogger().warning("Erro desconhecido enquanto tentava conectar ao MySQL.");
  36. Bukkit.shutdown();
  37. }
  38. }
  39.  
  40. public Connection getConnection() {
  41. return this._con;
  42. }
  43.  
  44. public void SQLdisconnect() {
  45. try {
  46. if ((this._con != null) && (!this._con.isClosed())) {
  47. this._con.close();
  48. }
  49. } catch (SQLException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54. public void SQLQuery(final String sql) {
  55. ExecutorService executor = Executors.newCachedThreadPool();
  56. executor.execute(new Thread(new Runnable() {
  57. public void run() {
  58. try {
  59. Statement stmt = _con.createStatement();
  60. stmt.executeUpdate(sql);
  61. stmt.close();
  62. } catch (SQLException e) {
  63. Main.getInstance().getLogger().info("Erro ao tentar executar Query");
  64. Main.getInstance().getLogger().info(sql);
  65. Main.getInstance().getLogger().info(e.getMessage());
  66. }
  67. }
  68. }));
  69. executor.shutdown();
  70. }
  71.  
  72. public void SQLQuerySync(String sql) {
  73. try {
  74. Statement stmt = _con.createStatement();
  75. stmt.executeUpdate(sql);
  76. stmt.close();
  77. } catch (SQLException e) {
  78. Main.getInstance().getLogger().info("Erro ao tentar executar Query");
  79. Main.getInstance().getLogger().info(sql);
  80. Main.getInstance().getLogger().info(e.getMessage());
  81. }
  82. }
  83.  
  84. public void SQLQuerySyncNoLock(String sql) {
  85. try {
  86. Statement stmt = _con.createStatement();
  87. stmt.executeUpdate(sql);
  88. stmt.close();
  89. } catch (SQLException e) {
  90. Main.getInstance().getLogger().info("Erro ao tentar executar Query");
  91. Main.getInstance().getLogger().info(sql);
  92. Main.getInstance().getLogger().info(e.getMessage());
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement