* No instances can be created of this class
*/
protected BaseFrameworkSystem () {
- // Init properties instance
- this.properties = new Properties();
+ // Init properties instance with from system
+ this.properties = new Properties(System.getProperties());
// Init properties file
this.initProperties();
package org.mxchange.addressbook.database.backend;
import java.io.IOException;
+import java.sql.SQLException;
import java.util.Iterator;
import org.mxchange.addressbook.FrameworkInterface;
import org.mxchange.addressbook.contact.Contact;
/**
* Tries a connection to the database
+ *
+ * @throws java.sql.SQLException If the connection attempt fails
*/
- public void connectToDatabase ();
+ public void connectToDatabase () throws SQLException;
/**
* Shuts down this backend
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Base64;
* This database backend does not need to connect
*/
@Override
- public void connectToDatabase () {
+ public void connectToDatabase () throws SQLException {
// Empty body
}
package org.mxchange.addressbook.database.backend.mysql;
import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
import java.util.Iterator;
import org.mxchange.addressbook.contact.Contact;
import org.mxchange.addressbook.database.backend.BaseDatabaseBackend;
* @author Roland Haeder
*/
public class MySqlDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend {
+ /**
+ * An instance of a datbase connection
+ */
+ private Connection connection;
+
/**
* Constructor with table name
*
}
@Override
- public void connectToDatabase () {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ public void connectToDatabase () throws SQLException {
+ // Generate connection string
+ String connect = "jdbc:mysql://" + this.getProperty("org.mxchange.database.mysql.host") + "/" + this.getTableName();
+
+ // Now get a connection instance back
+ this.connection = DriverManager.getConnection(connect, this.getProperty("org.mxchange.database.mysql.login"), this.getProperty("org.mxchange.database.mysql.password"));
}
@Override
*/
package org.mxchange.addressbook.database.frontend;
+import java.sql.SQLException;
import org.mxchange.addressbook.BaseFrameworkSystem;
import org.mxchange.addressbook.database.backend.DatabaseBackend;
import org.mxchange.addressbook.database.backend.csv.Base64CsvDatabaseBackend;
* Initialize backend
*
* @throws org.mxchange.addressbook.exceptions.UnsupportedDatabaseBackendException If the backend is not supported
+ * @throws java.sql.SQLException If a SQL database backend fails to connect
*/
- protected void initBackend () throws UnsupportedDatabaseBackendException {
+ protected void initBackend () throws UnsupportedDatabaseBackendException, SQLException {
// Trace message
this.getLogger().trace("CALLED!"); //NOI18N
package org.mxchange.addressbook.database.frontend.contact;
import java.io.IOException;
+import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;
} catch (final UnsupportedDatabaseBackendException ex) {
// Abort program
this.abortProgramWithException(ex);
+ } catch (final SQLException ex) {
+ // Abort here
+ this.abortProgramWithException(ex);
}
}
}
// Read all entries
- while (iterator.hasNext()) {
+ while ((iterator instanceof Iterator) && (iterator.hasNext())) {
// Get next entry
Contact contact = iterator.next();