Java Utililty Methods SQL Table

List of utility methods to do SQL Table

Description

The list of methods to do SQL Table are organized into topic(s).

Method

ListgetTables(Connection connection)
Retrieves all tables from the database currently connected to and store them in an ArrayList of Strings.
DatabaseMetaData dbmd = connection.getMetaData();
List<String> tables = new ArrayList<String>();
String[] types = { "TABLE" };
ResultSet resultSet = dbmd.getTables(null, null, "%", types);
while (resultSet.next()) {
    String tableName = resultSet.getString("TABLE_NAME");
    tables.add(tableName);
resultSet.close();
return tables;
ListgetTablesFromDatabase(Connection conn)
get Tables From Database
List<String> tables = new ArrayList<>();
String types[] = new String[] { "TABLE", "VIEW" };
DatabaseMetaData md;
try {
    md = conn.getMetaData();
    ResultSet rs = md.getTables(null, null, "%", types);
    while (rs.next()) {
        String tn = rs.getString(3);
...
intgetTableSize(Connection conn, String tableName)

Gets the record size from the database with the given table.

Statement statement = null;
ResultSet rs = null;
try {
    statement = conn.createStatement();
    rs = statement.executeQuery("SELECT COUNT(*) FROM " + tableName);
    rs.next();
    return rs.getInt(1);
} finally {
...
StringgetTableSize(final Statement statement, final String schema, final String table, boolean scope)
get Table Size
String size = "";
try {
    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);
...
longgetTotalRows(String tableName, Statement statement)
get Total Rows
ResultSet resultSet = statement.executeQuery("select count(*) from " + tableName + ";");
long numberOfRows = 0;
if (resultSet.next()) {
    numberOfRows = resultSet.getLong(1);
resultSet.close();
return numberOfRows;
booleanhasTable(Connection conn, String schemaName, String tableName)
has Table
ResultSet rs = null;
try {
    rs = conn.getMetaData().getTables(null, schemaName, tableName.toUpperCase(), null);
    while (rs.next()) {
        if (tableName.equalsIgnoreCase(rs.getString("TABLE_NAME")))
            return true;
} catch (Exception ex) {
...
booleanhasTable(String TableName, Connection conn)
has Table
DatabaseMetaData md = conn.getMetaData();
String[] Types = { "TABLE", "VIEW" };
ResultSet rs = md.getTables(null, null, TableName, null);
boolean ret = rs.next();
rs.close();
if (ret)
    return (ret);
rs = md.getTables(null, null, TableName.toUpperCase(), null);
...
booleanisExistsDerbyTable(Connection derbyConnection, String schema, String table)
is Exists Derby Table
ResultSet rs = derbyConnection.getMetaData().getTables(null, schema, table, null);
boolean isTableExists = rs.next();
rs.close();
return isTableExists;
booleanisFileImportable(File file, String prefix)
Check if the file is well formatted regarding an extension prefix.
if (isExtensionWellFormated(file, prefix)) {
    if (file.exists()) {
        return true;
    } else {
        throw new FileNotFoundException("The following file does not exists:\n" + file.getPath());
} else {
    throw new SQLException("Please use " + prefix + " extension.");
...
booleanisTableEmpty(Connection con, String schemaName, String tableName)
is Table Empty
boolean result = false;
try {
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + schemaName + "." + tableName);
    rs.next();
    int rowCount = rs.getInt(1);
    if (rowCount == 0) {
        result = true;
...