Advertisement
Guest User

MySQLManager (noAsync)

a guest
Apr 21st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. package io.coding4you.ban.util;
  2.  
  3. import io.coding4you.ban.main.BanPlugin;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.configuration.file.YamlConfiguration;
  6.  
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.sql.*;
  10.  
  11. public class MySQLManager {
  12.    
  13.    static String host, username, password, database, port;
  14.    static String prefix = "§6MySQL";
  15.    private static Connection con;
  16.    
  17.    public MySQLManager(){
  18.      
  19.       File file = new File("plugins//" + BanPlugin.getInstance().getDescription().getName() + "//mysql//mysql.yml");
  20.       YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
  21.      
  22.       if(file.exists()){
  23.          
  24.          MySQLManager.host = configuration.getString("host");
  25.          MySQLManager.username = configuration.getString("username");
  26.          MySQLManager.password = configuration.getString("password");
  27.          MySQLManager.database = configuration.getString("database");
  28.          MySQLManager.port = configuration.getString("port");
  29.          
  30.       }else{
  31.          
  32.          try{
  33.            
  34.             configuration.set("host", "host");
  35.             configuration.set("username", "username");
  36.             configuration.set("password", "password");
  37.             configuration.set("database", "database");
  38.             configuration.set("port", "3306");
  39.            
  40.             configuration.save(file);
  41.            
  42.             MySQLManager.host = configuration.getString("host");
  43.             MySQLManager.username = configuration.getString("username");
  44.             MySQLManager.password = configuration.getString("password");
  45.             MySQLManager.database = configuration.getString("database");
  46.             MySQLManager.port = configuration.getString("port");
  47.            
  48.          }catch(IOException e){
  49.            
  50.             Bukkit.getConsoleSender().sendMessage(prefix + " MySQL file cannot be saved!");
  51.            
  52.          }
  53.          
  54.       }
  55.      
  56.    }
  57.    
  58.    public static boolean isConnected(){
  59.       return con != null;
  60.    }
  61.    
  62.    public void connect() throws SQLException{
  63.      
  64.       if(! isConnected()){
  65.          
  66.          con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
  67.          Bukkit.getConsoleSender().sendMessage(prefix + " connected!");
  68.          
  69.       }else{
  70.          
  71.          Bukkit.getConsoleSender().sendMessage(prefix + " already connected!");
  72.          
  73.       }
  74.      
  75.    }
  76.    
  77.    public void disconnect() throws SQLException{
  78.      
  79.       if(isConnected()){
  80.          
  81.          con.close();
  82.          Bukkit.getConsoleSender().sendMessage(prefix + " disconnected!");
  83.          
  84.       }
  85.      
  86.    }
  87.    
  88.    private ResultSet query(String qry) throws SQLException{
  89.       ResultSet rs = null;
  90.      
  91.       if(isConnected()){
  92.          
  93.          Statement st = con.createStatement();
  94.          rs = st.executeQuery(qry);
  95.          
  96.       }else{
  97.          
  98.          Bukkit.getConsoleSender().sendMessage(prefix + " failed, MySQL is connecting!");
  99.          connect();
  100.          
  101.       }
  102.      
  103.       return rs;
  104.    }
  105.    
  106.    public void update(String qry) throws SQLException{
  107.      
  108.       if(isConnected()){
  109.          
  110.          Statement st = con.createStatement();
  111.          st.executeUpdate(qry);
  112.          st.close();
  113.          
  114.       }else{
  115.          
  116.          Bukkit.getConsoleSender().sendMessage(prefix + " failed, MySQL is connecting!");
  117.          connect();
  118.          
  119.       }
  120.      
  121.    }
  122.    
  123.    public void createTable(String table, String data){
  124.      
  125.       try{
  126.          
  127.          update("CREATE TABLE IF NOT EXISTS " + table + "(" + data + ");");
  128.          
  129.       }catch(SQLException e){
  130.          
  131.          e.printStackTrace();
  132.          
  133.       }
  134.      
  135.    }
  136.    
  137.    public void insertIntoTable(String table, String a, String value){
  138.    
  139.       try{
  140.      
  141.          update("INSERT INTO " + table + " (" + a + ") VALUES ('" + value + "');");
  142.      
  143.       }catch(SQLException e){
  144.      
  145.          e.printStackTrace();
  146.      
  147.       }
  148.    
  149.    }
  150.    
  151.    public String getFromTable(String table, String from, String is, String get){
  152.    
  153.       try{
  154.      
  155.          ResultSet rs = query(("SELECT * FROM " + table + " WHERE " + from + "= '" + is + "'"));
  156.          if(rs.next()) return rs.getString(get);
  157.      
  158.       }catch(SQLException e){
  159.      
  160.          e.printStackTrace();
  161.      
  162.       }
  163.       return get;
  164.    }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement