Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. package com.alta189.sqllitelib;
  2.  
  3. import java.io.File;
  4. import java.sql.Connection;
  5. import java.sql.DatabaseMetaData;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. public class DatabaseHandler {
  12.     /*
  13.      * @author: alta189
  14.      *
  15.      */
  16.    
  17.     private sqlCore core;
  18.     private Connection connection;
  19.     private File SQLFile;
  20.    
  21.     public DatabaseHandler(sqlCore core, File SQLFile) {
  22.         this.core = core;
  23.         this.SQLFile = SQLFile;
  24.     }
  25.  
  26.     public Connection getConnection() {
  27.         if (connection == null) {
  28.               initialize();
  29.         }
  30.         return connection;
  31.     }
  32.    
  33.     public void closeConnection() {
  34.         if (this.connection != null)
  35.           try {
  36.             this.connection.close();
  37.           } catch (SQLException ex) {
  38.             this.core.writeError("Error on Connection close: " + ex, true);
  39.           }
  40.       }
  41.    
  42.     public Boolean initialize() {
  43.         try {
  44.           Class.forName("org.sqlite.JDBC");
  45.           connection = DriverManager.getConnection("jdbc:sqlite:" + SQLFile.getAbsolutePath());
  46.           return true;
  47.         } catch (SQLException ex) {
  48.           core.writeError("SQLite exception on initialize " + ex, true);
  49.         } catch (ClassNotFoundException ex) {
  50.           core.writeError("You need the SQLite library " + ex, true);
  51.         }
  52.         return false;
  53.     }
  54.    
  55.     public Connection getNewConnection() {
  56.         try {
  57.               Class.forName("org.sqlite.JDBC");
  58.               return DriverManager.getConnection("jdbc:sqlite:" + SQLFile.getAbsolutePath());
  59.             } catch (SQLException ex) {
  60.               core.writeError("SQLite exception on initialize " + ex, true);
  61.             } catch (ClassNotFoundException ex) {
  62.               core.writeError("You need the SQLite library " + ex, true);
  63.             }
  64.             return null;
  65.     }
  66.    
  67.     public Boolean createTable(String query) {
  68.         try {
  69.             if (query == null) { core.writeError("SQL Create Table query empty.", true); return false; }
  70.             Connection con = this.getNewConnection();
  71.             Statement statement = con.createStatement();
  72.             statement.execute(query);
  73.             con.close();
  74.             return true;
  75.         } catch (SQLException ex){
  76.             core.writeError(ex.getMessage(), true);
  77.             return false;
  78.         }
  79.     }
  80.    
  81.     public ResultSet sqlQuery(String query) {
  82.         try {
  83.             Connection con = getNewConnection();
  84.             Statement statement = con.createStatement();
  85.            
  86.             ResultSet result = statement.executeQuery(query);
  87.             con.close();
  88.             return result;
  89.         } catch (SQLException ex) {
  90.             core.writeError("Error at SQL Query: " + ex.getMessage(), false);
  91.         }
  92.         return null;
  93.     }
  94.    
  95.     public void insertQuery(String query) {
  96.         try {
  97.             Connection con = getNewConnection();
  98.             Statement statement = con.createStatement();           
  99.             statement.executeQuery(query);
  100.             con.close();
  101.            
  102.         } catch (SQLException ex) {
  103.             if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL INSERT Query: " + ex, false);
  104.            
  105.         }
  106.     }
  107.    
  108.     public void updateQuery(String query) {
  109.         try {
  110.             Connection con = getNewConnection();
  111.             Statement statement = con.createStatement();           
  112.             statement.executeQuery(query);
  113.             con.close();
  114.            
  115.         } catch (SQLException ex) {
  116.             if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL UPDATE Query: " + ex.getMessage(), false);
  117.         }
  118.     }
  119.    
  120.     public void deleteQuery(String query) {
  121.         try {
  122.             Connection con = getNewConnection();
  123.             Statement statement = con.createStatement();
  124.            
  125.             statement.executeQuery(query);
  126.             con.close();
  127.            
  128.         } catch (SQLException ex) {
  129.             if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL DELETE Query: " + ex.getMessage(), false);
  130.         }
  131.     }
  132.    
  133.     public Boolean wipeTable(String table) {
  134.         try {
  135.             if (!core.checkTable(table)) {
  136.                 core.writeError("Error at Wipe Table: table, " + table + ", does not exist", true);
  137.                 return false;
  138.             }
  139.             Connection con = getNewConnection();
  140.             Statement statement = con.createStatement();
  141.             String query = "DELETE FROM " + table + ";";
  142.             statement.executeQuery(query);
  143.             con.close();
  144.             return true;
  145.         } catch (SQLException ex) {
  146.             if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL UPDATE Query: " + ex.getMessage(), false);
  147.             return false;
  148.         }
  149.     }
  150.    
  151.    
  152.     public Boolean checkTable(String table) {
  153.         DatabaseMetaData dbm;
  154.         try {
  155.             Connection con = getNewConnection();
  156.             dbm = con.getMetaData();
  157.             ResultSet tables = dbm.getTables(null, null, table, null);
  158.             con.close();
  159.             if (tables.next()) {
  160.               return true;
  161.             }
  162.             else {
  163.               return false;
  164.             }
  165.         } catch (SQLException e) {
  166.             // TODO Auto-generated catch block
  167.             core.writeError("Failed to check if table \"" + table + "\" exists: " + e.getMessage(), true);
  168.             return false;
  169.         }
  170.        
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement