* 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.
* @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
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;
*/
private static Connection connection;
+ /**
+ * Prepared statement for full row count
+ */
+ private PreparedStatement totalRowCount;
+
/**
* Constructor with table name
*
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
}
@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
}
@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;
}
/**
* @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