Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.alta189.sqllitelib;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class DatabaseHandler {
- /*
- * @author: alta189
- *
- */
- private sqlCore core;
- private Connection connection;
- private File SQLFile;
- public DatabaseHandler(sqlCore core, File SQLFile) {
- this.core = core;
- this.SQLFile = SQLFile;
- }
- public Connection getConnection() {
- if (connection == null) {
- initialize();
- }
- return connection;
- }
- public void closeConnection() {
- if (this.connection != null)
- try {
- this.connection.close();
- } catch (SQLException ex) {
- this.core.writeError("Error on Connection close: " + ex, true);
- }
- }
- public Boolean initialize() {
- try {
- Class.forName("org.sqlite.JDBC");
- connection = DriverManager.getConnection("jdbc:sqlite:" + SQLFile.getAbsolutePath());
- return true;
- } catch (SQLException ex) {
- core.writeError("SQLite exception on initialize " + ex, true);
- } catch (ClassNotFoundException ex) {
- core.writeError("You need the SQLite library " + ex, true);
- }
- return false;
- }
- public Connection getNewConnection() {
- try {
- Class.forName("org.sqlite.JDBC");
- return DriverManager.getConnection("jdbc:sqlite:" + SQLFile.getAbsolutePath());
- } catch (SQLException ex) {
- core.writeError("SQLite exception on initialize " + ex, true);
- } catch (ClassNotFoundException ex) {
- core.writeError("You need the SQLite library " + ex, true);
- }
- return null;
- }
- public Boolean createTable(String query) {
- try {
- if (query == null) { core.writeError("SQL Create Table query empty.", true); return false; }
- Connection con = this.getNewConnection();
- Statement statement = con.createStatement();
- statement.execute(query);
- con.close();
- return true;
- } catch (SQLException ex){
- core.writeError(ex.getMessage(), true);
- return false;
- }
- }
- public ResultSet sqlQuery(String query) {
- try {
- Connection con = getNewConnection();
- Statement statement = con.createStatement();
- ResultSet result = statement.executeQuery(query);
- con.close();
- return result;
- } catch (SQLException ex) {
- core.writeError("Error at SQL Query: " + ex.getMessage(), false);
- }
- return null;
- }
- public void insertQuery(String query) {
- try {
- Connection con = getNewConnection();
- Statement statement = con.createStatement();
- statement.executeQuery(query);
- con.close();
- } catch (SQLException ex) {
- if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL INSERT Query: " + ex, false);
- }
- }
- public void updateQuery(String query) {
- try {
- Connection con = getNewConnection();
- Statement statement = con.createStatement();
- statement.executeQuery(query);
- con.close();
- } catch (SQLException ex) {
- if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL UPDATE Query: " + ex.getMessage(), false);
- }
- }
- public void deleteQuery(String query) {
- try {
- Connection con = getNewConnection();
- Statement statement = con.createStatement();
- statement.executeQuery(query);
- con.close();
- } catch (SQLException ex) {
- if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL DELETE Query: " + ex.getMessage(), false);
- }
- }
- public Boolean wipeTable(String table) {
- try {
- if (!core.checkTable(table)) {
- core.writeError("Error at Wipe Table: table, " + table + ", does not exist", true);
- return false;
- }
- Connection con = getNewConnection();
- Statement statement = con.createStatement();
- String query = "DELETE FROM " + table + ";";
- statement.executeQuery(query);
- con.close();
- return true;
- } catch (SQLException ex) {
- if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL UPDATE Query: " + ex.getMessage(), false);
- return false;
- }
- }
- public Boolean checkTable(String table) {
- DatabaseMetaData dbm;
- try {
- Connection con = getNewConnection();
- dbm = con.getMetaData();
- ResultSet tables = dbm.getTables(null, null, table, null);
- con.close();
- if (tables.next()) {
- return true;
- }
- else {
- return false;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- core.writeError("Failed to check if table \"" + table + "\" exists: " + e.getMessage(), true);
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement