Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 1: */ package mintcraftian.stats.mysql;
- /* 2: */
- /* 3: */ import java.sql.Connection;
- /* 4: */ import java.sql.DriverManager;
- /* 5: */ import java.sql.PreparedStatement;
- /* 6: */ import java.sql.ResultSet;
- /* 7: */ import java.sql.SQLException;
- /* 8: */ import mintcraftian.stats.Main;
- /* 9: */ import org.bukkit.configuration.file.FileConfiguration;
- /* 10: */ import org.bukkit.scheduler.BukkitRunnable;
- /* 11: */
- /* 12: */ public class MySQL
- /* 13: */ {
- /* 14: */ private String address;
- /* 15: */ private String database;
- /* 16: */ private String pass;
- /* 17: */ private String username;
- /* 18: */ private Connection connection;
- /* 19: */
- /* 20: */ public MySQL(Main instance)
- /* 21: */ {
- /* 22: 19 */ this.address = instance.getConfig().getString("Address");
- /* 23: 20 */ this.database = instance.getConfig().getString("Database");
- /* 24: 21 */ this.pass = instance.getConfig().getString("Password");
- /* 25: 22 */ this.username = instance.getConfig().getString("Username");
- /* 26: 23 */ connect();
- /* 27: */ }
- /* 28: */
- /* 29: */ public synchronized boolean connect()
- /* 30: */ {
- /* 31: */ try
- /* 32: */ {
- /* 33: 28 */ this.connection = DriverManager.getConnection("jdbc:mysql://" + this.address + "/" + this.database, this.username, this.pass);
- /* 34: 29 */ connectionCheck();
- /* 35: 30 */ return true;
- /* 36: */ }
- /* 37: */ catch (SQLException e)
- /* 38: */ {
- /* 39: 32 */ e.printStackTrace();
- /* 40: */ }
- /* 41: 33 */ return false;
- /* 42: */ }
- /* 43: */
- /* 44: */ private void connectionCheck()
- /* 45: */ {
- /* 46: 50 */ new BukkitRunnable()
- /* 47: */ {
- /* 48: */ public void run()
- /* 49: */ {
- /* 50: */ try
- /* 51: */ {
- /* 52: 42 */ MySQL.this.connection.close();
- /* 53: */ }
- /* 54: */ catch (SQLException e)
- /* 55: */ {
- /* 56: 44 */ e.printStackTrace();
- /* 57: */ }
- /* 58: 46 */ cancel();
- /* 59: 47 */ MySQL.this.connect();
- /* 60: */ }
- /* 61: 50 */ }.runTaskLater(Main.getInstance(), 2400L);
- /* 62: */ }
- /* 63: */
- /* 64: */ public Object getValue(String uuid, String table, String column, boolean isString)
- /* 65: */ {
- /* 66: 54 */ if (this.connection == null) {
- /* 67: 55 */ connect();
- /* 68: */ }
- /* 69: 57 */ String query = "SELECT * FROM `" + table + "` WHERE `name`='" + uuid + "'";
- /* 70: */ try
- /* 71: */ {
- /* 72: 60 */ ResultSet res = this.connection.prepareStatement(query).executeQuery();
- /* 73: */
- /* 74: 62 */ Object value = null;
- /* 75: 63 */ while (res.next()) {
- /* 76: 64 */ if (!isString) {
- /* 77: 65 */ value = res.getObject(column);
- /* 78: */ } else {
- /* 79: 67 */ value = res.getString(column);
- /* 80: */ }
- /* 81: */ }
- /* 82: 71 */ res.close();
- /* 83: 72 */ return value;
- /* 84: */ }
- /* 85: */ catch (SQLException e)
- /* 86: */ {
- /* 87: 74 */ e.printStackTrace();
- /* 88: */ }
- /* 89: 75 */ return null;
- /* 90: */ }
- /* 91: */
- /* 92: */ public void setValue(String uuid, String table, String column, Object value)
- /* 93: */ {
- /* 94: 80 */ if (this.connection == null) {
- /* 95: 81 */ connect();
- /* 96: */ }
- /* 97: */ try
- /* 98: */ {
- /* 99: 84 */ String query = "";
- /* 100: 85 */ if ((value instanceof String))
- /* 101: */ {
- /* 102: 86 */ if (containsElement(table, "name", uuid)) {
- /* 103: 87 */ query = "UPDATE `" + table + "` SET `name`='" + uuid + "',`" + column + "`='" + value + "' WHERE `name`='" + uuid + "'";
- /* 104: */ } else {
- /* 105: 89 */ query = "INSERT INTO `" + table + "`(`name`, `" + column + "`) VALUES ('" + uuid + "','" + value + "')";
- /* 106: */ }
- /* 107: */ }
- /* 108: 92 */ else if (containsElement(table, "name", uuid)) {
- /* 109: 93 */ query = "UPDATE `" + table + "` SET `name`='" + uuid + "',`" + column + "`=" + value + " WHERE `name`='" + uuid + "'";
- /* 110: */ } else {
- /* 111: 95 */ query = "INSERT INTO `" + table + "`(`name`, `" + column + "`) VALUES ('" + uuid + "'," + value + ")";
- /* 112: */ }
- /* 113: 99 */ PreparedStatement p = this.connection.prepareStatement(query);
- /* 114:100 */ p.execute();
- /* 115:101 */ p.close();
- /* 116: */ }
- /* 117: */ catch (SQLException e)
- /* 118: */ {
- /* 119:103 */ e.printStackTrace();
- /* 120: */ }
- /* 121: */ }
- /* 122: */
- /* 123: */ public boolean containsElement(String table, String column, Object element)
- /* 124: */ {
- /* 125:108 */ if (this.connection == null) {
- /* 126:109 */ connect();
- /* 127: */ }
- /* 128:111 */ String query = "SELECT `" + column + "` FROM `" + table + "` WHERE `" + column + "`='" + element + "'";
- /* 129: */ try
- /* 130: */ {
- /* 131:113 */ ResultSet res = this.connection.prepareStatement(query).executeQuery();
- /* 132:114 */ boolean temp = res.next();
- /* 133:115 */ res.close();
- /* 134:116 */ return temp;
- /* 135: */ }
- /* 136: */ catch (SQLException e)
- /* 137: */ {
- /* 138:118 */ e.printStackTrace();
- /* 139: */ }
- /* 140:119 */ return true;
- /* 141: */ }
- /* 142: */ }
- /* Location: C:\Users\Internet\Desktop\Stats\bin\
- * Qualified Name: mintcraftian.stats.mysql.MySQL
- * JD-Core Version: 0.7.0.1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement