2014-09-23 19:02:17 +02:00
|
|
|
package com.intellectualcrafters.plot.database;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2014-09-24 22:21:43 +10:00
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.Statement;
|
2014-09-23 19:02:17 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
|
2014-09-24 22:21:43 +10:00
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
|
2014-09-23 19:02:17 +02:00
|
|
|
/**
|
|
|
|
* Connects to and uses a SQLite database
|
2014-10-11 00:33:10 -07:00
|
|
|
*
|
2014-09-23 19:02:17 +02:00
|
|
|
* @author Citymonstret
|
|
|
|
* @author tips48
|
|
|
|
*/
|
|
|
|
public class SQLite extends Database {
|
|
|
|
|
2014-10-11 00:33:10 -07:00
|
|
|
private Connection connection;
|
|
|
|
private final String dbLocation;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new SQLite instance
|
|
|
|
*
|
|
|
|
* @param plugin
|
|
|
|
* Plugin instance
|
|
|
|
* @param dbLocation
|
|
|
|
* Location of the Database (Must end in .db)
|
|
|
|
*/
|
|
|
|
public SQLite(Plugin plugin, String dbLocation) {
|
|
|
|
super(plugin);
|
|
|
|
this.dbLocation = dbLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-10-12 00:37:36 -07:00
|
|
|
public Connection openConnection() throws SQLException, ClassNotFoundException {
|
2014-10-11 00:33:10 -07:00
|
|
|
if (checkConnection()) {
|
|
|
|
return this.connection;
|
|
|
|
}
|
|
|
|
if (!this.plugin.getDataFolder().exists()) {
|
|
|
|
this.plugin.getDataFolder().mkdirs();
|
|
|
|
}
|
|
|
|
File file = new File(this.plugin.getDataFolder(), this.dbLocation);
|
|
|
|
if (!(file.exists())) {
|
|
|
|
try {
|
|
|
|
file.createNewFile();
|
2014-10-12 00:37:36 -07:00
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
this.plugin.getLogger().log(Level.SEVERE, "Unable to create database!");
|
2014-10-11 00:33:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Class.forName("org.sqlite.JDBC");
|
2014-10-12 00:37:36 -07:00
|
|
|
this.connection =
|
|
|
|
DriverManager.getConnection("jdbc:sqlite:" + this.plugin.getDataFolder().toPath().toString() + "/"
|
|
|
|
+ this.dbLocation);
|
2014-10-11 00:33:10 -07:00
|
|
|
return this.connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean checkConnection() throws SQLException {
|
|
|
|
return (this.connection != null) && !this.connection.isClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Connection getConnection() {
|
|
|
|
return this.connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean closeConnection() throws SQLException {
|
|
|
|
if (this.connection == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.connection.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-10-12 00:37:36 -07:00
|
|
|
public ResultSet querySQL(String query) throws SQLException, ClassNotFoundException {
|
2014-10-11 00:33:10 -07:00
|
|
|
if (checkConnection()) {
|
|
|
|
openConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
Statement statement = this.connection.createStatement();
|
|
|
|
|
|
|
|
ResultSet result = statement.executeQuery(query);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-10-12 00:37:36 -07:00
|
|
|
public int updateSQL(String query) throws SQLException, ClassNotFoundException {
|
2014-10-11 00:33:10 -07:00
|
|
|
if (checkConnection()) {
|
|
|
|
openConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
Statement statement = this.connection.createStatement();
|
|
|
|
|
|
|
|
int result = statement.executeUpdate(query);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2014-09-24 22:21:43 +10:00
|
|
|
}
|