catch (InstantiationException exception) {
throw new MDatabaseConnectionFailureException("Could not load driver.", exception);
}
+ // Initialize the connection.
+ this.initialize();
+ }
+
+ public void initialize() throws MDatabaseConnectionFailureException {
// Establish a connection.
try {
this.connection = DriverManager.getConnection(this.getUrl(), this.getUsername(), this.getPassword());
/* Statements. */
- public MSqlStatementResults executePreparedStatement(String statement) throws MSqlStatementException {
+ public MSqlStatementResults executePreparedStatement(String statement) throws MDatabaseConnectionFailureException, MSqlStatementException {
return this.executePreparedStatement(statement, new LinkedList<Object>());
}
- public MSqlStatementResults executePreparedStatement(String statement, LinkedList<Object> parameters) throws MSqlStatementException {
+ public MSqlStatementResults executePreparedStatement(String statement, LinkedList<Object> parameters) throws MDatabaseConnectionFailureException, MSqlStatementException {
if (MText.isBlank(statement)) {
throw new IllegalArgumentException("Invalid 'statement': null or empty.");
}
MSqlStatementResults results = null;
PreparedStatement preparedStatement = null;
try {
+ // Check the connection.
+ try {
+ preparedStatement = this.getConnection().prepareStatement("/* ping */ SELECT 1", PreparedStatement.RETURN_GENERATED_KEYS);
+ }
+ catch (SQLException exception) {
+ this.initialize();
+ }
// Prepare the statement.
preparedStatement = this.getConnection().prepareStatement(statement, PreparedStatement.RETURN_GENERATED_KEYS);
for (int p = 0; parameters.size() > p; p++) {
/* Engine version. */
- public String getEngineVersion() throws MSqlStatementException {
+ public String getEngineVersion() throws MDatabaseConnectionFailureException, MSqlStatementException {
MSqlStatementResults results = this.executePreparedStatement("SELECT VERSION()");
LinkedList<LinkedHashMap<String, Object>> resultList = results.getRecords();
//
/* Statements. */
- protected MSqlStatementResults insertRecord(LinkedHashMap<String, Object> map) throws MSqlStatementException {
+ protected MSqlStatementResults insertRecord(LinkedHashMap<String, Object> map) throws MDatabaseConnectionFailureException, MSqlStatementException {
if (null == map) {
throw new IllegalArgumentException("Invalid 'map': null.");
}
p);
}
- protected MSqlStatementResults updateRecord(LinkedHashMap<String, Object> map, Object id) throws MSqlStatementException {
+ protected MSqlStatementResults updateRecord(LinkedHashMap<String, Object> map, Object id) throws MDatabaseConnectionFailureException, MSqlStatementException {
if (null == map) {
throw new IllegalArgumentException("Invalid 'map': null.");
}
p);
}
- public MSqlStatementResults setRecord(LinkedHashMap<String, Object> map, Object id) throws MSqlStatementException {
+ public MSqlStatementResults setRecord(LinkedHashMap<String, Object> map, Object id) throws MDatabaseConnectionFailureException, MSqlStatementException {
if (null == id) {
return this.insertRecord(map);
}
}
}
- public MSqlStatementResults getRecord(Object id) throws MSqlStatementException {
+ public MSqlStatementResults getRecord(Object id) throws MDatabaseConnectionFailureException, MSqlStatementException {
if (null == id) {
throw new IllegalArgumentException("Invalid 'id': null.");
}
p);
}
- public MSqlStatementResults deleteRecord(Object id) throws MSqlStatementException {
+ public MSqlStatementResults deleteRecord(Object id) throws MDatabaseConnectionFailureException, MSqlStatementException {
if (null == id) {
throw new IllegalArgumentException("Invalid 'id': null.");
}
p);
}
- public MSqlStatementResults deleteRecords(LinkedHashSet idSet) throws MSqlStatementException {
+ public MSqlStatementResults deleteRecords(LinkedHashSet idSet) throws MDatabaseConnectionFailureException, MSqlStatementException {
if (null == idSet) {
throw new IllegalArgumentException("Invalid 'idSet': null.");
}