From: Roland Haeder Date: Thu, 30 Jul 2015 13:32:41 +0000 (+0200) Subject: First atttempt of prepared statement X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f9fc2ec9a73e932e678cf6532225cd228d7cfa95;p=jaddressbook-lib.git First atttempt of prepared statement Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java index 9a66e94f..70cfd81d 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java @@ -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 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 8845521e..8b0ad9f3 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java @@ -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