Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.batthomas.ttt.database;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.bukkit.entity.Player;
- public class MySQL {
- public static String username = "";
- public static String password = "";
- public static String database = "";
- public static String host = "";
- public static int port = 3306;
- public static Connection con;
- public static void connect() {
- if (!MySQL.isConnected()) {
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
- } catch (SQLException ex) {
- Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static void close() {
- if (MySQL.isConnected()) {
- try {
- con.close();
- } catch (SQLException ex) {
- Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static boolean isConnected() {
- return con != null;
- }
- public static void createTable() {
- if (MySQL.isConnected()) {
- try {
- con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Stats (PlayerUUID TEXT,Karma TEXT,Kills TEXT,Deaths TEXT,Pass TEXT,Tested TEXT,WinnedGames TEXT,PlayedGames TEXT,RID int(11) NOT NULL auto_increment,primary KEY (RID));");
- } catch (SQLException ex) {
- Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static void update(String qry) {
- try {
- con.createStatement().executeUpdate(qry);
- } catch (SQLException ex) {
- Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static ResultSet getResult(String qry) {
- if (MySQL.isConnected()) {
- try {
- return con.createStatement().executeQuery(qry);
- } catch (SQLException ex) {
- Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- return null;
- }
- public static boolean isInDatabase(Player player) {
- try {
- ResultSet rs = MySQL.getResult("SELECT * FROM Stats WHERE PlayerUUID = '" + player.getUniqueId() + "'");
- return rs.next();
- } catch (Exception ex) {
- Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- }
- public static void addPlayerToDatabase(Player player) {
- try {
- MySQL.update("INSERT INTO Stats (PlayerUUID,Karma,Kills,Deaths,Pass,Tested,WinnedGames,PlayedGames) VALUES ('" + player.getUniqueId().toString() + "', '0', '0', '0', '0', '0', '0', '0')");
- } catch (Exception ex) {
- Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement