Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package pl.argania.core.basic;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.UUID;
  8.  
  9. import pl.argania.core.managers.UserManager;
  10. import pl.argania.core.objects.User;
  11.  
  12. public class MySQL {
  13.  
  14.     public static Connection conn;
  15.    
  16.     static Config config = Config.getInstance();
  17.    
  18.     public static boolean isConnected(){
  19.         try {
  20.             if (conn == null){
  21.                 return false;
  22.             }
  23.             if (conn.isClosed()){
  24.                 return false;
  25.             }
  26.         } catch (final SQLException e){
  27.             e.printStackTrace();
  28.         }
  29.         return true;
  30.     }
  31.    
  32.     public static void connect(){
  33.         if (!isConnected()){
  34.             try {
  35.                 MySQL.conn = DriverManager.getConnection("jdbc:mysql://" + config.getConfig().getString("mysql.hostname") + ":" + config.getConfig().getString("mysql.port") + "/" + config.getConfig().getString("mysql.database") + "?user=" + config.getConfig().getString("mysql.username") + "&password=" + config.getConfig().getString("mysql.password"));
  36.             } catch (final SQLException e){
  37.                 e.printStackTrace();
  38.             }
  39.         }
  40.     }
  41.    
  42.     public static void disconnect(){
  43.         if (isConnected()){
  44.             try {
  45.                 conn.close();
  46.             } catch (final SQLException e){
  47.                 e.printStackTrace();
  48.             }
  49.         }
  50.     }
  51.    
  52.     public static void createTable(){
  53.         connect();
  54.         final StringBuilder sb = new StringBuilder();
  55.         sb.append("create table if not exists core_users(");
  56.         sb.append("uuid char(36) not null,");
  57.         sb.append("name varchar(16) not null,");
  58.         sb.append("protectiontime text not null,");
  59.         sb.append("deathbantime text not null,");
  60.         sb.append("primary key(uuid));");
  61.         try {
  62.             conn.createStatement().executeUpdate(sb.toString());
  63.         }
  64.         catch (SQLException e) {
  65.             e.printStackTrace();
  66.         }
  67.         disconnect();
  68.     }
  69.    
  70.     public static void loadData() throws SQLException {
  71.         connect();
  72.         int i = 0;
  73.         final ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM `core_users`");
  74.         while (rs.next()){
  75.             final User u = User.get(UUID.fromString(rs.getString("uuid")));
  76.             u.setName(rs.getString("name"));
  77.             u.setProtectionTime(Long.valueOf(rs.getString("protectiontime")));
  78.             u.setDeathBanTime(Long.valueOf(rs.getString("deathbantime")));
  79.             i++;
  80.         }
  81.         System.out.println("Loaded " + i + " users!");
  82.         disconnect();
  83.     }
  84.    
  85.     public static void saveData() throws SQLException {
  86.         connect();
  87.         int i = 0;
  88.         for (final User u : UserManager.getUsers()){
  89.             final StringBuilder sb = new StringBuilder();
  90.             sb.append("INSERT INTO core_users (uuid, name, protectiontime, deathbantime) VALUES (");
  91.             sb.append("'" + u.getUniqueId().toString() + "',");
  92.             sb.append("'" + u.getName() + "',");
  93.             sb.append("'" + u.getProtectionTime() + "',");
  94.             sb.append("'" + u.getDeathBanTime() + "'");
  95.             sb.append(") ON DUPLICATE KEY UPDATE ");
  96.             sb.append("name='" + u.getName() + "',");
  97.             sb.append("protectiontime='" + u.getProtectionTime() + "',");
  98.             sb.append("deathbantime='" + u.getDeathBanTime() + "';");
  99.             conn.createStatement().executeUpdate(sb.toString());
  100.             i++;
  101.         }
  102.         System.out.println("Saved " + i + " users!");
  103.         disconnect();
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement