Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.argania.core.basic;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- import pl.argania.core.managers.UserManager;
- import pl.argania.core.objects.User;
- public class MySQL {
- public static Connection conn;
- static Config config = Config.getInstance();
- public static boolean isConnected(){
- try {
- if (conn == null){
- return false;
- }
- if (conn.isClosed()){
- return false;
- }
- } catch (final SQLException e){
- e.printStackTrace();
- }
- return true;
- }
- public static void connect(){
- if (!isConnected()){
- try {
- 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"));
- } catch (final SQLException e){
- e.printStackTrace();
- }
- }
- }
- public static void disconnect(){
- if (isConnected()){
- try {
- conn.close();
- } catch (final SQLException e){
- e.printStackTrace();
- }
- }
- }
- public static void createTable(){
- connect();
- final StringBuilder sb = new StringBuilder();
- sb.append("create table if not exists core_users(");
- sb.append("uuid char(36) not null,");
- sb.append("name varchar(16) not null,");
- sb.append("protectiontime text not null,");
- sb.append("deathbantime text not null,");
- sb.append("primary key(uuid));");
- try {
- conn.createStatement().executeUpdate(sb.toString());
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- disconnect();
- }
- public static void loadData() throws SQLException {
- connect();
- int i = 0;
- final ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM `core_users`");
- while (rs.next()){
- final User u = User.get(UUID.fromString(rs.getString("uuid")));
- u.setName(rs.getString("name"));
- u.setProtectionTime(Long.valueOf(rs.getString("protectiontime")));
- u.setDeathBanTime(Long.valueOf(rs.getString("deathbantime")));
- i++;
- }
- System.out.println("Loaded " + i + " users!");
- disconnect();
- }
- public static void saveData() throws SQLException {
- connect();
- int i = 0;
- for (final User u : UserManager.getUsers()){
- final StringBuilder sb = new StringBuilder();
- sb.append("INSERT INTO core_users (uuid, name, protectiontime, deathbantime) VALUES (");
- sb.append("'" + u.getUniqueId().toString() + "',");
- sb.append("'" + u.getName() + "',");
- sb.append("'" + u.getProtectionTime() + "',");
- sb.append("'" + u.getDeathBanTime() + "'");
- sb.append(") ON DUPLICATE KEY UPDATE ");
- sb.append("name='" + u.getName() + "',");
- sb.append("protectiontime='" + u.getProtectionTime() + "',");
- sb.append("deathbantime='" + u.getDeathBanTime() + "';");
- conn.createStatement().executeUpdate(sb.toString());
- i++;
- }
- System.out.println("Saved " + i + " users!");
- disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement