Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
- public class DBManager {
- private static Connection connection;
- private static Connection connectionLog;
- private static List<String> dbAccountInfo;
- private static List<String> dbLogInfo;
- // Считывает конфиг и на его основе выполняет подключение к БДшкам
- public static boolean tryConnect() {
- try {
- String pathAccount = ItemShopMod.configPath.getParent().toString() + "/dbAccountInfo.cfg";
- File dbAccountFile = new File(pathAccount);
- if (!dbAccountFile.exists()) {
- List<String> list = new ArrayList<>();
- list.add("host");
- list.add("port");
- list.add("dbName");
- list.add("user");
- list.add("password");
- list.add("tableNameWithUUIDandMoney");
- list.add("uuid");
- list.add("money");
- Files.write(Paths.get(pathAccount), list, Charset.forName("UTF-8"));
- return false;
- }
- dbAccountInfo = Files.readAllLines(Paths.get(pathAccount));
- String pathLog = ItemShopMod.configPath.getParent().toString() + "/dbLogInfo.cfg";
- File dbLogFile = new File(pathLog);
- if (!dbLogFile.exists()) {
- List<String> list = new ArrayList<>();
- list.add("host");
- list.add("port");
- list.add("dbName");
- list.add("user");
- list.add("password");
- list.add("logTableName");
- list.add("UUID");
- list.add("ItemId");
- list.add("Count");
- Files.write(Paths.get(pathLog), list, Charset.forName("UTF-8"));
- return false;
- }
- dbLogInfo = Files.readAllLines(Paths.get(pathLog));
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + dbAccountInfo.get(0) + ":" + dbAccountInfo.get(1) + "/" + dbAccountInfo.get(2) + "?user=" + dbAccountInfo.get(3) + "&password=" + dbAccountInfo.get(4) + "&useUnicode=true&characterEncoding=UTF-8");
- connectionLog = DriverManager.getConnection("jdbc:mysql://" + dbLogInfo.get(0) + ":" + dbLogInfo.get(1) + "/" + dbLogInfo.get(2) + "?user=" + dbLogInfo.get(3) + "&password=" + dbLogInfo.get(4) + "&useUnicode=true&characterEncoding=UTF-8");
- } catch (ClassNotFoundException | SQLException e) {
- System.out.println("Could not connect to mysql database!");
- e.printStackTrace();
- return false;
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- // Возвращает деньги игрока из БД
- public static int getMoney(UUID player) {
- try {
- String sql = "SELECT " + dbAccountInfo.get(7) + " FROM " + dbAccountInfo.get(5) + " WHERE " + dbAccountInfo.get(6) + "=?";
- PreparedStatement preparedUpdateStatement = connection.prepareStatement(sql);
- preparedUpdateStatement.setString(1, player.toString());
- ResultSet result = preparedUpdateStatement.executeQuery();
- System.out.println("Get Player Balance: " + result);
- while (result.next()) {
- return result.getInt(dbAccountInfo.get(7));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return 0;
- }
- // Устанавливает требуемуб сумму денег для игрока в БД
- public static boolean setMoney(UUID player, int money) {
- try {
- String updateSql = "UPDATE " + dbAccountInfo.get(5) + " SET " + dbAccountInfo.get(7) + "=? WHERE " + dbAccountInfo.get(6) + "=?";
- PreparedStatement preparedUpdateStatement = connection.prepareStatement(updateSql);
- preparedUpdateStatement.setInt(1, money);
- preparedUpdateStatement.setString(2, player.toString());
- System.out.println("Player Balance Now: " + money);
- preparedUpdateStatement.executeUpdate();
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- // Логирует покупку. oldBalance - балан игрока до покупки
- public static boolean logPurchase(UUID player, int oldBalance, ModConfigs.ShopItem item) {
- try {
- String updateSql = "INSERT INTO " + dbLogInfo.get(5) + " VALUES(?, ?, ?, ?, ?, ?)";
- PreparedStatement preparedUpdateStatement = connectionLog.prepareStatement(updateSql);
- preparedUpdateStatement.setDate(1, new Date(Utils.getMSKDate().getTime()));
- preparedUpdateStatement.setString(2, player.toString());
- preparedUpdateStatement.setInt(3, oldBalance);
- preparedUpdateStatement.setInt(4, item.itemId);
- preparedUpdateStatement.setInt(5, item.count);
- preparedUpdateStatement.setInt(6, item.price);
- System.out.println(updateSql);
- System.out.println(preparedUpdateStatement);
- preparedUpdateStatement.executeUpdate();
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- // Возвращает все купленные когда-либо игроком предметы на основе логов
- public static List<StorageItem> getPurchasedItems(UUID player) {
- try {
- String sql = "SELECT " + dbLogInfo.get(7) + ", " + dbLogInfo.get(8) + " FROM " + dbLogInfo.get(5) + " WHERE " + dbLogInfo.get(6) + "=?";
- PreparedStatement preparedUpdateStatement = connectionLog.prepareStatement(sql);
- preparedUpdateStatement.setString(1, player.toString());
- ResultSet result = preparedUpdateStatement.executeQuery();
- System.out.println(sql);
- System.out.println(result);
- List<StorageItem> items = new ArrayList<>();
- while (result.next()) {
- int itemId = result.getInt(dbLogInfo.get(7));
- int count = result.getInt(dbLogInfo.get(8));
- items.add(new StorageItem(itemId, count));
- }
- System.out.println(items);
- return items;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return new ArrayList<>();
- }
- public static Connection getDB() {
- return connection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement