Advertisement
Guest User

Untitled

a guest
Sep 30th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.81 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package me.Laubi.BigBrother.Util;
  6.  
  7. import java.net.MalformedURLException;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.logging.Level;
  15. import me.Laubi.BigBrother.BigBrotherPlugin;
  16.  
  17. /**
  18.  *
  19.  * @author Laubi
  20.  */
  21. public class MySqlHandler {
  22.     private BigBrotherPlugin plugin;
  23.     private Connection connection;
  24.     private boolean isopen;
  25.    
  26.     public MySqlHandler(BigBrotherPlugin instance){
  27.         this.plugin=instance;
  28.         this.isopen=false;
  29.         this.connection=null;
  30.     }
  31.    
  32.     public void init(String host,String port,String username,String password,String base){
  33.         if(!isopen){
  34.             try{
  35.                 this.openConnection(host, port, username, password, base);
  36.             }catch(MalformedURLException e){
  37.                 this.plugin.log("MalformedURLException: "+e.getMessage(),Level.WARNING);
  38.                 this.isopen=false;
  39.             } catch (InstantiationException e) {
  40.                 this.plugin.log("InstantiationException: "+e.getMessage(),Level.WARNING);
  41.                 this.isopen=false;
  42.             } catch (IllegalAccessException e) {
  43.                 this.plugin.log("IllegalAccessException: "+e.getMessage(),Level.WARNING);
  44.                 this.isopen=false;
  45.             }
  46.         }
  47.     }
  48.     public void init(MySqlConfig config){
  49.         if(!isopen){
  50.             try{
  51.                 this.openConnection(config.host, config.port, config.username, config.password, config.base);
  52.             }catch(MalformedURLException e){
  53.                 this.plugin.log("MalformedURLException: "+e.getMessage(),Level.WARNING);
  54.                 this.isopen=false;
  55.             } catch (InstantiationException e) {
  56.                 this.plugin.log("InstantiationException: "+e.getMessage(),Level.WARNING);
  57.                 this.isopen=false;
  58.             } catch (IllegalAccessException e) {
  59.                 this.plugin.log("IllegalAccessException: "+e.getMessage(),Level.WARNING);
  60.                 this.isopen=false;
  61.             }
  62.         }
  63.     }
  64.    
  65.     private void openConnection(String host,String port,String username,String password,String base)throws MalformedURLException, InstantiationException, IllegalAccessException{
  66.         try{
  67.             Class.forName("com.mysql.jdbc.Driver");
  68.             this.connection= DriverManager.getConnection("jdbc:mysql://" + host +":"+port+ "/" + base, username, password);
  69.             this.isopen=true;
  70.         }catch(ClassNotFoundException e){
  71.             this.plugin.log("ClassNotFoundException: "+e.getMessage(),Level.WARNING);
  72.             this.isopen=false;
  73.         }catch(SQLException e){
  74.             this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
  75.             this.isopen=false;
  76.         }
  77.     }
  78.    
  79.     public boolean isOpen(){
  80.         return this.isopen;
  81.     }
  82.     public void close(){
  83.         if(isopen){
  84.             try{
  85.                 this.connection.close();
  86.             }catch(SQLException e){
  87.                 this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
  88.             }
  89.             this.isopen=false;
  90.             this.connection=null;
  91.         }
  92.     }
  93.    
  94.     public boolean hasTable(String table){
  95.         if(isopen){
  96.             try{
  97.                 PreparedStatement stat=this.connection.prepareStatement("SHOW TABLES LIKE ?;");
  98.                 stat.setString(1, table);
  99.                 ResultSet rs=stat.executeQuery();
  100.                 return rs.next();
  101.             }catch(SQLException e){
  102.                 this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
  103.                 return false;
  104.             }
  105.         }
  106.         return false;
  107.     }
  108.    
  109.     public boolean clearTable(String table){
  110.         if(isopen){
  111.             try{
  112.                 Statement stat=this.connection.createStatement();
  113.                 stat.executeUpdate("CLEAR TABLE "+table+";");
  114.                 return true;
  115.             }catch(SQLException e){
  116.                 this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
  117.                 return false;
  118.             }
  119.         }
  120.         return false;
  121.     }
  122.     public void truncTable(String table){
  123.         if(isopen){
  124.             try{
  125.                 Statement stat=this.connection.createStatement();
  126.                 stat.executeUpdate("TRUNCATE TABLE "+table+";");
  127.             }catch(SQLException e){
  128.                 this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
  129.             }
  130.         }
  131.     }
  132.     public boolean createTable(String query){
  133.         if(query==null||query.isEmpty())return false;
  134.         if(isopen){
  135.             try{
  136.                 Statement stat=this.connection.createStatement();
  137.                 stat.execute(query);
  138.                 return true;
  139.             }catch(SQLException e){
  140.                 this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
  141.                 return false;
  142.             }
  143.         }
  144.         return false;
  145.        
  146.     }
  147.     public boolean deleteQuery(String query){
  148.         if(isopen){
  149.             try{
  150.                 Statement stat=this.connection.createStatement();
  151.                 stat.execute(query);
  152.                 return true;
  153.             }catch(SQLException e){
  154.                 this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
  155.                 return false;
  156.             }
  157.         }
  158.         return false;
  159.     }
  160.     public boolean updateQuery(String query){
  161.         if(isopen){
  162.             try{
  163.                 Statement stat=this.connection.createStatement();
  164.                 stat.execute(query);
  165.                 return true;
  166.             }catch(SQLException e){
  167.                 this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
  168.                 return false;
  169.             }
  170.         }
  171.         return false;
  172.     }
  173.     public boolean insertQuery(String query){
  174.         if(isopen){
  175.             try{
  176.                 Statement stat=this.connection.createStatement();
  177.                 stat.execute(query);
  178.                 return true;
  179.             }catch(SQLException e){
  180.                 this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
  181.                 return false;
  182.             }
  183.         }
  184.         return false;
  185.     }
  186.     public ResultSet sqlQuery(String query){
  187.         if(isopen){
  188.             try{
  189.                 Statement stat=this.connection.createStatement();
  190.                 ResultSet rs=stat.executeQuery(query);
  191.                 return rs;
  192.             }catch(SQLException e){
  193.                 this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
  194.                 return null;
  195.             }
  196.         }
  197.         return null;
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement