Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.88 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.nio.charset.Charset;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.sql.*;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.UUID;
  10.  
  11. public class DBManager {
  12.     private static Connection connection;
  13.     private static Connection connectionLog;
  14.     private static List<String> dbAccountInfo;
  15.     private static List<String> dbLogInfo;
  16.  
  17.     // Считывает конфиг и на его основе выполняет подключение к БДшкам
  18.     public static boolean tryConnect() {
  19.         try {
  20.             String pathAccount = ItemShopMod.configPath.getParent().toString() + "/dbAccountInfo.cfg";
  21.             File dbAccountFile = new File(pathAccount);
  22.             if (!dbAccountFile.exists()) {
  23.                 List<String> list = new ArrayList<>();
  24.                 list.add("host");
  25.                 list.add("port");
  26.                 list.add("dbName");
  27.                 list.add("user");
  28.                 list.add("password");
  29.  
  30.                 list.add("tableNameWithUUIDandMoney");
  31.                 list.add("uuid");
  32.                 list.add("money");
  33.                 Files.write(Paths.get(pathAccount), list, Charset.forName("UTF-8"));
  34.                 return false;
  35.             }
  36.             dbAccountInfo = Files.readAllLines(Paths.get(pathAccount));
  37.  
  38.             String pathLog = ItemShopMod.configPath.getParent().toString() + "/dbLogInfo.cfg";
  39.             File dbLogFile = new File(pathLog);
  40.             if (!dbLogFile.exists()) {
  41.                 List<String> list = new ArrayList<>();
  42.                 list.add("host");
  43.                 list.add("port");
  44.                 list.add("dbName");
  45.                 list.add("user");
  46.                 list.add("password");
  47.                 list.add("logTableName");
  48.                 list.add("UUID");
  49.                 list.add("ItemId");
  50.                 list.add("Count");
  51.                 Files.write(Paths.get(pathLog), list, Charset.forName("UTF-8"));
  52.                 return false;
  53.             }
  54.             dbLogInfo = Files.readAllLines(Paths.get(pathLog));
  55.  
  56.             Class.forName("com.mysql.jdbc.Driver");
  57.             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");
  58.             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");
  59.  
  60.         } catch (ClassNotFoundException | SQLException e) {
  61.             System.out.println("Could not connect to mysql database!");
  62.             e.printStackTrace();
  63.             return false;
  64.         } catch (IOException e) {
  65.             e.printStackTrace();
  66.             return false;
  67.         }
  68.         return true;
  69.     }
  70.  
  71.     // Возвращает деньги игрока из БД
  72.     public static int getMoney(UUID player) {
  73.         try {
  74.  
  75.             String sql = "SELECT " + dbAccountInfo.get(7) + " FROM " + dbAccountInfo.get(5) + " WHERE " + dbAccountInfo.get(6) + "=?";
  76.             PreparedStatement preparedUpdateStatement = connection.prepareStatement(sql);
  77.             preparedUpdateStatement.setString(1, player.toString());
  78.             ResultSet result = preparedUpdateStatement.executeQuery();
  79.  
  80.             System.out.println("Get Player Balance: " + result);
  81.  
  82.             while (result.next()) {
  83.                 return result.getInt(dbAccountInfo.get(7));
  84.             }
  85.         } catch (SQLException e) {
  86.             e.printStackTrace();
  87.         }
  88.         return 0;
  89.     }
  90.  
  91.     // Устанавливает требуемуб сумму денег для игрока в БД
  92.     public static boolean setMoney(UUID player, int money) {
  93.         try {
  94.             String updateSql = "UPDATE " + dbAccountInfo.get(5) + " SET " + dbAccountInfo.get(7) + "=? WHERE " + dbAccountInfo.get(6) + "=?";
  95.             PreparedStatement preparedUpdateStatement = connection.prepareStatement(updateSql);
  96.             preparedUpdateStatement.setInt(1, money);
  97.             preparedUpdateStatement.setString(2, player.toString());
  98.  
  99.             System.out.println("Player Balance Now: " + money);
  100.  
  101.             preparedUpdateStatement.executeUpdate();
  102.             return true;
  103.         } catch (SQLException e) {
  104.             e.printStackTrace();
  105.         }
  106.         return false;
  107.     }
  108.  
  109.     // Логирует покупку. oldBalance - балан игрока до покупки
  110.     public static boolean logPurchase(UUID player, int oldBalance, ModConfigs.ShopItem item) {
  111.         try {
  112.             String updateSql = "INSERT INTO " + dbLogInfo.get(5) + " VALUES(?, ?, ?, ?, ?, ?)";
  113.             PreparedStatement preparedUpdateStatement = connectionLog.prepareStatement(updateSql);
  114.             preparedUpdateStatement.setDate(1, new Date(Utils.getMSKDate().getTime()));
  115.             preparedUpdateStatement.setString(2, player.toString());
  116.             preparedUpdateStatement.setInt(3, oldBalance);
  117.             preparedUpdateStatement.setInt(4, item.itemId);
  118.             preparedUpdateStatement.setInt(5, item.count);
  119.             preparedUpdateStatement.setInt(6, item.price);
  120.  
  121.             System.out.println(updateSql);
  122.             System.out.println(preparedUpdateStatement);
  123.  
  124.             preparedUpdateStatement.executeUpdate();
  125.  
  126.             return true;
  127.         } catch (SQLException e) {
  128.             e.printStackTrace();
  129.         }
  130.         return false;
  131.     }
  132.  
  133.     // Возвращает все купленные когда-либо игроком предметы на основе логов
  134.     public static List<StorageItem> getPurchasedItems(UUID player) {
  135.         try {
  136.             String sql = "SELECT " + dbLogInfo.get(7) + ", " + dbLogInfo.get(8) + " FROM " + dbLogInfo.get(5) + " WHERE " + dbLogInfo.get(6) + "=?";
  137.             PreparedStatement preparedUpdateStatement = connectionLog.prepareStatement(sql);
  138.             preparedUpdateStatement.setString(1, player.toString());
  139.             ResultSet result = preparedUpdateStatement.executeQuery();
  140.             System.out.println(sql);
  141.             System.out.println(result);
  142.  
  143.             List<StorageItem> items = new ArrayList<>();
  144.             while (result.next()) {
  145.                 int itemId = result.getInt(dbLogInfo.get(7));
  146.                 int count = result.getInt(dbLogInfo.get(8));
  147.                 items.add(new StorageItem(itemId, count));
  148.             }
  149.             System.out.println(items);
  150.             return items;
  151.         } catch (SQLException e) {
  152.             e.printStackTrace();
  153.         }
  154.         return new ArrayList<>();
  155.     }
  156.  
  157.     public static Connection getDB() {
  158.         return connection;
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement