package tk.disturbedgame.arcadeapi; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; public class MySQL { Main plugin; private String Host; private int Port; private String User; private String Pass; private String Data; private Connection connect; public MySQL() throws Exception { plugin = new Main(); FileConfiguration cfg = YamlConfiguration.loadConfiguration(Main.file); this.Host = cfg.getString("MySQL.Host"); this.Port = cfg.getInt("MySQL.Port"); this.User = cfg.getString("MySQL.Username"); this.Pass = cfg.getString("MySQL.Password"); this.Data = cfg.getString("MySQL.Database"); this.openConnection(); } public Connection openConnection() throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://" + Host + ":" + Port + "/" + Data, User, Pass); this.connect = con; return connect; } public Connection getConnection() { return this.connect; } public boolean hasConnection() { try { return this.connect != null || this.connect.isValid(1); } catch (SQLException e) { return false; } } public void queryUpdate(String query) { Connection con = this.connect; PreparedStatement st = null; try { st = con.prepareStatement(query); st.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { closeRessources(null, st); } } public ResultSet getQuery(String qry) { ResultSet rs = null; try { PreparedStatement stmt = connect.prepareStatement(qry); rs = stmt.executeQuery(qry); return rs; } catch (Exception ex) { ex.printStackTrace(); } return rs; } public void closeRessources(ResultSet rs, PreparedStatement st) { if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void closeConnection() { try { this.connect.close(); } catch (SQLException e) { e.printStackTrace(); } finally { this.connect = null; } } }