Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.katho.herolandshg.Mysql;
- 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 ip;
- private int porta;
- private String usuario;
- private String senha;
- private String banco;
- private Connection conn;
- public MySQL() throws Exception {
- File file = new File("plugins/cHardcoreGames/", "banco.yml");
- FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
- String db = "bancodedados.";
- cfg.addDefault(db + "ip", "talalal.tk");
- cfg.addDefault(db + "porta", 3306);
- cfg.addDefault(db + "usuario", "talala");
- cfg.addDefault(db + "senha", "talala");
- cfg.addDefault(db + "banco", "talala");
- cfg.options().copyDefaults(true);
- try {
- cfg.save(file);
- } catch (IOException e) {
- e.printStackTrace();
- }
- this.ip = cfg.getString(db + "ip");
- this.porta = cfg.getInt(db + "porta");
- this.usuario = cfg.getString(db + "usuario");
- this.senha = cfg.getString(db + "senha");
- this.banco = cfg.getString(db + "banco");
- this.openConnection();
- }
- public Connection openConnection() throws Exception {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection("jdbc:mysql://" + this.ip + ":" + this.porta + "/" + this.banco, this.usuario, this.senha);
- this.conn = conn;
- return conn;
- }
- public Connection getConnection() {
- return this.conn;
- }
- public boolean hasConnection() {
- try {
- return this.conn != null || this.conn.isValid(1);
- } catch (SQLException e) {
- return false;
- }
- }
- public void queryUpdate(String query) {
- Connection conn = this.conn;
- PreparedStatement st = null;
- try {
- st = conn.prepareStatement(query);
- st.executeUpdate();
- } catch (SQLException e) {
- System.err.println("Failed to send update '" + query + "'.");
- } finally {
- this.closeRessources(null, st);
- }
- }
- public void refreshConnection() {
- PreparedStatement st = null;
- ResultSet valid = null;
- try
- {
- st = conn.prepareStatement("SELECT 1 FROM Dual");
- valid = st.executeQuery();
- if (valid.next())
- return;
- } catch (SQLException e2)
- {
- System.out.println("Connection is idle or terminated. Reconnecting...");
- } finally
- {
- this.closeRessources(valid, st);
- }
- long start = 0;
- long end = 0;
- try
- {
- start = System.currentTimeMillis();
- System.out.println("Attempting to establish a connection the MySQL server!");
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://" + this.ip + ":" + this.porta + "/" + this.banco, this.usuario, this.senha);
- end = System.currentTimeMillis();
- System.out.println("Conexao com mysql estabilizado!");
- System.out.println(((end - start)) + "ms!");
- } catch (SQLException e)
- {
- System.out.println("Could not connect to MySQL server! because: " + e.getMessage());
- } catch (ClassNotFoundException e)
- {
- System.out.println("JDBC Driver not found!");
- }
- }
- 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 void closeConnection() {
- try {
- this.conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- this.conn = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement