import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import net.pietregcode.pietreg.core.apies.SimpleLogger; import net.pietregcode.pietreg.core.data.configs.Config; public class MySQL implements Storage { private Connection connection; @Override public boolean connect() { try { Class.forName("com.mysql.jdbc.Driver"); this.connection = DriverManager.getConnection("jdbc:mysql://" + Config.MYSQL_HOST + ":" + Config.MYSQL_PORT + "/" + Config.MYSQL_BASENAME, Config.MYSQL_USERNAME, Config.MYSQL_PASSWORLD); aLogger.getLogger().info(" Polaczono z baza danych MySQL!"); return true; } catch (ClassNotFoundException e) { SimpleLogger.getLogger().warn("JDBC driver not found!", "Error: " + e.getMessage()); e.printStackTrace(); } catch (SQLException e2) { aLogger.getLogger().warn("Can not connect to a MySQL server!", "Error: " + e2.getMessage()); e2.printStackTrace(); } return false; } @Override public void disconnect() { try { if (this.connection != null) this.connection.close(); } catch (SQLException e) { e.printStackTrace(); } } @Override public ResultSet executeQuery(String query) { try { if (this.connection != null) return this.connection.createStatement().executeQuery(query); } catch (SQLException e) { e.printStackTrace(); } return null; } @Override public void executeUpdate(String update) { try { if (this.connection != null) this.connection.createStatement().executeUpdate(update); } catch (SQLException e) { e.printStackTrace(); } } @Override public void createTables() { executeUpdate("CREATE TABLE IF NOT EXISTS `bans` (`name` varchar(32) NOT NULL, " + "`time` bigint(22) NOT NULL, " + "`reason` text NOT NULL, " + "`admin` varchar(32) NOT NULL, " + "`start` BIGINT(22) NOT NULL, " + "`unban` int(1) NOT NULL);"); } }