Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.polocraft.rework.Mysql;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
- import java.util.logging.Level;
- import me.polocraft.rework.Main;
- import me.polocraft.rework.Mysql.PlayerProfile;
- public class Database {
- String user = "";
- String database = "";
- String password = "";
- String port = "";
- String hostname = "";
- Connection c = null;
- public Database(String user, String password, String database, String port, String hostname) {
- this.user = user;
- this.database = database;
- this.password = password;
- if (port == null) {
- this.port = "3306";
- } else {
- this.port = port;
- }
- this.hostname = hostname;
- }
- /**
- * Open mysql connection
- * @return
- */
- public Connection open() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- c = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/" + database + "?autoReconnect=true", user, password);
- System.out.println("MySQL connection successful");
- Main.getInstance().getLogger().log(Level.INFO, "MySQL connection successful");
- return c;
- } catch (SQLException e) {
- Main.getInstance().getLogger().log(Level.SEVERE, "MySQL connection has been aborted");
- Main.getInstance().getLogger().log(Level.SEVERE, "Reason:" + e.getMessage());
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- System.out.println("JDBC Driver not found!");
- }
- return c;
- }
- public Connection getConnection() {
- return this.c;
- }
- /**
- * Close mysql connection
- */
- public Connection close(Connection c){
- try{
- c.close();
- } catch (SQLException e) {
- Main.getInstance().getLogger().log(Level.SEVERE, "MySQL can't close connection");
- e.printStackTrace();
- }
- return c;
- }
- /**
- * Get all tables together and setup one by one
- */
- public void setupTables(){
- setupProfiles();
- }
- /**
- * Setup the hub player profiles
- */
- public void setupProfiles(){
- try {
- PreparedStatement profiles = c
- .prepareStatement("CREATE TABLE IF NOT EXISTS `Core`(`id` int(11) NOT NULL auto_increment,"
- + " `UUID` varchar(255) NOT NULL,"
- + " `Coins` bigint(255) NOT NULL,"
- + " `Tokens` bigint(255) NOT NULL,"
- + " PRIMARY KEY(`id`));");
- profiles.execute();
- profiles.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Try to create a new player profile
- * @param uuid
- * @return
- */
- public boolean createPlayerProfile(UUID uuid){
- try
- {
- String coreStatement = "SELECT * FROM Core WHERE UUID = '" + uuid + "';";
- ResultSet result = this.c.createStatement().executeQuery(coreStatement);
- if (result.next()) {
- PlayerProfile.addPlayerProfile(new PlayerProfile(uuid, result.getLong("Tokens"),result.getLong("Coins")));
- } else {
- PlayerProfile.addPlayerProfile(new PlayerProfile(uuid,0,0));
- }
- }
- catch (SQLException e)
- {
- System.out.println("Error while trying to create PlayerDat! Reason: " + e.getMessage());
- return false;
- }
- return true;
- }
- /**
- * Update all data from player profile to the sql database
- * @param playerProfile
- * @return
- */
- public boolean updatePlayerProfileSQL(PlayerProfile playerProfile)
- {
- try
- {
- String coreStatement = "SELECT * FROM Core WHERE UUID = '" + playerProfile.getUUID() + "'";
- ResultSet result = this.c.createStatement().executeQuery(coreStatement);
- if (result.next())
- {
- String coreUpdate = "UPDATE Core SET Tokens = " + playerProfile.getTokens()
- + ", Coins = " + playerProfile.getCoins()
- + ", Tokens = " + playerProfile.getTokens()
- + " WHERE UUID = '"
- + playerProfile.getUUID() + "';";
- this.c.createStatement().executeUpdate(coreUpdate);
- }
- else
- {
- String coreUpdate = "INSERT INTO Core (UUID, Tokens, Coins) VALUES ('"
- + playerProfile.getUUID() + "', "
- + playerProfile.getTokens() + ", "
- + playerProfile.getCoins() + ", ";
- this.c.createStatement().executeUpdate(coreUpdate);
- }
- }
- catch (SQLException e)
- {
- System.out.println("Error while trying to update the MySQL! Reason: " + e.getMessage());
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement