auto-reconnect

This commit is contained in:
MrBesen 2018-07-16 12:43:56 +02:00
parent b1ed31efe4
commit 9221b03120
1 changed files with 21 additions and 8 deletions

View File

@ -19,13 +19,10 @@ public class DB {
private Logger log = Logger.getLogger(DB.class.getName());
public DB() {
public DB() {
try {
Class.forName("com.mysql.jdbc.Driver");//Treiber laden
//verbinden
con = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/?serverTimezone=UTC" ,user,pw);
connect(false);
//set the database up!
boolean found = false;
ResultSet set = con.getMetaData().getCatalogs();//does the db exists?
@ -46,12 +43,22 @@ public class DB {
log.info("Database is set up! -> \n\nFirst Entry in uploaded needed!!!!!!\nPlease insert MANUALY!\n ");
}
} catch (ClassNotFoundException | SQLException e) {
} catch (SQLException e) {
log.error("Error while connecting to the database! ", e);
}
}
private void connect(boolean selectdb) {
try {
Class.forName("com.mysql.jdbc.Driver");//Treiber laden try this driver: com.mysql.cj.jdbc.Driver
//verbinden
con = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + (selectdb ? db : "") + "?serverTimezone=UTC" ,user,pw);
}catch (ClassNotFoundException | SQLException e) {
log.error("Error while connecting to the database! ", e);
}
}
/**
* removes all videos, that are known from the db
* @param input
@ -100,6 +107,9 @@ public class DB {
*/
public ResultSet query(String q) {
try {
if(con.isClosed()) {
connect(true);
}
return con.prepareStatement(q).executeQuery();
} catch (SQLException e) {
log.error("Fehler bim ausführen der Query: " + q, e);
@ -113,6 +123,9 @@ public class DB {
*/
public void update(String q) {
try {
if(con.isClosed()) {
connect(true);
}
con.prepareStatement(q).executeUpdate();
} catch (SQLException e) {
log.error("Fehler bim ausführen der Update-Query: " + q, e);