Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.toppecraft.ToppeBattles.stats;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.entity.Player;
- import com.toppecraft.ToppeBattles.ToppeBattles;
- /**
- * MySQL system.
- * I'm not very familiar with the MySQL things so I'm sorry if it's badly made now.
- * @author Toppe5
- * @since 0.1
- */
- public class MySQL {
- private String host;
- private int port;
- private String user;
- private String password;
- private String db;
- private Connection conn;
- public static final String TABLE = "stats";
- /**
- * Create a new MySQL with the given values.
- * @param host
- * @param port
- * @param user
- * @param password
- * @param name
- */
- public MySQL(String host, int port, String user, String password, String name) {
- this.host = host;
- this.port = port;
- this.user = user;
- this.password = password;
- this.db = name;
- }
- public boolean isConnected(boolean reconnect) {
- try {
- if(conn != null && !conn.isClosed() && conn.isValid(5000)) {
- return true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- if(reconnect) connect();
- return false;
- }
- public void connect() {
- if (!isConnected(false)) {
- try{
- conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/"+
- db, user, password);
- }catch (SQLException e){
- Bukkit.getLogger().warning("There was an error while connecting to the MySQL...");
- e.printStackTrace();
- return;
- }
- }
- }
- public void createTable() {
- if(isConnected(true)) {
- query("CREATE TABLE IF NOT EXISTS " + TABLE + "(UUID VARCHAR(100), " + Stats.KILLS
- + " int, " + Stats.DEATHS + " int, " + Stats.ELO + " int, " + Stats.LMS + " int, "
- + Stats.BRACKETS + " int, " + Stats.PARTY_VS_PARTY_WINS + " int)");
- }
- }
- public void close() {
- try {
- if (isConnected(false)) {
- conn.close();
- conn = null;
- }
- }catch (SQLException localSQLException) {}
- }
- public void query(String q) {
- query(q, true);
- }
- private void query(String q, boolean first) {
- if (isConnected(true)) {
- PreparedStatement ps = null;
- try{
- ps = conn.prepareStatement(q);
- ps.execute();
- } catch (SQLException e) {
- if(first) {
- query(q, false);
- }
- else {
- e.printStackTrace();
- Bukkit.getScheduler().runTask(ToppeBattles.getInstance(), new Runnable() {
- @Override
- public void run() {
- for(Player pl : Bukkit.getOnlinePlayers()) {
- if(pl.hasPermission("toppebattles.admin")) {
- pl.sendMessage(ChatColor.RED + "Failed to update data "
- + "(Query:" +q + ")");
- }
- }
- }
- });
- }
- } finally {
- if(ps != null) {
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- public ResultSet getResult(String q) {
- if (isConnected(true)) {
- Statement ps = null;
- try {
- ps = conn.createStatement();
- ResultSet rs = ps.executeQuery(q);
- return rs;
- } catch (SQLException e) {
- e.printStackTrace();
- Bukkit.getScheduler().runTask(ToppeBattles.getInstance(), new Runnable() {
- @Override
- public void run() {
- for(Player pl : Bukkit.getOnlinePlayers()) {
- if(pl.hasPermission("toppebattles.admin")) {
- pl.sendMessage(ChatColor.RED + "Failed to collect data "
- + "(Query: " + q + ")");
- }
- }
- }
- });
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement