Java SQL Table getTableSize(final Statement statement, final String schema, final String table, boolean scope)

Here you can find the source of getTableSize(final Statement statement, final String schema, final String table, boolean scope)

Description

get Table Size

License

Apache License

Declaration

public static String getTableSize(final Statement statement, final String schema, final String table,
            boolean scope) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static String getTableSize(final Statement statement, final String schema, final String table,
            boolean scope) {
        String size = "";
        try {/*  www  .j  a v a  2s  .co  m*/
            ResultSet rs = statement.executeQuery(String.format(
                    "SELECT bytes FROM %s" + " WHERE segment_name = UPPER('%s') AND segment_type = 'TABLE'"
                            + " %s AND owner = UPPER('%s')",
                    scope ? "user_segments" : "dba_segments", table, scope ? "--" : "", schema));

            while (rs.next()) {
                Double bytes = rs.getDouble(1);
                if (bytes > 1024) {
                    size = bytes / (double) 1024 + "KB";
                } else if (bytes > 104578) {
                    size = bytes / (double) 1048576 + "MB";
                } else if (bytes > 104578 * 1024) {
                    size = bytes / (double) (1048576 * 1024) + "GB";
                }
            }
        } catch (SQLException ex) {
            if (ex.getErrorCode() == 942) {
                throw new RuntimeException(ex.getMessage() + "Internal Error..", ex);
            }
        }
        return size;
    }
}

Related

  1. getTables(Connection connection)
  2. getTables(Connection connection)
  3. getTables(Connection connection)
  4. getTablesFromDatabase(Connection conn)
  5. getTableSize(Connection conn, String tableName)
  6. getTotalRows(String tableName, Statement statement)
  7. hasTable(Connection conn, String schemaName, String tableName)
  8. hasTable(String TableName, Connection conn)
  9. isExistsDerbyTable(Connection derbyConnection, String schema, String table)