From: Roland Haeder Date: Wed, 29 Jul 2015 12:41:56 +0000 (+0200) Subject: Added more MySQL-related stuff X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f71bfc8a19feecd8ce7ba4ca3e00f49f9f066d40;p=jaddressbook-share-lib.git Added more MySQL-related stuff Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java index 6315877..f5e8f05 100644 --- a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java +++ b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java @@ -84,8 +84,8 @@ public class BaseFrameworkSystem implements FrameworkInterface { * 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(); diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java index 405c53a..a38c45b 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java @@ -17,6 +17,7 @@ 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; @@ -32,8 +33,10 @@ public interface DatabaseBackend extends FrameworkInterface { /** * 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 diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/Base64CsvDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/Base64CsvDatabaseBackend.java index 20f8d1a..4423861 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/Base64CsvDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/Base64CsvDatabaseBackend.java @@ -20,6 +20,7 @@ import java.io.DataOutput; 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; @@ -83,7 +84,7 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat * This database backend does not need to connect */ @Override - public void connectToDatabase () { + public void connectToDatabase () throws SQLException { // Empty body } diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java index 495c74b..f2fea93 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java @@ -17,6 +17,9 @@ 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; @@ -31,6 +34,11 @@ import org.mxchange.addressbook.exceptions.UnsupportedDatabaseDriverException; * @author Roland Haeder */ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend { + /** + * An instance of a datbase connection + */ + private Connection connection; + /** * Constructor with table name * @@ -45,8 +53,12 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas } @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 diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java index 63fd428..fafdb52 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java @@ -16,6 +16,7 @@ */ 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; @@ -63,8 +64,9 @@ public class BaseDatabaseFrontend extends BaseFrameworkSystem { * 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 diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java index 85dcf15..d02d739 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java @@ -17,6 +17,7 @@ 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; @@ -74,6 +75,9 @@ public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements Con } catch (final UnsupportedDatabaseBackendException ex) { // Abort program this.abortProgramWithException(ex); + } catch (final SQLException ex) { + // Abort here + this.abortProgramWithException(ex); } } @@ -149,7 +153,7 @@ public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements Con } // Read all entries - while (iterator.hasNext()) { + while ((iterator instanceof Iterator) && (iterator.hasNext())) { // Get next entry Contact contact = iterator.next();