Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.lifecraft.sql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import fr.lifecraft.HungerGames;
- public class MySQL {
- private Database db;
- private Connection connection;
- private boolean readOnly;
- public MySQL(Database db) throws SQLException, NullPointerException {
- this.db = db;
- try {
- this.connection = DriverManager.getConnection("jdbc:mysql://" + db.getHost() + ":" + db.getPort() + "/"
- + db.getDatabase() + "?user=" + db.getUser() + "&password=" + db.getPassword() + "&autoReconnect="
- + db.isAutoReconnect());
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public Database getDatabase() {
- return db;
- }
- public void setDatabase(Database db) {
- this.db = db;
- }
- public Connection getConnection() {
- return connection;
- }
- public void setConnection(Connection connection) {
- this.connection = connection;
- }
- public ResultSet query(String query) {
- ResultSet rs = null;
- try {
- Statement state = this.connection.createStatement();
- rs = state.executeQuery(query);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return rs;
- }
- public void update(String query) {
- if (this.readOnly) {
- throw new RuntimeException("Failed lol (Read Only)");
- }
- try {
- Statement state = this.connection.createStatement();
- state.executeQuery(query);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void updatePrepared(String query) {
- if (this.readOnly) {
- throw new RuntimeException("Failed lol (Read Only)");
- }
- try {
- PreparedStatement state = this.connection.prepareStatement(query);
- state.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void clearWarnings() {
- try {
- this.connection.clearWarnings();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void deleteDatabase(Database db) {
- this.db = null;
- this.connection = null;
- }
- public void setReadOnly() {
- try {
- this.connection.setReadOnly(true);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void close() {
- try {
- this.connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void createTable() {
- try {
- final PreparedStatement sql = HungerGames.getInstance().getSQL().getConnection()
- .prepareStatement("CREATE TABLE IF NOT EXISTS "
- + HungerGames.getInstance().getConfig().getString("mysql.table")
- + " (id INT(11) NOT NULL AUTO_INCREMENT,"
- + "name VARCHAR(255) NOT NULL,"
- + "kills INT(11) NOT NULL,"
- + "games INT(11) NOT NULL,"
- + "wins INT(11) NOT NULL,"
- + "PRIMARY KEY (id));");
- sql.executeUpdate();
- sql.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement