Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hohserg.economy;
- import net.minecraft.entity.player.EntityPlayer;
- import java.sql.*;
- public final class DatabaseConnector
- {
- private String table;
- private Connection connection;
- public DatabaseConnector(String url, String user, String pass, String db, String table, short port) throws SQLException
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- }
- catch (final ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- connection = DriverManager.getConnection("jdbc:mysql://" + url + ":" + port + "/" + db, user, pass);
- this.table = table;
- createTable(table);
- }
- public void createTable(String table) throws SQLException
- {
- PreparedStatement statement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `" + table + "` (`name` CHAR(32) NOT NULL, `uuid` CHAR(36) NOT NULL, `laky` INT(11) NOT NULL DEFAULT 0, `arena` INT(11) NOT NULL DEFAULT 0, `donate` INT(11) NOT NULL DEFAULT 0, `game` INT(11) NOT NULL DEFAULT 0);");
- statement.executeUpdate();
- }
- public int get(EntityPlayer player, MoneyType moneyType) throws SQLException
- {
- if (player != null)
- {
- PreparedStatement select = connection.prepareStatement(
- "SELECT " + moneyType.getName() + " FROM `" + table + "` WHERE `name` = '" + player.getDisplayName() + "' LIMIT 1"
- );
- try (ResultSet set = select.executeQuery())
- {
- if (set.next())
- {
- return set.getInt(moneyType.getName());
- }
- else
- {
- return 0;
- }
- }
- }
- return 0;
- }
- public void add(EntityPlayer player, MoneyType moneyType, int count) throws SQLException
- {
- if (player != null)
- {
- String
- name = player.getDisplayName(),
- uuid = player.getUniqueID().toString();
- PreparedStatement select = connection.prepareStatement("SELECT * FROM `" + table + "` WHERE `name` = '" + name + "' LIMIT 1;");
- try (ResultSet set = select.executeQuery())
- {
- count = validInt(count);
- if (set.next())
- {
- count = set.getInt(moneyType.getName()) + count;
- connection.prepareStatement(
- "UPDATE `" + table + "` SET `" + moneyType.getName() + "` = '" + count + "' WHERE `name` = '" + name + "';"
- ).executeUpdate();
- }
- else
- {
- connection.prepareStatement(
- "INSERT INTO `" + table + "` (`name`, `uuid`, `" + moneyType.getName() +"`) VALUES ('" + name + "', '" + uuid + "', '" + count + "');"
- ).executeUpdate();
- }
- Economy.update(player);
- }
- }
- }
- public void remove(EntityPlayer player, MoneyType money, int count) throws SQLException
- {
- if (player != null)
- {
- String name = player.getDisplayName();
- PreparedStatement select = connection.prepareStatement("SELECT * FROM `" + table + "` WHERE `name` = '" + name + "' LIMIT 1;");
- try (ResultSet set = select.executeQuery())
- {
- count = validInt(count);
- if (set.next())
- {
- count = set.getInt(money.getName()) - count;
- connection.prepareStatement(
- "UPDATE `" + table + "` SET `" + money.getName() + "` = '" + count + "' WHERE `name` = '" + name + "';"
- ).executeUpdate();
- }
- Economy.update(player);
- }
- }
- }
- public void delete(EntityPlayer player) throws SQLException
- {
- if (player != null)
- {
- connection.prepareStatement(
- "DELETE FROM `" + table + "` WHERE `uuid` = '" + player.getUniqueID().toString() + "';"
- ).executeUpdate();
- Economy.update(player);
- }
- }
- private int validInt(int count)
- {
- return Math.abs(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement