]> git.mxchange.org Git - jaddressbook-lib.git/blobdiff - Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java
Handled / throw some exceptions
[jaddressbook-lib.git] / Addressbook / src / org / mxchange / addressbook / database / backend / mysql / MySqlDatabaseBackend.java
index 6a7b0094f60a3672f34b3a140ed6b9e767b2161c..d95afb3e68c007ff837d791d2f2c5c8a643290c5 100644 (file)
@@ -19,6 +19,8 @@ package org.mxchange.addressbook.database.backend.mysql;
 import java.io.IOException;
 import java.sql.Connection;
 import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.text.MessageFormat;
 import java.util.Iterator;
@@ -38,7 +40,12 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
        /**
         * An instance of a datbase connection
         */
-       private Connection connection;
+       private static Connection connection;
+
+       /**
+        * Prepared statement for full row count
+        */
+       private PreparedStatement totalRowCount;
 
        /**
         * Constructor with table name
@@ -57,34 +64,125 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
        @Override
        public void connectToDatabase () throws SQLException {
                // Trace message
-               this.getLogger().trace("CALLED!");
+               this.getLogger().trace("CALLED!"); //NOI18N
+
+               // Is the connection already there?
+               if (MySqlDatabaseBackend.connection instanceof Connection) {
+                       // Already connected
+                       this.getLogger().debug("Connection is already established."); //NOI18N
+
+                       // No need to connect
+                       return;
+               }
 
                // Generate connection string
-               String connect = String.format("jdbc:mysql://%s/%s", this.getProperty("org.mxchange.addressbook.database.mysql.host"), this.getProperty("org.mxchange.addressbook.database.mysql.dbname"));
+               String connect = String.format("jdbc:mysql://%s/%s", //NOI18N
+                               this.getProperty("database.mysql.host"), //NOI18N
+                               this.getProperty("database.mysql.dbname") //NOI18N
+               );
 
                // Debug message
-               this.getLogger().debug(MessageFormat.format("Attempting to connect to {0} ...", connect));
+               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"), this.getProperty("org.mxchange.addressbook.database.mysql.password"));
+               MySqlDatabaseBackend.connection = DriverManager.getConnection(
+                               connect,
+                               this.getProperty("database.mysql.login"), //NOI18N
+                               this.getProperty("database.mysql.password") //NOI18N
+               );
+
+               // Is the connection really up?
+               if (MySqlDatabaseBackend.connection.isClosed()) {
+                       // Connection is closed again
+                       throw new SQLException("Connection is closed."); //NOI18N
+               }
+
+               // Debug message
+               this.getLogger().debug("Connection is up, preparing some statements ..."); //NOI18N
+
+               // Here, the connection is established, so prepare some statements
+               this.totalRowCount = MySqlDatabaseBackend.connection.prepareStatement(String.format("SELECT COUNT(`id`) AS `cnt` FROM `%s` LIMIT 1", this.getTableName())); //NOI18N
 
                // Trace message
-               this.getLogger().trace("EXIT!");
+               this.getLogger().trace("EXIT!"); //NOI18N
        }
 
        @Override
+       @Deprecated
        public Iterator<Contact> contactIterator () throws BadTokenException {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
 
        @Override
        public void doShutdown () {
+               // This should not happen:
+               assert(MySqlDatabaseBackend.connection instanceof Connection) : MessageFormat.format("connection is not valid: {0}", MySqlDatabaseBackend.connection); //NOI18N
+
+               try {
+                       // Close down database connection
+                       MySqlDatabaseBackend.connection.close();
+               } catch (final SQLException ex) {
+                       // Something happened during close()
+                       this.abortProgramWithException(ex);
+               }
+       }
+
+       @Override
+       public int getTotalCount () throws SQLException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("tableName={0} - CALLED!", this.getTableName())); //NOI18N
+
+               // Nothing counted by default
+               int count = 0;
+
+               // Prepared statements are cool ...
+               if (this.totalRowCount.execute()) {
+                       // Did fully work, so get result set
+                       ResultSet set = this.totalRowCount.getResultSet();
+
+                       // First rewind it
+                       assert(set.last()) : ": last() failed"; //NOI18N
+
+                       // Get integer from 'cnt' alias (see statement)
+                       count = set.getInt("cnt"); //NOI18N
+
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("count={0}", count)); //NOI18N
+
+                       // Go back to beginning
+                       set.beforeFirst();
+               } else {
+                       // Empty result
+                       this.getLogger().warn(MessageFormat.format("COUNT() query didn't return any result on table {0}.", this.getTableName())); //NOI18N
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("count={0} - EXIT!", count)); //NOI18N
+
+               // Return result
+               return count;
+       }
+
+       /**
+        * Checks whether at least one row is found with given boolean value.
+        *
+        * @param columnName Column to check for boolean value
+        * @param bool Boolean value to check
+        * @return Whether boolean value is found and returns at least one row
+        */
+       @Override
+       public boolean isRowFound (final String columnName, final boolean bool) throws SQLException {
+               // Is at least one entry found?
+               if (this.getTotalCount() == 0) {
+                       // No entry found at all
+                       return false;
+               }
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
 
        @Override
        public long length () {
-               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+               throw new UnsupportedOperationException("Not implemented for this backend."); //NOI18N
        }
 
        @Override