Java SQL Type isTableMissing(Connection con)

Here you can find the source of isTableMissing(Connection con)

Description

is Table Missing

License

Open Source License

Declaration

public static boolean isTableMissing(Connection con)
            throws SQLException, IOException 

Method Source Code

//package com.java2s;
/*/* ww  w  . j  av  a 2  s .  c  om*/
 * Artifactory is a binaries repository manager.
 * Copyright (C) 2013 JFrog Ltd.
 *
 * Artifactory is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Artifactory 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Artifactory.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;

import java.sql.SQLException;

public class Main {
    /**
     * A list of all the tables in the database
     */
    public static String[] tables = new String[] { "db_properties",
            "artifactory_servers", "stats_remote", "stats", "watches",
            "node_props", "node_meta_infos", "nodes",
            "indexed_archives_entries", "archive_names", "archive_paths",
            "indexed_archives", "binary_blobs", "binaries", "aces", "acls",
            "users_groups", "groups", "user_props", "users",
            "permission_target_repos", "permission_targets", "configs",
            "tasks", "module_props", "build_props", "build_jsons",
            "build_promotions", "build_dependencies", "build_artifacts",
            "build_modules", "builds", "unique_ids" };

    public static boolean isTableMissing(Connection con)
            throws SQLException, IOException {
        for (String table : tables) {
            if (!tableExists(table, con)) {
                return true;
            }
        }
        return false;
    }

    private static boolean tableExists(String tableName, Connection con)
            throws SQLException {
        DatabaseMetaData metaData = con.getMetaData();
        if (metaData.storesLowerCaseIdentifiers()) {
            tableName = tableName.toLowerCase();
        } else if (metaData.storesUpperCaseIdentifiers()) {
            tableName = tableName.toUpperCase();
        }
        try (ResultSet rs = metaData.getTables(null, null, tableName,
                new String[] { "TABLE" })) {
            boolean tableExists = rs.next();
            return tableExists;
        }
    }
}

Related

  1. isStringType(int sqlType)
  2. isStringType(int sqlType)
  3. isStringType(int type)
  4. isStringType(int type)
  5. isSupportedDataType(int type, String typeName)
  6. isTextType(int columnType)
  7. isValidReaderOrStream(final Object o)
  8. isXMLType(int type)
  9. jdbcType2javaType(int jdbcType)