Dateien nach "src/main/java/net/viper/money/database" hochladen
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package net.craftersland.money.database;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public interface AccountDatabaseInterface<X> {
|
||||
//Accountmethods
|
||||
public boolean hasAccount(OfflinePlayer player);
|
||||
public boolean hasAccount(UUID player);
|
||||
public boolean createAccount(Player player);
|
||||
public Double getBalance(OfflinePlayer player);
|
||||
public Double getBalance(UUID player);
|
||||
public boolean setBalance(OfflinePlayer player, Double amount);
|
||||
public boolean setBalance(UUID player, Double amount);
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package net.craftersland.money.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import net.craftersland.money.Money;
|
||||
|
||||
public class DatabaseManagerFlatFile implements DatabaseManagerInterface {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private Money money;
|
||||
|
||||
public DatabaseManagerFlatFile(Money money) {
|
||||
this.money = money;
|
||||
|
||||
setupDatabase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setupDatabase() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean closeDatabase() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
package net.craftersland.money.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public interface DatabaseManagerInterface {
|
||||
|
||||
public boolean setupDatabase();
|
||||
public boolean closeDatabase();
|
||||
public Connection getConnection();
|
||||
}
|
205
src/main/java/net/viper/money/database/DatabaseManagerMysql.java
Normal file
205
src/main/java/net/viper/money/database/DatabaseManagerMysql.java
Normal file
@@ -0,0 +1,205 @@
|
||||
package net.craftersland.money.database;
|
||||
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import net.craftersland.money.Money;
|
||||
|
||||
public class DatabaseManagerMysql implements DatabaseManagerInterface{
|
||||
|
||||
private Connection conn = null;
|
||||
private Money money;
|
||||
|
||||
public DatabaseManagerMysql(Money money) {
|
||||
this.money = money;
|
||||
connectToDatabase();
|
||||
setupDatabase();
|
||||
}
|
||||
|
||||
private void connectToDatabase() {
|
||||
Money.log.info("Connecting to the database...");
|
||||
try {
|
||||
//Load Drivers
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("user", money.getConfigurationHandler().getString("database.mysql.user"));
|
||||
properties.setProperty("password", money.getConfigurationHandler().getString("database.mysql.password"));
|
||||
properties.setProperty("autoReconnect", "true");
|
||||
properties.setProperty("verifyServerCertificate", "false");
|
||||
properties.setProperty("useSSL", money.getConfigurationHandler().getString("database.mysql.ssl"));
|
||||
properties.setProperty("requireSSL", money.getConfigurationHandler().getString("database.mysql.ssl"));
|
||||
//Connect to database
|
||||
conn = DriverManager.getConnection("jdbc:mysql://" + money.getConfigurationHandler().getString("database.mysql.host") + ":" + money.getConfigurationHandler().getString("database.mysql.port") + "/" + money.getConfigurationHandler().getString("database.mysql.databaseName"), properties);
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
Money.log.severe("Could not locate drivers for mysql! Error: " + e.getMessage());
|
||||
return;
|
||||
} catch (SQLException e) {
|
||||
Money.log.severe("Could not connect to mysql database! Error: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
Money.log.info("Database connection successful!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setupDatabase() {
|
||||
PreparedStatement query = null;
|
||||
try {
|
||||
String data = "CREATE TABLE IF NOT EXISTS `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "` (id int(10) AUTO_INCREMENT, player_uuid varchar(50) NOT NULL UNIQUE, player_name varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, money double(30,2) NOT NULL, last_seen varchar(30) NOT NULL, sync_complete varchar(5) NOT NULL, PRIMARY KEY(id));";
|
||||
query = conn.prepareStatement(data);
|
||||
query.execute();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (query != null) {
|
||||
query.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//Update Tables
|
||||
updateTables();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
checkConnection();
|
||||
return conn;
|
||||
}
|
||||
|
||||
public boolean checkConnection() {
|
||||
try {
|
||||
if (conn == null) {
|
||||
Money.log.warning("Connection failed. Reconnecting...");
|
||||
if (reConnect() == true) return true;
|
||||
return false;
|
||||
}
|
||||
if (!conn.isValid(3)) {
|
||||
Money.log.warning("Connection is idle or terminated. Reconnecting...");
|
||||
if (reConnect() == true) return true;
|
||||
return false;
|
||||
}
|
||||
if (conn.isClosed() == true) {
|
||||
Money.log.warning("Connection is closed. Reconnecting...");
|
||||
if (reConnect() == true) return true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
Money.log.severe("Could not reconnect to Database!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean reConnect() {
|
||||
try {
|
||||
long start = 0;
|
||||
long end = 0;
|
||||
|
||||
start = System.currentTimeMillis();
|
||||
Money.log.info("Attempting to establish a connection to the MySQL server!");
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("user", money.getConfigurationHandler().getString("database.mysql.user"));
|
||||
properties.setProperty("password", money.getConfigurationHandler().getString("database.mysql.password"));
|
||||
properties.setProperty("autoReconnect", "true");
|
||||
properties.setProperty("verifyServerCertificate", "false");
|
||||
properties.setProperty("useSSL", money.getConfigurationHandler().getString("database.mysql.ssl"));
|
||||
properties.setProperty("requireSSL", money.getConfigurationHandler().getString("database.mysql.ssl"));
|
||||
conn = DriverManager.getConnection("jdbc:mysql://" + money.getConfigurationHandler().getString("database.mysql.host") + ":" + money.getConfigurationHandler().getString("database.mysql.port") + "/" + money.getConfigurationHandler().getString("database.mysql.databaseName"), properties);
|
||||
end = System.currentTimeMillis();
|
||||
Money.log.info("Connection to MySQL server established!");
|
||||
Money.log.info("Connection took " + ((end - start)) + "ms!");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
Money.log.severe("Could not connect to MySQL server! because: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean closeDatabase() {
|
||||
try {
|
||||
conn.close();
|
||||
conn = null;
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void updateTables() {
|
||||
if (conn != null) {
|
||||
DatabaseMetaData md = null;
|
||||
ResultSet rs1 = null;
|
||||
ResultSet rs2 = null;
|
||||
ResultSet rs3 = null;
|
||||
PreparedStatement query1 = null;
|
||||
PreparedStatement query2 = null;
|
||||
PreparedStatement query3 = null;
|
||||
try {
|
||||
md = conn.getMetaData();
|
||||
rs1 = md.getColumns(null, null, money.getConfigurationHandler().getString("database.mysql.tableName"), "sync_complete");
|
||||
if (rs1.next()) {
|
||||
} else {
|
||||
String data = "ALTER TABLE `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "` ADD sync_complete varchar(5) NOT NULL DEFAULT 'true';";
|
||||
query1 = conn.prepareStatement(data);
|
||||
query1.execute();
|
||||
}
|
||||
rs2 = md.getColumns(null, null, money.getConfigurationHandler().getString("database.mysql.tableName"), "player_name");
|
||||
if (rs2.next()) {
|
||||
} else {
|
||||
String data = "ALTER TABLE `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "` ADD player_name varchar(50) NOT NULL DEFAULT 'true';";
|
||||
query2 = conn.prepareStatement(data);
|
||||
query2.execute();
|
||||
}
|
||||
rs3 = md.getColumns(null, null, money.getConfigurationHandler().getString("database.mysql.tableName"), "last_seen");
|
||||
if (rs3.next()) {
|
||||
} else {
|
||||
String data = "ALTER TABLE `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "` ADD last_seen varchar(30) NOT NULL DEFAULT 'true';";
|
||||
query3 = conn.prepareStatement(data);
|
||||
query3.execute();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Money.log.severe("Error updating inventory table! Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (query1 != null) {
|
||||
query1.close();
|
||||
}
|
||||
if (query2 != null) {
|
||||
query2.close();
|
||||
}
|
||||
if (query3 != null) {
|
||||
query3.close();
|
||||
}
|
||||
if (rs1 != null) {
|
||||
rs1.close();
|
||||
}
|
||||
if (rs2 != null) {
|
||||
rs2.close();
|
||||
}
|
||||
if (rs3 != null) {
|
||||
rs3.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,150 @@
|
||||
package net.craftersland.money.database;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import net.craftersland.money.Money;
|
||||
|
||||
public class MoneyFlatFileInterface implements AccountDatabaseInterface<Double> {
|
||||
|
||||
private Money money;
|
||||
|
||||
public MoneyFlatFileInterface(Money money) {
|
||||
this.money = money;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAccount(OfflinePlayer player) {
|
||||
(new File("plugins"+System.getProperty("file.separator") + "MysqlEconomyBank" + System.getProperty("file.separator") + "Accounts" + System.getProperty("file.separator") + player.getUniqueId().toString() + ".yml")).exists();
|
||||
|
||||
|
||||
return (new File("plugins"+System.getProperty("file.separator") + "MysqlEconomyBank" + System.getProperty("file.separator") + "Accounts" + System.getProperty("file.separator") + player.getUniqueId().toString() + ".yml")).exists();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAccount(UUID playerUUID) {
|
||||
return (new File("plugins"+System.getProperty("file.separator") + "MysqlEconomyBank" + System.getProperty("file.separator") + "Accounts" + System.getProperty("file.separator") + playerUUID.toString() + ".yml")).exists();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createAccount(Player player) {
|
||||
try {
|
||||
File accountFile = new File("plugins" + System.getProperty("file.separator") + "MysqlEconomyBank" + System.getProperty("file.separator") + "Accounts" + System.getProperty("file.separator") + player.getUniqueId().toString() + ".yml");
|
||||
accountFile.createNewFile();
|
||||
|
||||
FileWriter fw = new FileWriter(accountFile, false);
|
||||
BufferedWriter bw = new BufferedWriter(fw);
|
||||
bw.write("Balance: 0");
|
||||
bw.close();
|
||||
fw.close();
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
money.getLogger().severe("Could not create Account file " + player.getName() + "!");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getBalance(OfflinePlayer player) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player.getPlayer());
|
||||
}
|
||||
|
||||
try {
|
||||
File accountFile = new File("plugins" + System.getProperty("file.separator") + "MysqlEconomyBank" + System.getProperty("file.separator") + "Accounts" + System.getProperty("file.separator") + player.getUniqueId().toString() + ".yml");
|
||||
|
||||
FileReader fr = new FileReader(accountFile);
|
||||
BufferedReader br = new BufferedReader(fr);
|
||||
Double balance = Double.parseDouble(br.readLine().split(":")[1]);
|
||||
br.close();
|
||||
fr.close();
|
||||
return balance;
|
||||
|
||||
} catch (Exception e) {
|
||||
money.getLogger().severe("Could not get Balance of " + player.getName() + "!");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getBalance(UUID playerUUID) {
|
||||
try {
|
||||
File accountFile = new File("plugins" + System.getProperty("file.separator") + "MysqlEconomyBank" + System.getProperty("file.separator") + "Accounts" + System.getProperty("file.separator") + playerUUID.toString() + ".yml");
|
||||
|
||||
FileReader fr = new FileReader(accountFile);
|
||||
BufferedReader br = new BufferedReader(fr);
|
||||
Double balance = Double.parseDouble(br.readLine().split(":")[1]);
|
||||
br.close();
|
||||
fr.close();
|
||||
return balance;
|
||||
|
||||
} catch (Exception e) {
|
||||
money.getLogger().severe("Could not get Balance of " + playerUUID + "!");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBalance(OfflinePlayer player, Double amount) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player.getPlayer());
|
||||
}
|
||||
|
||||
try {
|
||||
File accountFile = new File("plugins" + System.getProperty("file.separator") + "MysqlEconomyBank" + System.getProperty("file.separator") + "Accounts"+System.getProperty("file.separator") + player.getUniqueId().toString() + ".yml");
|
||||
|
||||
FileReader fr = new FileReader(accountFile);
|
||||
BufferedReader br = new BufferedReader(fr);
|
||||
String balances = br.readLine();
|
||||
br.close();
|
||||
fr.close();
|
||||
|
||||
|
||||
FileWriter fw = new FileWriter(accountFile, false);
|
||||
BufferedWriter bw = new BufferedWriter(fw);
|
||||
bw.write(balances.split(":")[0]+": "+amount);
|
||||
bw.close();
|
||||
fw.close();
|
||||
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
money.getLogger().severe("Could not set Balance of "+player.getName()+"!");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBalance(UUID playerUUID, Double amount) {
|
||||
try {
|
||||
File accountFile = new File("plugins" + System.getProperty("file.separator") + "MysqlEconomyBank" + System.getProperty("file.separator") + "Accounts"+System.getProperty("file.separator") + playerUUID.toString() + ".yml");
|
||||
|
||||
FileReader fr = new FileReader(accountFile);
|
||||
BufferedReader br = new BufferedReader(fr);
|
||||
String balances = br.readLine();
|
||||
br.close();
|
||||
fr.close();
|
||||
|
||||
|
||||
FileWriter fw = new FileWriter(accountFile, false);
|
||||
BufferedWriter bw = new BufferedWriter(fw);
|
||||
bw.write(balances.split(":")[0]+": "+amount);
|
||||
bw.close();
|
||||
fw.close();
|
||||
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
money.getLogger().severe("Could not set Balance of " + playerUUID + "!");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
236
src/main/java/net/viper/money/database/MoneyMysqlInterface.java
Normal file
236
src/main/java/net/viper/money/database/MoneyMysqlInterface.java
Normal file
@@ -0,0 +1,236 @@
|
||||
package net.craftersland.money.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import net.craftersland.money.Money;
|
||||
|
||||
public class MoneyMysqlInterface implements AccountDatabaseInterface <Double>{
|
||||
|
||||
private Money money;
|
||||
|
||||
public MoneyMysqlInterface(Money money) {
|
||||
this.money = money;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAccount(OfflinePlayer player) {
|
||||
Connection conn = money.getDatabaseManagerInterface().getConnection();
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
ResultSet result = null;
|
||||
try {
|
||||
String sql = "SELECT `player_uuid` FROM `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "` WHERE `player_uuid` = ? LIMIT 1";
|
||||
preparedUpdateStatement = conn.prepareStatement(sql);
|
||||
preparedUpdateStatement.setString(1, player.getUniqueId().toString());
|
||||
|
||||
result = preparedUpdateStatement.executeQuery();
|
||||
while (result.next()) {
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (result != null) {
|
||||
result.close();
|
||||
}
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAccount(UUID playerUUID) {
|
||||
Connection conn = money.getDatabaseManagerInterface().getConnection();
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
ResultSet result = null;
|
||||
try {
|
||||
String sql = "SELECT `player_uuid` FROM `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "` WHERE `player_uuid` = ? LIMIT 1";
|
||||
preparedUpdateStatement = conn.prepareStatement(sql);
|
||||
preparedUpdateStatement.setString(1, playerUUID.toString());
|
||||
|
||||
result = preparedUpdateStatement.executeQuery();
|
||||
while (result.next()) {
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (result != null) {
|
||||
result.close();
|
||||
}
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createAccount(Player player) {
|
||||
Connection conn = money.getDatabaseManagerInterface().getConnection();
|
||||
PreparedStatement preparedStatement = null;
|
||||
try {
|
||||
String sql = "INSERT INTO `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "`(`player_uuid`, `player_name`, `money`, `last_seen`, `sync_complete`) " + "VALUES(?, ?, ?, ?, ?)";
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
|
||||
preparedStatement.setString(1, player.getUniqueId().toString());
|
||||
preparedStatement.setString(2, player.getName());
|
||||
preparedStatement.setDouble(3, 0.0);
|
||||
preparedStatement.setString(4, System.currentTimeMillis() + "");
|
||||
preparedStatement.setString(5, "true");
|
||||
|
||||
preparedStatement.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedStatement != null) {
|
||||
preparedStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getBalance(OfflinePlayer player) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player.getPlayer());
|
||||
}
|
||||
Connection conn = money.getDatabaseManagerInterface().getConnection();
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
ResultSet result = null;
|
||||
try {
|
||||
String sql = "SELECT `money` FROM `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "` WHERE `player_uuid` = ? LIMIT 1";
|
||||
|
||||
preparedUpdateStatement = conn.prepareStatement(sql);
|
||||
preparedUpdateStatement.setString(1, player.getUniqueId().toString());
|
||||
result = preparedUpdateStatement.executeQuery();
|
||||
|
||||
while (result.next()) {
|
||||
return Double.parseDouble(result.getString("money"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (result != null) {
|
||||
result.close();
|
||||
}
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getBalance(UUID playerUUID) {
|
||||
Connection conn = money.getDatabaseManagerInterface().getConnection();
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
ResultSet result = null;
|
||||
try {
|
||||
String sql = "SELECT `money` FROM `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "` WHERE `player_uuid` = ? LIMIT 1";
|
||||
|
||||
preparedUpdateStatement = conn.prepareStatement(sql);
|
||||
preparedUpdateStatement.setString(1, playerUUID.toString());
|
||||
result = preparedUpdateStatement.executeQuery();
|
||||
|
||||
while (result.next()) {
|
||||
return Double.parseDouble(result.getString("money"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (result != null) {
|
||||
result.close();
|
||||
}
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBalance(OfflinePlayer player, Double amount) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player.getPlayer());
|
||||
}
|
||||
Connection conn = money.getDatabaseManagerInterface().getConnection();
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
try {
|
||||
String updateSql = "UPDATE `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "` " + "SET `money` = ?" + "WHERE `player_uuid` = ?";
|
||||
preparedUpdateStatement = conn.prepareStatement(updateSql);
|
||||
preparedUpdateStatement.setDouble(1, amount);
|
||||
preparedUpdateStatement.setString(2, player.getUniqueId().toString());
|
||||
|
||||
preparedUpdateStatement.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBalance(UUID playerUUID, Double amount) {
|
||||
Connection conn = money.getDatabaseManagerInterface().getConnection();
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
try {
|
||||
String updateSql = "UPDATE `" + money.getConfigurationHandler().getString("database.mysql.tableName") + "` " + "SET `money` = ?" + "WHERE `player_uuid` = ?";
|
||||
preparedUpdateStatement = conn.prepareStatement(updateSql);
|
||||
preparedUpdateStatement.setDouble(1, amount);
|
||||
preparedUpdateStatement.setString(2, playerUUID.toString());
|
||||
|
||||
preparedUpdateStatement.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user