Advertisement
Guest User

MySQLManager (Async)

a guest
Apr 21st, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 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 Object get(String table, String from, String value, String get){
  74.    
  75.       try{
  76.    
  77.          PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM " + table + " WHERE "+ from + "=?");
  78.          statement.setString(1, value);
  79.    
  80.          ResultSet resultSet = statement.executeQuery();
  81.          resultSet.next();
  82.          return resultSet.getObject(get);
  83.          
  84.       }catch(SQLException ex){
  85.          
  86.          ex.printStackTrace();
  87.          
  88.       }
  89.      
  90.       return null;
  91.      
  92.    }
  93.    
  94.    public void update(String table, String a, String b,String what, String value){
  95.      
  96.       try{
  97.          
  98.          PreparedStatement statement = getConnection().prepareStatement("UPDATE FROM " + table + " " + what + "=?" + " WHERE" + a + "=?");
  99.          statement.setString(1, value);
  100.          statement.setString(2, b);
  101.          
  102.          ResultSet resultSet = statement.executeQuery();
  103.          
  104.       }catch(SQLException ex){
  105.          
  106.          ex.printStackTrace();
  107.          
  108.       }
  109.      
  110.    }
  111.    
  112.    public void disconnect(){
  113.      
  114.       if(isConnected()){
  115.          
  116.          try{
  117.            
  118.             getConnection().close();
  119.    
  120.          }catch(SQLException ex){
  121.            
  122.             ex.printStackTrace();
  123.            
  124.          }
  125.       }
  126.      
  127.    }
  128.    
  129.    public boolean isConnected(){
  130.      
  131.       try{
  132.    
  133.          if(!getConnection().isClosed()){
  134.            
  135.             return true;
  136.            
  137.          }
  138.          
  139.       }catch(SQLException ex){
  140.          
  141.          ex.printStackTrace();
  142.          
  143.       }
  144.      
  145.       return false;
  146.      
  147.    }
  148.    
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement