Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.riderstorm.WarnMe;
- import java.io.File;
- import java.io.IOException;
- 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
- {
- private String host;
- private int port;
- private String database;
- private String username;
- private String password;
- private Connection conn;
- public WarnSystem plugin;
- public MySQL(WarnSystem plugin) {
- this.plugin = plugin;
- }
- public MySQL() throws Exception {
- File file = new File("plugins/WarnSystem/", "database.yml");
- FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
- String db = "database.";
- cfg.addDefault(db + "host", "localhost");
- cfg.addDefault(db + "port", 3306);
- cfg.addDefault(db + "user", "user");
- cfg.addDefault(db + "password", "password");
- cfg.addDefault(db + "database", "database");
- cfg.addDefault(db + "autoReconnect", true);
- cfg.options().copyDefaults(true);
- try
- {
- cfg.save(file);
- } catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- this.host = cfg.getString(db + "host");
- this.port = cfg.getInt(db + "port");
- this.username = cfg.getString(db + "user");
- this.database = cfg.getString(db + "database");
- this.password = cfg.getString(db + "password");
- this.conn = this.openConnection();
- if(this.conn == null) {
- this.plugin.getServer().getPluginManager().disablePlugin(this.plugin);
- }
- }
- public Connection getConnection() {
- return this.conn;
- }
- public void queryUpdate(String query) {
- PreparedStatement st = null;
- try
- {
- st = this.conn.prepareStatement(query);
- st.executeUpdate();
- } catch (SQLException e)
- {
- System.out.println("Failed to send query update '" + query + "'.");
- } finally {
- this.closeRessources(null, st);
- }
- }
- public void closeConnection() {
- try
- {
- this.conn.close();
- } catch (SQLException e)
- {
- e.printStackTrace();
- } finally {
- this.conn = null;
- }
- }
- public boolean hasConnection() {
- try
- {
- return this.conn != null || this.conn.isValid(1);
- } catch (SQLException e)
- {
- return false;
- }
- }
- public void closeRessources(ResultSet rs, PreparedStatement st) {
- if(rs != null) {
- try {
- rs.close();
- } catch(SQLException e) {
- }
- }
- if(st != null) {
- try {
- st.close();
- } catch(SQLException e) {
- }
- }
- }
- public Connection openConnection() {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
- this.conn = con;
- return conn;
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement