Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 60 additions & 89 deletions src/com/pablo67340/SQLiteLib/Database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.pablo67340.SQLiteLib.Main.SQLiteLib;

public abstract class Database {

protected Connection connection;

public abstract Connection getSQLConnection();
Expand All @@ -31,17 +30,17 @@ public void initialize() {
close(ps, rs);

} catch (SQLException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, "Unable to retreive connection", ex);
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, "Unable to retrieve connection", ex);
}
}

/**
* <p>
* Execute any statement using this method. This will return a success or
* failure boolean.
* <p>
* </p>
*
* @param The
* statement to execute.
* @param statement The statement to execute.
*
* @return the {@link Database}'s success or failure (true/false).
*/
Expand All @@ -53,17 +52,14 @@ public Boolean executeStatement(String statement) {
ps = conn.prepareStatement(statement);
return !ps.execute();
} catch (SQLException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
SQLiteLib.log(Errors.sqlConnectionExecute(), ex);
return false;
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
if (ps != null) ps.close();
if (conn != null) conn.close();
} catch (SQLException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
return false;
SQLiteLib.log(Errors.sqlConnectionClose(), ex);
}
}
}
Expand All @@ -72,39 +68,31 @@ public Boolean executeStatement(String statement) {
* Get a single value from the database. Your If your statement returns multiple
* values, only the first value will return. Use queryRow for multiple values in
* 1 row.
* <p>
*
* @param The
* statement to execute.
*
* @param The
* row you would like to store data from.
* @param statement The statement to execute.
* @param row The row you would like to store data from.
*
* @return the {@link Database}'s Query in Object format. Casting required to
* change variables into their original form.
*/
public Object queryValue(String statement, String row) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
ResultSet rs;
try {
conn = getSQLConnection();
ps = conn.prepareStatement(statement);

rs = ps.executeQuery();
while (rs.next()) {
return rs.getObject(row);
}
if (rs.next()) return rs.getObject(row);
} catch (SQLException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
SQLiteLib.log(Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
if (ps != null) ps.close();
if (conn != null) conn.close();
} catch (SQLException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
SQLiteLib.log(Errors.sqlConnectionClose(), ex);
}
}
return null;
Expand All @@ -115,41 +103,36 @@ public Object queryValue(String statement, String row) {
* querying for multiple values, in 1 row. I.E If you had a row called test,
* with numbers 1 to 10. This method could be used to build a list containing
* the data 1 to 10 from this row.
* <p>
*
* @param The
* statement to execute.
*
* @param The
* row you would like to store data from.
*
* @return the {@link Database}'s Query in List<Object> format. Casting required
* to change variables into their original form.
* @param statement The statement to execute.
* @param row The row you would like to store data from.
*
* @return the {@link Database}'s Query in {@code List<Object>} format.
* Casting required to change variables into their original form.
*/
public List<Object> queryRow(String statement, String row) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
ResultSet rs;

List<Object> objects = new ArrayList<>();

try {
conn = getSQLConnection();
ps = conn.prepareStatement(statement);

rs = ps.executeQuery();
while (rs.next()) {
while (rs.next())
objects.add(rs.getObject(row));
}
return objects;
} catch (SQLException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
SQLiteLib.log(Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
if (ps != null) ps.close();
if (conn != null) conn.close();
} catch (SQLException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
SQLiteLib.log(Errors.sqlConnectionClose(), ex);
}
}
return null;
Expand All @@ -158,95 +141,83 @@ public List<Object> queryRow(String statement, String row) {
/**
* Get a map which contains lists for each row specified. For each row, a list
* is stored containing all the queried values.
*
* You can access the list for each row using <Row, List> format. The list is in
* List<Object> format.
* <p>
* You can access the list for each row using {@code <Row, List>} format. The list is in
* List<Object> format.
*
* @param The
* statement to execute.
*
* @param The
* row(s) you would like to store data from. Minimum 1 row required.
* @param statement The statement to execute.
* @param row The row(s) you would like to store data from. Minimum 1 row required.
*
* @return the {@link Database}'s Query in Map<Row,List<Object>> format. Casting
* @return the {@link Database}'s Query in {@code Map<Row,List<Object>>} format. Casting
* required to change variables into their original form.
*/
public Map<String, List<Object>> queryMultipleRows(String statement, String... row) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
ResultSet rs;

List<Object> objects = new ArrayList<>();
Map<String, List<Object>> map = new HashMap<>();

try {
conn = getSQLConnection();
ps = conn.prepareStatement(statement);

rs = ps.executeQuery();
while (rs.next()) {
for (String singleRow : row) {
for (String singleRow : row)
objects.add(rs.getObject(singleRow));
}

for (String singleRow : row) {
for (String singleRow : row)
map.put(singleRow, objects);
}

}

return map;
} catch (SQLException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
SQLiteLib.log(Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
if (ps != null) ps.close();
if (conn != null) conn.close();
} catch (SQLException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
SQLiteLib.log(Errors.sqlConnectionClose(), ex);
}
}
return null;
}


/**
* Close the current connection of the statement to the database.
* <p>
*
* @param The
* statement previously used.
*
* @param The
* result set that was returned from the statement.
*
* @param ps The statement previously used.
* @param rs The result set that was returned from the statement.
*/
public void close(PreparedStatement ps, ResultSet rs) {
try {
if (ps != null)
ps.close();
if (rs != null)
rs.close();
if (ps != null) ps.close();
if (rs != null) rs.close();
} catch (SQLException ex) {
Error.close(SQLiteLib.getInstance(), ex);
Error.close(ex);
}
}


/**
* Close the current connection to the database. The database will need to be
* re-initialized if this is used. When intializing using the main class, it
* will delete this current object and create a new object connected to the db.
* If you'd like to reload this db without trashing the database object, invoke
* the load() method through the global map of databases. E.g
* getDatabase("name").load();.
*
* Close the current connection to the database.
* <p>
* The database will need to be re-initialized if this is used. When
* initializing using the main class, it will delete this current object
* and create a new object connected to the db. If you'd like to reload
* this db without trashing the database object, invoke the {@link #load()}
* method through the global map of databases.
* <p>
* Ex: getDatabase("name").load();
*/
public void closeConnection() {
try {
connection.close();
} catch (SQLException e) {
Error.close(SQLiteLib.getInstance(), e);
} catch (SQLException ex) {
Error.close(ex);
}
}
}
46 changes: 29 additions & 17 deletions src/com/pablo67340/SQLiteLib/Database/SQLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,65 @@
import com.pablo67340.SQLiteLib.Main.SQLiteLib;

public class SQLite extends Database {
private final String dbname;
private final String createStatement;
private final File dataFolder;

private String dbname;

private String createTestTable = "CREATE TABLE IF NOT EXISTS test (" + "`test` varchar(32) NOT NULL,"
+ "PRIMARY KEY (`test`)" + ");";

private String customCreateString;

private File dataFolder;

/**
* Create a new SQLite database.
*
* @param databaseName the database's name.
* @param createStatement the statement to create our first table.
* @param folder the folder the database file will be created.
*/
public SQLite(String databaseName, String createStatement, File folder) {
dbname = databaseName;
customCreateString = createStatement;
dataFolder = folder;
this.dbname = databaseName;
this.createStatement = createStatement;
this.dataFolder = folder;
}

/**
* Get the connection to the database file.
*
* @return The connection to the database.
*/
public Connection getSQLConnection() {
File folder = new File(dataFolder, dbname + ".db");

if (!folder.exists()) {
try {
folder.createNewFile();
} catch (IOException e) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, "File write error: " + dbname + ".db");
}
}

try {
if (connection != null && !connection.isClosed()) {
return connection;
}
if (connection != null && !connection.isClosed()) return connection;
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + folder);
return connection;
} catch (SQLException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, "SQLite exception on initialize", ex);
SQLiteLib.log("SQLite exception on initialize", ex);
} catch (ClassNotFoundException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE,
"You need the SQLite JBDC library. Google it. Put it in /lib folder.");
}

return null;
}

/**
* Load the database.
*/
public void load() {
connection = getSQLConnection();
try {
Statement s = connection.createStatement();
String createTestTable = "CREATE TABLE IF NOT EXISTS test (" + "`test` varchar(32) NOT NULL,"
+ "PRIMARY KEY (`test`)" + ");";
s.executeUpdate(createTestTable);
s.executeUpdate(customCreateString);
s.executeUpdate(createStatement);
s.close();
} catch (SQLException e) {
e.printStackTrace();
Expand Down
10 changes: 4 additions & 6 deletions src/com/pablo67340/SQLiteLib/Error/Error.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.pablo67340.SQLiteLib.Error;

import java.util.logging.Level;

import com.pablo67340.SQLiteLib.Main.SQLiteLib;

public class Error {
public static void execute(SQLiteLib plugin, Exception ex) {
plugin.getLogger().log(Level.SEVERE, "Couldn't execute MySQL statement: ", ex);
public static void execute(Exception ex) {
SQLiteLib.log("Couldn't execute MySQL statement: ", ex);
}

public static void close(SQLiteLib plugin, Exception ex) {
plugin.getLogger().log(Level.SEVERE, "Failed to close MySQL connection: ", ex);
public static void close(Exception ex) {
SQLiteLib.log("Failed to close MySQL connection: ", ex);
}
}
2 changes: 1 addition & 1 deletion src/com/pablo67340/SQLiteLib/Error/Errors.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static String sqlConnectionClose() {
}

public static String noSQLConnection() {
return "Unable to retreive MYSQL connection: ";
return "Unable to retrieve MYSQL connection: ";
}

public static String noTableFound() {
Expand Down
Loading