diff --git a/src/de/anura/core/API/Errors.java b/src/de/anura/core/API/Errors.java index 2b33191..c5355c2 100644 --- a/src/de/anura/core/API/Errors.java +++ b/src/de/anura/core/API/Errors.java @@ -17,7 +17,7 @@ public class Errors { String message = exc.getLocalizedMessage(); String name = exc.getClass().toString(); String server = Core.getMainClass() == null ? "~not available~" : Core.getMainClass().getConfig().getString("server-name"); - String errorSql = "INSERT INTO coreErrors (server, exception, msg, info, timestamp) VALUES('" + server + "', '" + name + "', '" + message + "', '" + info + "', '" + System.currentTimeMillis() / 1000 + "')"; + String errorSql = "INSERT INTO coreErrors (server, exception, msg, info, timestamp) VALUES('" + server + "', '" + name + "', '" + Core.getMySql().escapeString(message) + "', '" + info + "', '" + System.currentTimeMillis() / 1000 + "')"; Core.getMySql().queryUpdate(errorSql); ResultSet rs = Core.getMySql().querySelect("SELECT id FROM coreErrors ORDER BY id DESC LIMIT 1"); rs.first(); diff --git a/src/de/anura/core/API/MySQL.java b/src/de/anura/core/API/MySQL.java index b119e37..ece4644 100644 --- a/src/de/anura/core/API/MySQL.java +++ b/src/de/anura/core/API/MySQL.java @@ -40,8 +40,7 @@ public class MySQL { cfg.options().copyDefaults(true); try { cfg.save(file); - } catch(Throwable e) { - Errors.reportException(e); + } catch (IOException e) { } this.host = cfg.getString(dbLoc + "host"); this.port = cfg.getInt(dbLoc + "port"); @@ -57,7 +56,7 @@ public class MySQL { } public String escapeString(String text) { - return text; + return text.replace("'", "\\'"); } private Boolean openConnection() { @@ -66,10 +65,9 @@ public class MySQL { Connection connLoc = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.db, this.username, this.pw); this.conn = connLoc; return true; - } catch(Throwable e) { - Errors.reportException(e); + } catch (ClassNotFoundException | SQLException e) { + return false; } - return false; } private boolean hasConnection() { @@ -81,10 +79,9 @@ public class MySQL { validConn = false; } return validConn; - } catch(Throwable e) { - Errors.reportException(e); + } catch (SQLException e) { + return false; } - return false; } private Boolean reconnect() { @@ -131,8 +128,8 @@ public class MySQL { try { st = connLoc.prepareStatement(query); st.executeUpdate(); - } catch(Throwable e) { - Errors.reportException(e); + } catch (SQLException e) { + System.err.println("Failed to send Update '" + query + "'! (" + e.getLocalizedMessage() + ")"); } this.closeRessources(null, st); } @@ -150,8 +147,8 @@ public class MySQL { } else { return returns; } - } catch(Throwable e) { - Errors.reportException(e); + } catch (SQLException e) { + System.err.println("Unknown error whilst trying to build Prepared Statement!"); queryRedo(query, "select"); } return null; @@ -164,23 +161,28 @@ public class MySQL { ResultSet rs; try { rs = st.executeQuery(); - } catch(Throwable e) { - Errors.reportException(e); + } catch (SQLException e) { + System.err.println("Failed to send 'SELECT'-Query!(" + st.toString() + ") Will try to reconnect to database just in case... you know..."); + System.err.println("Caused by: " + e.getMessage()); return null; } return rs; } private void closeRessources(ResultSet rs, PreparedStatement st) { - try { - if (rs != null) { + if (rs != null) { + try { rs.close(); + } catch (SQLException e) { + } - if (st != null) { + } + if (st != null) { + try { st.close(); - } - } catch(Throwable e) { - Errors.reportException(e); + } catch (SQLException e) { + + } } } @@ -189,8 +191,7 @@ public class MySQL { if (this.conn != null) { this.conn.close(); } - } catch (Throwable e) { - Errors.reportException(e); + } catch (SQLException e) { } finally { this.conn = null; }