Advertisement
Guest User

Untitled

a guest
May 24th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. package com.gcb.lobbyselector.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. import com.gcb.lobbyselector.Main;
  8.  
  9. public class Conexao {
  10.     private static String usuario = Main.plugin.getConfig().getString("MySQL.Usuario");
  11.     private static String senha = Main.plugin.getConfig().getString("MySQL.Senha");
  12.     private static String banco = Main.plugin.getConfig().getString("MySQL.Database");
  13.     private static String ip = Main.plugin.getConfig().getString("MySQL.IP");
  14.     private static String driver = "com.mysql.jdbc.Driver";
  15.     private static Connection conexao = null;
  16.  
  17.     public static Connection getConnection() {
  18.         try {
  19.             Class.forName(driver);
  20.             if (conexao == null || conexao.isClosed()) {
  21.                 conexao = DriverManager.getConnection("jdbc:mysql://" + ip + "/" + banco, usuario, senha);
  22.             }
  23.             return conexao;
  24.         }
  25.         catch (ClassNotFoundException e) {
  26.             throw new RuntimeException(e);
  27.         }
  28.         catch (SQLException e) {
  29.             Conexao.closeConnection();
  30.             throw new RuntimeException(e);
  31.         }
  32.     }
  33.  
  34.     public static void closeConnection() {
  35.         try {
  36.             if (conexao != null && !conexao.isClosed()) {
  37.                 conexao.close();
  38.             }
  39.         }
  40.         catch (Exception e) {
  41.             e.printStackTrace();
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement