]> git.mxchange.org Git - jaddressbook-share-lib.git/commitdiff
First atttempt of prepared statement
authorRoland Haeder <roland@mxchange.org>
Thu, 30 Jul 2015 13:32:41 +0000 (15:32 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 30 Jul 2015 13:32:41 +0000 (15:32 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java
Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java

index 9a66e94f1f14b2006cf2fa7aac3e26c3fd6f503d..70cfd81d856448c29631d7d4e9a3e456a16314d4 100644 (file)
@@ -47,8 +47,9 @@ public interface DatabaseBackend extends FrameworkInterface {
         * Some "getter" for total table row count
         * 
         * @return Total row count
+        * @throws java.sql.SQLException If an SQL error occurs
         */
-       public int getTotalCount ();
+       public int getTotalCount () throws SQLException;
 
        /**
         * Checks whether at least one row is found with given boolean value.
@@ -56,8 +57,9 @@ public interface DatabaseBackend extends FrameworkInterface {
         * @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
+        * @throws java.sql.SQLException If an SQL error occurs
         */
-       public boolean isRowFound (final String columnName, final boolean bool);
+       public boolean isRowFound (final String columnName, final boolean bool) throws SQLException;
 
        /**
         * Rewinds backend
index 8845521e70a841683192e809fc6a2ab327ccad56..8b0ad9f32232fb19ffafb0ba200cb3ced58b9657 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;
@@ -40,6 +42,11 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
         */
        private static Connection connection;
 
+       /**
+        * Prepared statement for full row count
+        */
+       private PreparedStatement totalRowCount;
+
        /**
         * Constructor with table name
         * 
@@ -84,6 +91,18 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
                                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("SELECT COUNT(`id`) AS `cnt` FROM ? LIMIT 1"); //NOI18N
+
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
        }
@@ -97,7 +116,7 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
        @Override
        public void doShutdown () {
                // This should not happen:
-               assert(MySqlDatabaseBackend.connection == null) : "connection is null"; //NOI18N
+               assert(MySqlDatabaseBackend.connection instanceof Connection) : MessageFormat.format("connection is not valid: {0}", MySqlDatabaseBackend.connection); //NOI18N
 
                try {
                        // Close down database connection
@@ -108,8 +127,30 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
        }
 
        @Override
-       public int getTotalCount () {
-               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       public int getTotalCount () throws SQLException{
+               // Nothing counted by default
+               int count = 0;
+
+               // Set table name
+               this.totalRowCount.setString(1, this.getTableName());
+
+               // 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
+
+                       // Go back to beginning
+                       set.beforeFirst();
+               }
+
+               // Return result
+               return count;
        }
 
        /**
@@ -120,7 +161,7 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
         * @return Whether boolean value is found and returns at least one row
         */
        @Override
-       public boolean isRowFound (final String columnName, final boolean bool) {
+       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