Fix some rare cases

This commit is contained in:
kaenganxt
2014-11-22 23:26:57 +01:00
parent eca5ef967f
commit c1da960bdd
2 changed files with 25 additions and 24 deletions

View File

@@ -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();

View File

@@ -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;
}