Advertisement
Guest User

MYSQL

a guest
Dec 18th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. ####MYSQL:
  2.  
  3. package br.com.battlebits.ybattlecraft.mysql;
  4.  
  5. import br.com.battlebits.ybattlecraft.yBattleCraft;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.logging.Logger;
  11.  
  12. public class Connect
  13. {
  14. private yBattleCraft m;
  15.  
  16. public Connect(yBattleCraft m)
  17. {
  18. this.m = m;
  19. }
  20.  
  21. public synchronized Connection trySQLConnection()
  22. {
  23. if (!this.m.sql)
  24. {
  25. this.m.getLogger().info("MySQL Desativado!");
  26. return null;
  27. }
  28. try
  29. {
  30. this.m.getLogger().info("Conectando ao MySQL");
  31. Class.forName("com.mysql.jdbc.Driver").newInstance();
  32. String conn = "jdbc:mysql://" + this.m.host + ":" + this.m.port + "/status";
  33. this.m.mainConnection = DriverManager.getConnection(conn, this.m.user, this.m.password);
  34. return this.m.mainConnection;
  35. }
  36. catch (ClassNotFoundException ex)
  37. {
  38. this.m.getLogger().warning("MySQL Driver nao encontrado!");
  39. this.m.sql = false;
  40. }
  41. catch (SQLException ex)
  42. {
  43. this.m.getLogger().warning("Erro enquanto tentava conectar ao Mysql!");
  44. this.m.sql = false;
  45. }
  46. catch (Exception ex)
  47. {
  48. this.m.getLogger().warning("Erro desconhecido enquanto tentava conectar ao MySQL.");
  49. this.m.sql = false;
  50. }
  51. return null;
  52. }
  53.  
  54. public void prepareSQL(Connection con)
  55. {
  56. if (this.m.sql)
  57. {
  58. SQLQuery("CREATE TABLE IF NOT EXISTS `Status` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Uuid` varchar(255) NOT NULL, `Kills` int(10), `Deaths` int(10), `Killstreak` int(10), `Money` int(10), PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;", con);
  59. SQLQuery("CREATE TABLE IF NOT EXISTS `Kits` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `PlayerName` varchar(255) NOT NULL, `KitName` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;", con);
  60. SQLQuery("CREATE TABLE IF NOT EXISTS `KitFavorito` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Uuid` varchar(255) NOT NULL, `Kits` text NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;", con);
  61. this.m.getLogger().info("Criando Tabelas no SQL");
  62. }
  63. }
  64.  
  65. public static void SQLdisconnect(Connection con)
  66. {
  67. try
  68. {
  69. if ((con != null) && (!con.isClosed())) {
  70. con.close();
  71. }
  72. }
  73. catch (SQLException e)
  74. {
  75. e.printStackTrace();
  76. }
  77. }
  78.  
  79. public synchronized void SQLQuery(String sql, Connection con)
  80. {
  81. if (!this.m.sql) {
  82. return;
  83. }
  84. try
  85. {
  86. Statement stmt = con.createStatement();
  87. stmt.executeUpdate(sql);
  88. stmt.close();
  89. }
  90. catch (SQLException e)
  91. {
  92. this.m.getLogger().info("Erro ao tentar executar Query");
  93. this.m.getLogger().info(e.getMessage());
  94. }
  95. }
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. ####CARREGANDO:
  105. private void loadStatus(UUID uuid)
  106. throws SQLException
  107. {
  108. if (!this.m.sql) {
  109. return;
  110. }
  111. String sql = "SELECT * FROM `Kits` WHERE (`Uuid` = '" + uuid.toString().replace("-", "") + "');";
  112. if (this.m.mainConnection.isClosed()) {
  113. this.m.connect.trySQLConnection();
  114. }
  115. PreparedStatement stmt = this.m.mainConnection.prepareStatement(sql);
  116. ResultSet result = stmt.executeQuery();
  117. List<String> kitList = new ArrayList();
  118. while (result.next()) {
  119. kitList.add(result.getString("KitName"));
  120. }
  121. sql = "SELECT * FROM `Status` WHERE (`Uuid` = '" + uuid.toString().replace("-", "") + "');";
  122. result.close();
  123. stmt.close();
  124. stmt = this.m.mainConnection.prepareStatement(sql);
  125. result = stmt.executeQuery();
  126. int kills = 0;
  127. int deaths = 0;
  128. int killstreak = 0;
  129. if (result.next())
  130. {
  131. kills = result.getInt("Kills");
  132. deaths = result.getInt("Deaths");
  133. killstreak = result.getInt("Killstreak");
  134. }
  135. result.close();
  136. stmt.close();
  137. sql = "SELECT * FROM `KitFavorito` WHERE (`Uuid` = '" + uuid.toString().replace("-", "") + "');";
  138. stmt = this.m.mainConnection.prepareStatement(sql);
  139. result = stmt.executeQuery();
  140. List<String> kitsFavoritos = new ArrayList();
  141. if (result.next())
  142. {
  143. String kits = result.getString("Kits");
  144. if (kits.contains(","))
  145. {
  146. String[] arrayOfString;
  147. int j = (arrayOfString = kits.split(",")).length;
  148. for (int i = 0; i < j; i++)
  149. {
  150. String str = arrayOfString[i];
  151. kitsFavoritos.add(str);
  152. }
  153. }
  154. else
  155. {
  156. kitsFavoritos.add(kits);
  157. }
  158. }
  159. this.m.getStatusManager().addPlayer(uuid, kills, deaths, killstreak, kitList, kitsFavoritos);
  160. stmt.close();
  161. result.close();
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement