]> git.mxchange.org Git - jaddressbook-lib.git/commitdiff
Added more MySQL-related stuff
authorRoland Haeder <roland@mxchange.org>
Wed, 29 Jul 2015 12:41:56 +0000 (14:41 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 29 Jul 2015 12:41:56 +0000 (14:41 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java
Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java
Addressbook/src/org/mxchange/addressbook/database/backend/csv/Base64CsvDatabaseBackend.java
Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java
Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java
Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java

index 6315877d1f81f9d9387e012b0451777e77637b3f..f5e8f053d8744401fc6647c3522d853c41be7c3d 100644 (file)
@@ -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();
index 405c53aaa58a2eb07da802ed2f20433f89492dd6..a38c45b57c4235978d61fbf61320fca2a0e8404f 100644 (file)
@@ -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
index 20f8d1aa820ecd22f9d46f4ca0e81b9d4d1f541f..44238610cd0fb1e9bef32ff58e35afa54ba167b9 100644 (file)
@@ -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
        }
 
index 495c74bf89d6d90ea3d8b629c92a3c2426ade1da..f2fea93ce7fd64825747c11bb3d03190c69f37b0 100644 (file)
@@ -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
index 63fd4287a938a12a2335e15088292fc5f6b31b8b..fafdb521151e9a1522360bc18a1bdac0d1274787 100644 (file)
@@ -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
 
index 85dcf153689a00b5c78318bca34fe6504e1793c8..d02d73974ec90206f0747e5dc6235715d6a68dfa 100644 (file)
@@ -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();