Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package backer.database;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import java.sql.ResultSet;
- public class Database {
- private String databasePath;
- private Connection database;
- public Database(String name, String directory) throws FileNotFoundException {
- File databaseDirectory = new File(directory);
- if (databaseDirectory.exists() && databaseDirectory.isDirectory()) {
- if (directory.charAt(directory.length() - 1) == File.separatorChar) {
- this.databasePath = directory + name;
- } else {
- this.databasePath = directory + File.separator + name;
- }
- try {
- Class.forName("org.sqlite.JDBC");
- this.database = DriverManager.getConnection("jdbc:sqlite:" + this.databasePath);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- else {
- throw new FileNotFoundException("Database directory was not found");
- }
- }
- public int executeUpdate(String sql) {
- int updateResult = -1;
- try {
- Statement statement = this.database.createStatement();
- updateResult = statement.executeUpdate(sql);
- statement.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return updateResult;
- }
- public ResultSet executeQuery(String sql) {
- ResultSet resultSet = null;
- try {
- Statement statement = this.database.createStatement();
- resultSet = statement.executeQuery(sql);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return resultSet;
- }
- public void close() {
- try {
- this.database.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement