Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package me.Laubi.BigBrother.Util;
- import java.net.MalformedURLException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import me.Laubi.BigBrother.BigBrotherPlugin;
- /**
- *
- * @author Laubi
- */
- public class MySqlHandler {
- private BigBrotherPlugin plugin;
- private Connection connection;
- private boolean isopen;
- public MySqlHandler(BigBrotherPlugin instance){
- this.plugin=instance;
- this.isopen=false;
- this.connection=null;
- }
- public void init(String host,String port,String username,String password,String base){
- if(!isopen){
- try{
- this.openConnection(host, port, username, password, base);
- }catch(MalformedURLException e){
- this.plugin.log("MalformedURLException: "+e.getMessage(),Level.WARNING);
- this.isopen=false;
- } catch (InstantiationException e) {
- this.plugin.log("InstantiationException: "+e.getMessage(),Level.WARNING);
- this.isopen=false;
- } catch (IllegalAccessException e) {
- this.plugin.log("IllegalAccessException: "+e.getMessage(),Level.WARNING);
- this.isopen=false;
- }
- }
- }
- public void init(MySqlConfig config){
- if(!isopen){
- try{
- this.openConnection(config.host, config.port, config.username, config.password, config.base);
- }catch(MalformedURLException e){
- this.plugin.log("MalformedURLException: "+e.getMessage(),Level.WARNING);
- this.isopen=false;
- } catch (InstantiationException e) {
- this.plugin.log("InstantiationException: "+e.getMessage(),Level.WARNING);
- this.isopen=false;
- } catch (IllegalAccessException e) {
- this.plugin.log("IllegalAccessException: "+e.getMessage(),Level.WARNING);
- this.isopen=false;
- }
- }
- }
- private void openConnection(String host,String port,String username,String password,String base)throws MalformedURLException, InstantiationException, IllegalAccessException{
- try{
- Class.forName("com.mysql.jdbc.Driver");
- this.connection= DriverManager.getConnection("jdbc:mysql://" + host +":"+port+ "/" + base, username, password);
- this.isopen=true;
- }catch(ClassNotFoundException e){
- this.plugin.log("ClassNotFoundException: "+e.getMessage(),Level.WARNING);
- this.isopen=false;
- }catch(SQLException e){
- this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
- this.isopen=false;
- }
- }
- public boolean isOpen(){
- return this.isopen;
- }
- public void close(){
- if(isopen){
- try{
- this.connection.close();
- }catch(SQLException e){
- this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
- }
- this.isopen=false;
- this.connection=null;
- }
- }
- public boolean hasTable(String table){
- if(isopen){
- try{
- PreparedStatement stat=this.connection.prepareStatement("SHOW TABLES LIKE ?;");
- stat.setString(1, table);
- ResultSet rs=stat.executeQuery();
- return rs.next();
- }catch(SQLException e){
- this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
- return false;
- }
- }
- return false;
- }
- public boolean clearTable(String table){
- if(isopen){
- try{
- Statement stat=this.connection.createStatement();
- stat.executeUpdate("CLEAR TABLE "+table+";");
- return true;
- }catch(SQLException e){
- this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
- return false;
- }
- }
- return false;
- }
- public void truncTable(String table){
- if(isopen){
- try{
- Statement stat=this.connection.createStatement();
- stat.executeUpdate("TRUNCATE TABLE "+table+";");
- }catch(SQLException e){
- this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
- }
- }
- }
- public boolean createTable(String query){
- if(query==null||query.isEmpty())return false;
- if(isopen){
- try{
- Statement stat=this.connection.createStatement();
- stat.execute(query);
- return true;
- }catch(SQLException e){
- this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
- return false;
- }
- }
- return false;
- }
- public boolean deleteQuery(String query){
- if(isopen){
- try{
- Statement stat=this.connection.createStatement();
- stat.execute(query);
- return true;
- }catch(SQLException e){
- this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
- return false;
- }
- }
- return false;
- }
- public boolean updateQuery(String query){
- if(isopen){
- try{
- Statement stat=this.connection.createStatement();
- stat.execute(query);
- return true;
- }catch(SQLException e){
- this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
- return false;
- }
- }
- return false;
- }
- public boolean insertQuery(String query){
- if(isopen){
- try{
- Statement stat=this.connection.createStatement();
- stat.execute(query);
- return true;
- }catch(SQLException e){
- this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
- return false;
- }
- }
- return false;
- }
- public ResultSet sqlQuery(String query){
- if(isopen){
- try{
- Statement stat=this.connection.createStatement();
- ResultSet rs=stat.executeQuery(query);
- return rs;
- }catch(SQLException e){
- this.plugin.log("SQLException: "+e.getMessage(),Level.WARNING);
- return null;
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement