Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.zPoxi_.combatapi.sql;
- import java.io.File;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.scheduler.BukkitRunnable;
- import net.zPoxi_.combatapi.Main;
- import net.zPoxi_.combatapi.api.Message;
- public class MySQL
- {
- public static boolean ativo;
- private static FileConfiguration dbConfigFile;
- private String ip;
- private int porta;
- private String usuario;
- private String senha;
- private String banco;
- private Connection connection;
- static {
- MySQL.ativo = false;
- }
- @SuppressWarnings("unused")
- public MySQL() throws Exception {
- final File file = new File("plugins/CombatAPIsql/", "banco.yml");
- final FileConfiguration cfg = MySQL.dbConfigFile = (FileConfiguration)YamlConfiguration.loadConfiguration(file);
- final String db = "bancodedados.";
- cfg.addDefault("bancodedados.ativo", "true");
- cfg.addDefault("bancodedados.ip", "yourip");
- cfg.addDefault("bancodedados.porta", 3306);
- cfg.addDefault("bancodedados.usuario", "mc");
- cfg.addDefault("bancodedados.senha", "senpja");
- cfg.addDefault("bancodedados.banco", "mc");
- cfg.options().copyDefaults(true);
- try {
- cfg.save(file);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- if (isAtivo()) {
- MySQL.ativo = true;
- }
- if (MySQL.ativo) {
- this.ip = cfg.getString("bancodedados.ip");
- this.porta = cfg.getInt("bancodedados.porta");
- this.usuario = cfg.getString("bancodedados.usuario");
- this.senha = cfg.getString("bancodedados.senha");
- this.banco = cfg.getString("bancodedados.banco");
- this.openConnection();
- }
- }
- public static boolean isAtivo() {
- return !MySQL.dbConfigFile.getString("bancodedados.ativo").equalsIgnoreCase("false");
- }
- public static void sqlConnect() {
- try {
- Main.setSQL(new MySQL());
- if (MySQL.ativo) {
- Message.EOQ.send((CommandSender)Bukkit.getConsoleSender(), "Conectado ao Banco de Dados com sucesso!");
- }
- else {
- Message.INFO.send((CommandSender)Bukkit.getConsoleSender(), "Uso do Banco de Dados desligado, desativando MySQL!");
- }
- }
- catch (Exception ex) {
- Message.ERROR.send((CommandSender)Bukkit.getConsoleSender(), "Erro ao conectar ao Banco de Dados");
- MySQL.ativo = false;
- ex.printStackTrace();
- }
- }
- public String getData(String data) {
- if (!ativo) {
- return " ";
- }
- try
- {
- PreparedStatement ps = Main.getSQL().getConnection().prepareStatement("SELECT " + data + " FROM combat_data;");
- ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- return rs.getString(data);
- }
- rs.close();
- ps.close();
- } catch (SQLException e1) {
- e1.printStackTrace();
- try {
- Main.getSQL().openConnection();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return " ";
- }
- public static void addPlayerToTables(UUID id, String table, String[] columns, String[] valuesToInsert, boolean useAI) {
- new BukkitRunnable() {
- public void run() {
- if (!MySQL.ativo) {
- return;
- }
- try {
- final PreparedStatement ps = Main.getSQL().getConnection().prepareStatement("SELECT uuid FROM " + table + " WHERE uuid='" + id + "'");
- final ResultSet rs = ps.executeQuery();
- if (!rs.next()) {
- String colunas = (useAI ? "id, " : "") + "uuid, ";
- String values = (useAI ? "'0', " : "") + id + ", ";
- for (int i = 0; i < valuesToInsert.length; ++i) {
- String virgula = "";
- if (i < valuesToInsert.length - 1) {
- virgula = ", ";
- }
- colunas = colunas + columns[i] + virgula;
- values = values + "'" + valuesToInsert[i] + "'" + virgula;
- }
- final PreparedStatement ps2 = Main.getSQL().getConnection().prepareStatement("INSERT INTO " + table + " (" + colunas + ") VALUES(" + values + ")");
- ps2.executeUpdate();
- ps2.close();
- }
- rs.close();
- ps.close();
- }
- catch (SQLException e1) {
- e1.printStackTrace();
- try {
- Main.getSQL().openConnection();
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- }.runTaskAsynchronously(Main.getPlugin());
- }
- public static boolean addPlayerToTable(UUID id, String table, String[] valuesToInsert, boolean useAI, boolean checkIfNotExists)
- {
- if (!ativo) {
- return false;
- }
- try
- {
- PreparedStatement ps = Main.getSQL().getConnection().prepareStatement("SELECT uuid FROM " + table + " WHERE uuid='" + id + "'");
- ResultSet rs = ps.executeQuery();
- if ((!checkIfNotExists) || (!rs.next())) {
- String values = (useAI ? "'0', " : "") + "'" + id + "', ";
- for (int i = 0; i < valuesToInsert.length; i++) {
- String virgula = "";
- if (i < valuesToInsert.length - 1) {
- virgula = ", ";
- }
- values = values + "'" + valuesToInsert[i] + "'" + virgula;
- }
- PreparedStatement ps1 = Main.getSQL().getConnection().prepareStatement("INSERT INTO " + table + " VALUES(" + values + ")");
- ps1.executeUpdate();
- ps1.close();
- rs.close();
- ps.close();
- return true;
- }
- rs.close();
- ps.close();
- return false;
- } catch (SQLException e1) {
- e1.printStackTrace();
- try {
- Main.getSQL().openConnection();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return false;
- }
- public Connection openConnection() throws Exception {
- Class.forName("com.mysql.jdbc.Driver");
- final Connection connection = DriverManager.getConnection("jdbc:mysql://" + this.ip + ":" + this.porta + "/" + this.banco, this.usuario, this.senha);
- return this.connection = connection;
- }
- public Connection getConnection() {
- return this.connection;
- }
- public boolean hasConnection() {
- try {
- return this.connection != null || this.connection.isValid(1);
- }
- catch (SQLException e) {
- return false;
- }
- }
- public void queryUpdate(final String query) {
- new BukkitRunnable() {
- public void run() {
- final Connection conn = Main.getSQL().getConnection();
- PreparedStatement st = null;
- try {
- st = conn.prepareStatement(query);
- st.executeUpdate();
- st.close();
- }
- catch (SQLException e1) {
- e1.printStackTrace();
- try {
- if (Main.getSQL().hasConnection()) {
- Main.getSQL().openConnection();
- }
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- return;
- }
- finally {
- Main.getSQL().closeResources(null, st);
- }
- Main.getSQL().closeResources(null, st);
- }
- }.runTaskAsynchronously(Main.getPlugin());
- }
- public void closeResources(final ResultSet rs, final PreparedStatement st) {
- if (rs != null) {
- try {
- rs.close();
- }
- catch (SQLException ex) {}
- }
- if (st != null) {
- try {
- st.close();
- }
- catch (SQLException ex2) {}
- }
- }
- public void closeConnection() {
- try {
- if (!MySQL.ativo) {
- return;
- }
- if (this.connection.isClosed()) {
- return;
- }
- this.connection.close();
- }
- catch (SQLException ex) {
- return;
- }
- finally {
- this.connection = null;
- }
- this.connection = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement