Java SQL Table getRowCount(Statement stmt, String tableName)

Here you can find the source of getRowCount(Statement stmt, String tableName)

Description

Returns the row count of the given table

License

Open Source License

Parameter

Parameter Description
stmt Statement for sql
tableName table to check

Exception

Parameter Description
SQLException SQLException

Return

row count

Declaration

public static int getRowCount(Statement stmt, String tableName)
        throws SQLException 

Method Source Code

//package com.java2s;
/*//from w  w w  . j a v  a 2  s  .  c  om
 * Copyright (C) 2008 Michael Romankiewicz
 * microm at users.sourceforge.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    /**
     * Returns the row count of the given table
     * 
     * @param stmt
     *           Statement for sql
     * @param tableName
     *           table to check
     * @return row count
     * @throws SQLException
     *            SQLException
     */
    public static int getRowCount(Statement stmt, String tableName)
            throws SQLException {
        int rowCount = 0;
        ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM "
                + tableName);
        if (rs.next()) {
            rowCount = rs.getInt(1);
        }
        rs.close();

        return rowCount;
    }
}

Related

  1. getIdNumber(String tableName)
  2. getImmutableDefaults()
  3. getLastCreatedEntry(Connection conn, String tableName)
  4. getMetaDataMap(Statement statement, String tableOrViewName)
  5. getNextId(Connection con, String table, String identityFieldName)
  6. getSensorsList( final Connection jdbcConnection, String tableName)
  7. getShapeText(Connection conn, String tableName)
  8. getTableFields(Connection con, String tableName)
  9. getTableList(Connection connection)