]> git.mxchange.org Git - addressbook-swing.git/blobdiff - Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java
Implemented doShutdown() for MySQL backend
[addressbook-swing.git] / Addressbook / src / org / mxchange / addressbook / database / backend / mysql / MySqlDatabaseBackend.java
index 495c74bf89d6d90ea3d8b629c92a3c2426ade1da..900d5a8948d329f00f867e2fd3f6bcee89080242 100644 (file)
 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.text.MessageFormat;
 import java.util.Iterator;
 import org.mxchange.addressbook.contact.Contact;
 import org.mxchange.addressbook.database.backend.BaseDatabaseBackend;
@@ -31,6 +35,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
         * 
@@ -41,12 +50,33 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
                // Validate driver
                this.validateDriver("mysql"); //NOI18N
 
-               // Now that the driver is there ...
+               // Now that the driver is there, set the table name
+               this.setTableName(tableName);
        }
 
        @Override
-       public void connectToDatabase () {
-               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       public void connectToDatabase () throws SQLException {
+               // Trace message
+               this.getLogger().trace("CALLED!"); //NOI18N
+
+               // Generate connection string
+               String connect = String.format("jdbc:mysql://%s/%s", //NOI18N
+                               this.getProperty("org.mxchange.addressbook.database.mysql.host"), //NOI18N
+                               this.getProperty("org.mxchange.addressbook.database.mysql.dbname") //NOI18N
+               );
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("Attempting to connect to {0} ...", connect)); //NOI18N
+
+               // Now get a connection instance back
+               this.connection = DriverManager.getConnection(
+                               connect,
+                               this.getProperty("org.mxchange.addressbook.database.mysql.login"), //NOI18N
+                               this.getProperty("org.mxchange.addressbook.database.mysql.password") //NOI18N
+               );
+
+               // Trace message
+               this.getLogger().trace("EXIT!"); //NOI18N
        }
 
        @Override
@@ -56,7 +86,15 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
 
        @Override
        public void doShutdown () {
-               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+               // This should not happen:
+               assert(this.connection == null) : "connection is null";
+
+               try {
+                       // Close down database connection
+                       this.connection.close();
+               } catch (final SQLException ex) {
+                       this.abortProgramWithException(ex);
+               }
        }
 
        @Override