Advertisement
Guest User

MySQLManager (Async)

a guest
Apr 21st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. package io.coding4you.ban.util;
  2.  
  3. /*
  4.  *
  5.  * (c) by coding4you.io you are not permit to sell it!
  6.  *
  7.  */
  8.  
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.ChatColor;
  11.  
  12. import java.sql.*;
  13.  
  14. public class MySQL {
  15.    
  16.    Connection connection;
  17.    
  18.    public MySQL(String host, String username, String password, String database){
  19.      
  20.       try{
  21.    
  22.          Class.forName("com.mysql.jdbc.Driver");
  23.          setConnection(DriverManager.getConnection("jdbc:mysql://" + host + ":"
  24.             + 3306 + "/" + database, username, password));
  25.          
  26.          if(!getConnection().isClosed()){
  27.             Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "MYSQL connected!");
  28.          }else {
  29.             Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "MYSQL disconnected!");
  30.          }
  31.          
  32.       }catch(SQLException ex){
  33.          
  34.          ex.printStackTrace();
  35.          
  36.       }catch(ClassNotFoundException ex){
  37.          
  38.          ex.printStackTrace();
  39.          
  40.       }
  41.      
  42.      
  43.    }
  44.    
  45.    public MySQL(String host, String username, String password, String database, int port){
  46.    
  47.       try{
  48.      
  49.          Class.forName("com.mysql.jdbc.Driver");
  50.          setConnection(DriverManager.getConnection("jdbc:mysql://" + host + ":"
  51.             + port + "/" + database, username, password));
  52.      
  53.       }catch(SQLException ex){
  54.      
  55.          ex.printStackTrace();
  56.      
  57.       }catch(ClassNotFoundException ex){
  58.      
  59.          ex.printStackTrace();
  60.      
  61.       }
  62.    
  63.    }
  64.    
  65.    public Connection getConnection(){
  66.       return connection;
  67.    }
  68.    
  69.    public void setConnection(Connection connection){
  70.       this.connection = connection;
  71.    }
  72.    
  73.    public void createTable(String name, String value){
  74.      
  75.       try{
  76.    
  77.          PreparedStatement statement = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS " + name + " (" + value + ")");
  78.          statement.execute();
  79.          
  80.       }catch(SQLException ex){
  81.          
  82.          ex.printStackTrace();
  83.          
  84.       }
  85.      
  86.    }
  87.    
  88.    public Object get(String table, String from, String value, String get){
  89.    
  90.       try{
  91.    
  92.          PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM " + table + " WHERE "+ from + "=?");
  93.          statement.setString(1, value);
  94.    
  95.          ResultSet resultSet = statement.executeQuery();
  96.          resultSet.next();
  97.          return resultSet.getObject(get);
  98.          
  99.       }catch(SQLException ex){
  100.          
  101.          ex.printStackTrace();
  102.          
  103.       }
  104.      
  105.       return null;
  106.      
  107.    }
  108.    
  109.    public void update(String table, String a, String b,String what, String value){
  110.      
  111.       try{
  112.          
  113.          PreparedStatement statement = getConnection().prepareStatement("UPDATE FROM " + table + " " + what + "=?" + " WHERE" + a + "=?");
  114.          statement.setString(1, value);
  115.          statement.setString(2, b);
  116.          
  117.          ResultSet resultSet = statement.executeQuery();
  118.          
  119.       }catch(SQLException ex){
  120.          
  121.          ex.printStackTrace();
  122.          
  123.       }
  124.      
  125.    }
  126.    
  127.    public void disconnect(){
  128.      
  129.       if(isConnected()){
  130.          
  131.          try{
  132.            
  133.             getConnection().close();
  134.            
  135.             Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "MYSQL disconnected");
  136.    
  137.          }catch(SQLException ex){
  138.            
  139.             ex.printStackTrace();
  140.            
  141.          }
  142.          
  143.       }
  144.      
  145.    }
  146.    
  147.    public boolean isConnected(){
  148.      
  149.       try{
  150.    
  151.          if(!getConnection().isClosed()){
  152.            
  153.             return true;
  154.            
  155.          }
  156.          
  157.       }catch(SQLException ex){
  158.          
  159.          ex.printStackTrace();
  160.          
  161.       }
  162.      
  163.       return false;
  164.      
  165.    }
  166.    
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement