Utilize NovaUtils

This commit is contained in:
NovaFox161
2018-09-02 22:05:36 -05:00
parent 9f93d81ec6
commit 555b898ea2
6 changed files with 15 additions and 222 deletions
+12
View File
@@ -29,6 +29,10 @@
</properties>
<repositories>
<repository>
<id>nova-public</id>
<url>http://repo.novafox161.com/repository/nova-public/</url>
</repository>
<repository>
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
@@ -57,6 +61,14 @@
<artifactId>annotations</artifactId>
<version>RELEASE</version>
</dependency>
<!--NovaUtils API-->
<dependency>
<groupId>org.dreamexposure</groupId>
<artifactId>NovaUtils</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!--Discord4J API-->
<dependency>
<groupId>com.github.discord4j</groupId>
@@ -1,103 +0,0 @@
package com.cloudcraftgaming.discal.api.database;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Created by Nova Fox on 11/10/17.
* Website: www.cloudcraftgaming.com
* For Project: DisCal-Discord-Bot
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public abstract class Database {
protected Connection connection;
/**
* Creates a new Database
*/
protected Database() {
this.connection = null;
}
/**
* Opens a connection with the database
*
* @return Opened connection
* @throws SQLException if the connection can not be opened
* @throws ClassNotFoundException if the driver cannot be found
*/
public abstract Connection openConnection() throws SQLException, ClassNotFoundException;
/**
* Checks if a connection is open with the database
*
* @return true if the connection is open
* @throws SQLException if the connection cannot be checked
*/
public boolean checkConnection() throws SQLException {
return connection != null && !connection.isClosed();
}
/**
* Gets the connection with the database
*
* @return Connection with the database, null if none
*/
public Connection getConnection() {
return connection;
}
/**
* Closes the connection with the database
*
* @return true if successful
* @throws SQLException if the connection cannot be closed
*/
public boolean closeConnection() throws SQLException {
if (connection == null)
return false;
connection.close();
return true;
}
/**
* Executes a SQL Query<br>
* <p>
* If the connection is closed, it will be opened
*
* @param query Query to be run
* @return the results of the query
* @throws SQLException If the query cannot be executed
* @throws ClassNotFoundException If the driver cannot be found; see {@link #openConnection()}
*/
public ResultSet querySQL(String query) throws SQLException, ClassNotFoundException {
if (!checkConnection())
openConnection();
Statement statement = connection.createStatement();
return statement.executeQuery(query);
}
/**
* Executes an Update SQL Query<br>
* See {@link java.sql.Statement#executeUpdate(String)}<br>
* If the connection is closed, it will be opened
*
* @param query Query to be run
* @return Result Code, see {@link java.sql.Statement#executeUpdate(String)}
* @throws SQLException If the query cannot be executed
* @throws ClassNotFoundException If the driver cannot be found; see {@link #openConnection()}
*/
public int updateSQL(String query) throws SQLException, ClassNotFoundException {
if (!checkConnection())
openConnection();
Statement statement = connection.createStatement();
return statement.executeUpdate(query);
}
}
@@ -1,5 +1,7 @@
package com.cloudcraftgaming.discal.api.database;
import org.dreamexposure.novautils.database.MySQL;
import java.sql.Connection;
/**
@@ -11,6 +11,7 @@ import com.cloudcraftgaming.discal.api.object.event.EventData;
import com.cloudcraftgaming.discal.api.object.event.RsvpData;
import com.cloudcraftgaming.discal.api.object.web.UserAPIAccount;
import com.cloudcraftgaming.discal.logger.Logger;
import org.dreamexposure.novautils.database.MySQL;
import java.sql.*;
import java.util.ArrayList;
@@ -1,69 +0,0 @@
package com.cloudcraftgaming.discal.api.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Created by Nova Fox on 11/10/17.
* Website: www.cloudcraftgaming.com
* For Project: DisCal-Discord-Bot
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public class MySQL extends Database {
private final String user;
private final String database;
private final String password;
private final String port;
private final String hostname;
private final String prefix;
/**
* Creates a new MySQL instance
*
* @param hostname Name of the host
* @param port Port number
* @param username Username
* @param password Password
*/
public MySQL(String hostname, String port, String prefix, String username, String password) {
this(hostname, port, null, prefix, username, password);
}
/**
* Creates a new MySQL instance for a specific database
*
* @param hostname Name of the host
* @param port Port number
* @param database Database name
* @param username Username
* @param password Password
*/
public MySQL(String hostname, String port, String database, String prefix, String username, String password) {
this.hostname = hostname;
this.port = port;
this.database = database;
this.user = username;
this.password = password;
this.prefix = prefix;
}
@Override
public Connection openConnection() throws SQLException, ClassNotFoundException {
if (checkConnection())
return connection;
String connectionURL = "jdbc:mysql://" + this.hostname + ":" + this.port;
if (database != null)
connectionURL = connectionURL + "/" + this.database + "?autoReconnect=true&useSSL=false";
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, this.user, this.password);
return connection;
}
public String getPrefix() {
return prefix;
}
}
@@ -1,50 +0,0 @@
package com.cloudcraftgaming.discal.api.database;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Created by Nova Fox on 11/10/17.
* Website: www.cloudcraftgaming.com
* For Project: DisCal-Discord-Bot
*/
@SuppressWarnings({"unused", "ResultOfMethodCallIgnored"})
public class SQLite extends Database {
private final String dbLocation;
/**
* Creates a new SQLite instance
*
* @param dbLocation Location of the Database (Must end in .db)
*/
public SQLite(String dbLocation) {
this.dbLocation = dbLocation;
}
@Override
public Connection openConnection() throws SQLException, ClassNotFoundException {
if (checkConnection())
return connection;
File dataFolder = new File("sqlite-db/");
if (!dataFolder.exists())
dataFolder.mkdirs();
File file = new File(dataFolder, dbLocation);
if (!(file.exists())) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("Unable to create database!");
}
}
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + dataFolder + "/" + dbLocation);
return connection;
}
}