private SQLDatabase() throws SQLException { try { String database = Settings.getString(Parameter.DATABASE); String user = Settings.getString(Parameter.USER); String password = Settings.getString(Parameter.PASSWORD); DriverManager.registerDriver(new com.mysql.jdbc.Driver()); System.out.println("connection: " + user + "@" + database); connection = DriverManager.getConnection(database, user, password); } catch (SQLException ex) { System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); throw ex; } }
public void assureConnected() throws SQLException { try { Statement stmt = connection.createStatement(); ResultSet results = stmt.executeQuery("select 1"); results.close(); stmt.close(); } catch (SQLException e) { String state = e.getSQLState(); if (state.equals("08S01") || state.equals("08003")) { // try to reconnect String database = Settings.getString(Parameter.DATABASE); String user = Settings.getString(Parameter.USER); String password = Settings.getString(Parameter.PASSWORD); connection = DriverManager.getConnection(database, user, password); } } }