Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.96 KB | None | 0 0
  1. package com.alta189.sqlLibrary.MySQL;
  2.  
  3. import java.net.MalformedURLException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class DatabaseHandler {
  11.     private mysqlCore core;  
  12.     private Connection connection;
  13.     private String dblocation;
  14.     private String username;
  15.     private String password;
  16.     private String database;
  17.      
  18.      
  19.     public DatabaseHandler(mysqlCore core, String dbLocation, String database, String username, String password) {
  20.         this.core = core;
  21.         this.dblocation = dbLocation;
  22.         this.database = database;
  23.         this.username = username;
  24.         this.password = password;
  25.     }
  26.      
  27.     //DONT FORGOT U EDITED THIS :D
  28.     private boolean openConnection() throws MalformedURLException, InstantiationException, IllegalAccessException {
  29.         try {
  30.             Class.forName("com.mysql.jdbc.Driver");
  31.             connection = DriverManager.getConnection("jdbc:mysql://" + dblocation + "/" + database, username, password);
  32.             return true;
  33.         } catch (ClassNotFoundException e) {
  34.             core.writeError("ClassNotFoundException! " + e.getMessage(), true);
  35.             return false;
  36.         } catch (SQLException e) {
  37.             core.writeError("SQLException! " + e.getMessage(), true);
  38.             return false;
  39.         }
  40.     }
  41.  
  42.     public Boolean checkConnection() {
  43.         if (connection == null) {
  44.             try {
  45.                 return openConnection();
  46.             } catch (MalformedURLException ex) {
  47.                 core.writeError("MalformedURLException! " + ex.getMessage(), true);
  48.             } catch (InstantiationException ex) {
  49.                 core.writeError("InstantiationExceptioon! " + ex.getMessage(), true);
  50.             } catch (IllegalAccessException ex) {
  51.                 core.writeError("IllegalAccessException! " + ex.getMessage(), true);
  52.             }
  53.             return false;
  54.         }
  55.         return true;
  56.       }
  57.  
  58.     public void closeConnection() {
  59.         try {
  60.             if (connection != null)
  61.                 connection.close();
  62.         } catch (Exception e) {
  63.             core.writeError("Failed to close database connection! " + e.getMessage(), true);
  64.         }
  65.     }
  66.    
  67.     public Connection getConnection() throws MalformedURLException, InstantiationException, IllegalAccessException {
  68.         if (connection == null) {
  69.             openConnection();
  70.         }
  71.         return connection;
  72.     }
  73.    
  74.     public ResultSet sqlQuery(String query) throws MalformedURLException, InstantiationException, IllegalAccessException {
  75.         try {
  76.             Connection connection = getConnection();
  77.             Statement statement = connection.createStatement();
  78.            
  79.             ResultSet result = statement.executeQuery(query);
  80.            
  81.             return result;
  82.         } catch (SQLException ex) {
  83.             core.writeError("Error at SQL Query: " + ex.getMessage(), false);
  84.         }
  85.         return null;
  86.     }
  87.    
  88.     public void insertQuery(String query) throws MalformedURLException, InstantiationException, IllegalAccessException {
  89.         try {
  90.             Connection connection = getConnection();
  91.             Statement statement = connection.createStatement();
  92.            
  93.             statement.executeUpdate(query);
  94.            
  95.            
  96.         } catch (SQLException ex) {
  97.            
  98.                 if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL INSERT Query: " + ex, false);
  99.            
  100.            
  101.         }
  102.     }
  103.    
  104.     public void updateQuery(String query) throws MalformedURLException, InstantiationException, IllegalAccessException {
  105.         try {
  106.             Connection connection = getConnection();
  107.             Statement statement = connection.createStatement();
  108.            
  109.             statement.executeUpdate(query);
  110.            
  111.            
  112.         } catch (SQLException ex) {
  113.            
  114.                 if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL UPDATE Query: " + ex, false);
  115.            
  116.         }
  117.     }
  118.    
  119.     public void deleteQuery(String query) throws MalformedURLException, InstantiationException, IllegalAccessException {
  120.         try {
  121.             Connection connection = getConnection();
  122.             Statement statement = connection.createStatement();
  123.            
  124.             statement.executeUpdate(query);
  125.            
  126.            
  127.         } catch (SQLException ex) {
  128.            
  129.                 if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL DELETE Query: " + ex, false);
  130.            
  131.         }
  132.     }
  133.    
  134.     public Boolean checkTable(String table) throws MalformedURLException, InstantiationException, IllegalAccessException {
  135.         try {
  136.             Connection connection = getConnection();
  137.             Statement statement = connection.createStatement();
  138.            
  139.             ResultSet result = statement.executeQuery("SELECT * FROM " + table);
  140.            
  141.             if (result == null) return false;
  142.             if (result != null) return true;
  143.         } catch (SQLException ex) {
  144.             if (ex.getMessage().contains("exist")) {
  145.                 return false;
  146.             } else {
  147.                 core.writeError("Error at SQL Query: " + ex.getMessage(), false);
  148.             }
  149.         }
  150.        
  151.        
  152.         if (sqlQuery("SELECT * FROM " + table) == null) return true;
  153.         return false;
  154.     }
  155.    
  156.     public Boolean wipeTable(String table) throws MalformedURLException, InstantiationException, IllegalAccessException {
  157.         try {
  158.             if (!core.checkTable(table)) {
  159.                 core.writeError("Error at Wipe Table: table, " + table + ", does not exist", true);
  160.                 return false;
  161.             }
  162.             Connection connection = getConnection();
  163.             Statement statement = connection.createStatement();
  164.             String query = "DELETE FROM " + table + ";";
  165.             statement.executeUpdate(query);
  166.            
  167.             return true;
  168.         } catch (SQLException ex) {
  169.             if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL WIPE TABLE Query: " + ex, false);
  170.             return false;
  171.         }
  172.     }
  173.    
  174.     public Boolean createTable(String query) {
  175.         try {
  176.             if (query == null) { core.writeError("SQL Create Table query empty.", true); return false; }
  177.            
  178.             Statement statement = connection.createStatement();
  179.             statement.execute(query);
  180.             return true;
  181.         } catch (SQLException ex){
  182.             core.writeError(ex.getMessage(), true);
  183.             return false;
  184.         }
  185.     }
  186.    
  187.    
  188.     //xXN1pplefaceXx's Additions
  189.    
  190.    
  191.     private void openNewConnection() throws MalformedURLException, InstantiationException, IllegalAccessException {
  192.         try {
  193.             Class.forName("com.mysql.jdbc.Driver");
  194.             connection = DriverManager.getConnection("jdbc:mysql://" + dblocation + "/", username, password);
  195.         } catch (ClassNotFoundException e) {
  196.             core.writeError("ClassNotFoundException! " + e.getMessage(), true);
  197.         } catch (SQLException e) {
  198.             core.writeError("SQLException! " + e.getMessage(), true);
  199.         }
  200.     }
  201.    
  202.     public Boolean checkNewConnection() {
  203.         if (connection == null) {
  204.             try {
  205.                 openNewConnection();
  206.                 return true;
  207.             } catch (MalformedURLException ex) {
  208.                 core.writeError("MalformedURLException! " + ex.getMessage(), true);
  209.             } catch (InstantiationException ex) {
  210.                 core.writeError("InstantiationExceptioon! " + ex.getMessage(), true);
  211.             } catch (IllegalAccessException ex) {
  212.                 core.writeError("IllegalAccessException! " + ex.getMessage(), true);
  213.             }
  214.             return false;
  215.         }
  216.         return true;
  217.       }
  218.    
  219.     public Connection getNewConnection() throws MalformedURLException, InstantiationException, IllegalAccessException {
  220.         if (connection == null) {
  221.             openNewConnection();
  222.         }
  223.         return connection;
  224.     }
  225.    
  226.    
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement