Refactored code and fixed bugs

This commit is contained in:
Anthony committed 2018-04-16 09:52:37 -04:00
1 parent a5354a5793
commit fe0ce72492
4 files changed
+26 -15

No files matched your search

@@ -48,7 +48,6 @@ public class MusicBot {
ConsoleHandler consoleInput = new ConsoleHandler(); ConsoleHandler consoleInput = new ConsoleHandler();
try {
switch (consoleInput.init()) { switch (consoleInput.init()) {
case "quit": case "quit":
@@ -62,9 +61,6 @@ public class MusicBot {
break; break;
} }
} catch (IOException e) {
e.printStackTrace();
}
} }
@@ -118,7 +118,16 @@ public class MysqlDatabase {
return query.executeQuery(); return query.executeQuery();
} }
// https://github.com/Kaaz/DiscordBot/blob/master/src/main/java/emily/db/MySQLAdapter.java#resolveParameters //
/**
* Insert parameters (specified with a ?) in the SQL statement
* Taken from https://github.com/Kaaz/DiscordBot/blob/master/src/main/java/emily/db/MySQLAdapter.java#resolveParameters
*
* @param query The PreparedStatement object
* @param params The parameters to be included in the prepared statement
* @throws SQLException
*/
private void resolveParameters(PreparedStatement query, Object... params) throws SQLException { private void resolveParameters(PreparedStatement query, Object... params) throws SQLException {
int index = 1; int index = 1;
for (Object p : params) { for (Object p : params) {
@@ -163,7 +172,7 @@ public class MysqlDatabase {
} }
/** /**
* Recreate the MySQL connection. * Recreate and reconnect the MySQL connection.
*/ */
public void reconnect() { public void reconnect() {
@@ -193,7 +202,7 @@ public class MysqlDatabase {
* <br>The first row of the array is the names of the columns. * <br>The first row of the array is the names of the columns.
* <br>The other rows are their corresponding value * <br>The other rows are their corresponding value
* *
* * @deprecated Use {@link #convertToTableModel(ResultSet)} instead
* @param rs The ResultSet returned by the SELECT query * @param rs The ResultSet returned by the SELECT query
* @return an array of Object with the table structure * @return an array of Object with the table structure
* @throws SQLException * @throws SQLException
@@ -218,8 +227,9 @@ public class MysqlDatabase {
} }
try { try {
if (array[1][0] == null) if (array[1][0] == null) {
nbRow = 0; nbRow = 0;
}
} catch (ArrayIndexOutOfBoundsException ignored) { } catch (ArrayIndexOutOfBoundsException ignored) {
} }
@@ -231,7 +241,7 @@ public class MysqlDatabase {
} }
/** /**
* @param rs * @param rs The ResultSet returned by the SELECT query
* @return a {@link MTableModel} with the appropriate column and rows * @return a {@link MTableModel} with the appropriate column and rows
* @throws SQLException * @throws SQLException
*/ */
@@ -84,10 +84,11 @@ public class DBBlacklist extends AbstractController {
try { try {
if (isAlreadyBlacklisted(word, serverId)) { if (isAlreadyBlacklisted(word, serverId)) {
ResultSet rs = database.prepareAndExecute("SELECT * FROM blacklist WHERE word = ? AND serverId = ?", word, serverId); ResultSet rs = database.prepareAndExecute("SELECT * FROM blacklist WHERE word = ? AND serverId = ?", word, serverId);
Object array[][] = database.convertToArray(rs); MTableModel table = database.convertToTableModel(rs);
if (database.getNbRow() != 0) {
blacklisted = new MBlacklist((String) array[1][0], (Boolean) array[1][2], (String) array[1][3], if (table.getNbRows() != 0) {
(String) array[1][1]); blacklisted = new MBlacklist((String) table.getRow(0).get(0), (Boolean) table.getRow(0).get(2), (String) table.getRow(0).get(3),
(String) table.getRow(0).get(1));
} }
} else { } else {
throw new RuntimeException("Is not blacklisted"); throw new RuntimeException("Is not blacklisted");
@@ -13,13 +13,17 @@ public class ConsoleHandler {
MusicBot.LOGGER.info("Console input ready!"); MusicBot.LOGGER.info("Console input ready!");
} }
public String init() throws IOException { public String init() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input; String input = "";
do { do {
System.out.print(">"); System.out.print(">");
try {
input = br.readLine(); input = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
} while (!input.equalsIgnoreCase("quit")); } while (!input.equalsIgnoreCase("quit"));
return input; return input;