Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####MYSQL:
- package br.com.battlebits.ybattlecraft.mysql;
- import br.com.battlebits.ybattlecraft.yBattleCraft;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Logger;
- public class Connect
- {
- private yBattleCraft m;
- public Connect(yBattleCraft m)
- {
- this.m = m;
- }
- public synchronized Connection trySQLConnection()
- {
- if (!this.m.sql)
- {
- this.m.getLogger().info("MySQL Desativado!");
- return null;
- }
- try
- {
- this.m.getLogger().info("Conectando ao MySQL");
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String conn = "jdbc:mysql://" + this.m.host + ":" + this.m.port + "/status";
- this.m.mainConnection = DriverManager.getConnection(conn, this.m.user, this.m.password);
- return this.m.mainConnection;
- }
- catch (ClassNotFoundException ex)
- {
- this.m.getLogger().warning("MySQL Driver nao encontrado!");
- this.m.sql = false;
- }
- catch (SQLException ex)
- {
- this.m.getLogger().warning("Erro enquanto tentava conectar ao Mysql!");
- this.m.sql = false;
- }
- catch (Exception ex)
- {
- this.m.getLogger().warning("Erro desconhecido enquanto tentava conectar ao MySQL.");
- this.m.sql = false;
- }
- return null;
- }
- public void prepareSQL(Connection con)
- {
- if (this.m.sql)
- {
- 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);
- 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);
- 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);
- this.m.getLogger().info("Criando Tabelas no SQL");
- }
- }
- public static void SQLdisconnect(Connection con)
- {
- try
- {
- if ((con != null) && (!con.isClosed())) {
- con.close();
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public synchronized void SQLQuery(String sql, Connection con)
- {
- if (!this.m.sql) {
- return;
- }
- try
- {
- Statement stmt = con.createStatement();
- stmt.executeUpdate(sql);
- stmt.close();
- }
- catch (SQLException e)
- {
- this.m.getLogger().info("Erro ao tentar executar Query");
- this.m.getLogger().info(e.getMessage());
- }
- }
- }
- ####CARREGANDO:
- private void loadStatus(UUID uuid)
- throws SQLException
- {
- if (!this.m.sql) {
- return;
- }
- String sql = "SELECT * FROM `Kits` WHERE (`Uuid` = '" + uuid.toString().replace("-", "") + "');";
- if (this.m.mainConnection.isClosed()) {
- this.m.connect.trySQLConnection();
- }
- PreparedStatement stmt = this.m.mainConnection.prepareStatement(sql);
- ResultSet result = stmt.executeQuery();
- List<String> kitList = new ArrayList();
- while (result.next()) {
- kitList.add(result.getString("KitName"));
- }
- sql = "SELECT * FROM `Status` WHERE (`Uuid` = '" + uuid.toString().replace("-", "") + "');";
- result.close();
- stmt.close();
- stmt = this.m.mainConnection.prepareStatement(sql);
- result = stmt.executeQuery();
- int kills = 0;
- int deaths = 0;
- int killstreak = 0;
- if (result.next())
- {
- kills = result.getInt("Kills");
- deaths = result.getInt("Deaths");
- killstreak = result.getInt("Killstreak");
- }
- result.close();
- stmt.close();
- sql = "SELECT * FROM `KitFavorito` WHERE (`Uuid` = '" + uuid.toString().replace("-", "") + "');";
- stmt = this.m.mainConnection.prepareStatement(sql);
- result = stmt.executeQuery();
- List<String> kitsFavoritos = new ArrayList();
- if (result.next())
- {
- String kits = result.getString("Kits");
- if (kits.contains(","))
- {
- String[] arrayOfString;
- int j = (arrayOfString = kits.split(",")).length;
- for (int i = 0; i < j; i++)
- {
- String str = arrayOfString[i];
- kitsFavoritos.add(str);
- }
- }
- else
- {
- kitsFavoritos.add(kits);
- }
- }
- this.m.getStatusManager().addPlayer(uuid, kills, deaths, killstreak, kitList, kitsFavoritos);
- stmt.close();
- result.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement