Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.server.mysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import org.bukkit.Bukkit;
- import com.server.Main;
- public class MysqlConnection {
- private Connection _con;
- public MysqlConnection() {
- }
- public synchronized void trySQLConnection() {
- try {
- Main.getInstance().getLogger().info("Conectando ao Banco de dados...");
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String conn = "jdbc:mysql://" + "localhost" + ":" + 3306 + "/" + "kitpvp"
- + "?autoReconnect=true&failOverReadOnly=false&maxReconnects=2147483647";
- this._con = DriverManager.getConnection(conn, "root", "");
- } catch (ClassNotFoundException ex) {
- Main.getInstance().getLogger().warning("MySQL Driver nao encontrado!");
- Bukkit.shutdown();
- } catch (SQLException ex) {
- Main.getInstance().getLogger().warning("Erro enquanto tentava conectar ao Mysql!");
- Bukkit.shutdown();
- } catch (Exception ex) {
- Main.getInstance().getLogger().warning("Erro desconhecido enquanto tentava conectar ao MySQL.");
- Bukkit.shutdown();
- }
- }
- public Connection getConnection() {
- return this._con;
- }
- public void SQLdisconnect() {
- try {
- if ((this._con != null) && (!this._con.isClosed())) {
- this._con.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void SQLQuery(final String sql) {
- ExecutorService executor = Executors.newCachedThreadPool();
- executor.execute(new Thread(new Runnable() {
- public void run() {
- try {
- Statement stmt = _con.createStatement();
- stmt.executeUpdate(sql);
- stmt.close();
- } catch (SQLException e) {
- Main.getInstance().getLogger().info("Erro ao tentar executar Query");
- Main.getInstance().getLogger().info(sql);
- Main.getInstance().getLogger().info(e.getMessage());
- }
- }
- }));
- executor.shutdown();
- }
- public void SQLQuerySync(String sql) {
- try {
- Statement stmt = _con.createStatement();
- stmt.executeUpdate(sql);
- stmt.close();
- } catch (SQLException e) {
- Main.getInstance().getLogger().info("Erro ao tentar executar Query");
- Main.getInstance().getLogger().info(sql);
- Main.getInstance().getLogger().info(e.getMessage());
- }
- }
- public void SQLQuerySyncNoLock(String sql) {
- try {
- Statement stmt = _con.createStatement();
- stmt.executeUpdate(sql);
- stmt.close();
- } catch (SQLException e) {
- Main.getInstance().getLogger().info("Erro ao tentar executar Query");
- Main.getInstance().getLogger().info(sql);
- Main.getInstance().getLogger().info(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement