Advertisement
Guest User

Untitled

a guest
Nov 15th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. package dev.thoneick.clan.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. import java.util.concurrent.locks.ReentrantLock;
  10.  
  11. import dev.thoneick.clan.Main;
  12.  
  13. public class Connect
  14. {
  15. private Main m;
  16. public static ReentrantLock lock = new ReentrantLock(true);
  17.  
  18.  
  19. public Connect(Main m)
  20. {
  21. this.m = m;
  22. }
  23.  
  24. public synchronized Connection trySQLConnection()
  25. {
  26.  
  27. if (!this.m.sql)
  28. {
  29. this.m.getLogger().info("MySQL Desativado!");
  30. return null;
  31. }
  32. try
  33. {
  34. Class.forName("com.mysql.jdbc.Driver").newInstance();
  35. String conn = "jdbc:mysql://" + "localhost" + ":" + 3306 + "/" + "status";
  36. return DriverManager.getConnection(conn, "root", "");
  37. }
  38. catch (ClassNotFoundException ex)
  39. {
  40. this.m.getLogger().warning("MySQL Driver nao encontrado!");
  41. this.m.sql = false;
  42. }
  43. catch (SQLException ex)
  44. {
  45. this.m.getLogger().warning("Erro enquanto tentava conectar ao Mysql!");
  46. ex.printStackTrace();
  47. this.m.sql = false;//vo achar alguem
  48. }
  49. catch (Exception ex)
  50. {
  51. this.m.getLogger().warning("Erro desconhecido enquanto tentava conectar ao MySQL.");
  52. this.m.sql = false;
  53. }
  54. return null;
  55. }
  56.  
  57. public void prepareSQL(Connection con)
  58. {
  59. if (this.m.sql)
  60. {
  61.  
  62. SQLQuerySync("CREATE TABLE IF NOT EXISTS `clans` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `tag` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
  63. SQLQuerySync("CREATE TABLE IF NOT EXISTS `clans_players` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `uuid` varchar(255) NOT NULL, `rank` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
  64. SQLQuerySync("CREATE TABLE IF NOT EXISTS `clans_status` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `kills` int(10), `deaths` int(10), `elo` int(10), PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
  65. SQLQuerySync("CREATE TABLE IF NOT EXISTS `uuidfetcher` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `uuid` varchar(255) NOT NULL, `name` varchar(255) NOT NULL, `lastip` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
  66. this.m.getLogger().info("Criando Tabelas no SQL");
  67. }
  68. }
  69.  
  70. public static void SQLdisconnect(Connection con)
  71. {
  72. try
  73. {
  74. if ((con != null) && (!con.isClosed())) {
  75. con.close();
  76. }
  77. }
  78. catch (SQLException e)
  79. {
  80. e.printStackTrace();
  81. }
  82.  
  83. }
  84.  
  85. public void SQLdisconnect()
  86. {
  87. try
  88. {
  89. if ((m.mainConnection != null) && (!m.mainConnection.isClosed())) {
  90. m.mainConnection.close();
  91. }
  92. }
  93. catch (SQLException e)
  94. {
  95. e.printStackTrace();
  96. }
  97. }
  98.  
  99. public void SQLQuery(final String sql)
  100. {
  101. if (!this.m.sql) {
  102. return;
  103. }
  104. ExecutorService executor = Executors.newCachedThreadPool();
  105. executor.execute(new Thread(new Runnable()
  106. {
  107. public void run()
  108. {
  109. Connect.lock.lock();
  110. try
  111. {
  112. Statement stmt = m.mainConnection.createStatement();
  113. stmt.executeUpdate(sql);
  114. stmt.close();
  115. }
  116. catch (SQLException e)
  117. {
  118. Connect.this.m.getLogger().info("Erro ao tentar executar Query");
  119. Connect.this.m.getLogger().info(sql);
  120. Connect.this.m.getLogger().info(e.getMessage());
  121. }
  122. Connect.lock.unlock();
  123. }
  124. }));
  125. executor.shutdown();
  126. }
  127.  
  128. public void SQLQuerySync(String sql)
  129. {
  130. if (!this.m.sql) {
  131. return;
  132. }
  133. try
  134. {
  135. Statement stmt = m.mainConnection.createStatement();
  136. stmt.executeUpdate(sql);
  137. stmt.close();
  138. }
  139. catch (SQLException e)
  140. {
  141. this.m.getLogger().info("Erro ao tentar executar Query");
  142. this.m.getLogger().info(sql);
  143. this.m.getLogger().info(e.getMessage());
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement