is Exists Table - Java java.sql

Java examples for java.sql:Table

Description

is Exists Table

Demo Code

/*/*from w w w . jav a  2  s  .c  om*/
 * Copyright (c) 2008, 2009
 *  Rough Diamond Co., Ltd.              -- http://www.rough-diamond.co.jp/
 *  Information Systems Institute, Ltd.  -- http://www.isken.co.jp/
 *  All rights reserved.
 */
//package com.java2s;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {

    public static boolean isExistsTable(Connection con, String schema,
            String tableName) throws SQLException {
        DatabaseMetaData dmd = con.getMetaData();
        ResultSet rs = dmd.getTables(null, schema, "%", null);
        try {
            tableName = tableName.toUpperCase();
            while (rs.next()) {
                String tableNameTmp = rs.getString("TABLE_NAME");
                if (tableName.equals(tableNameTmp.toUpperCase())) {
                    return true;
                }
            }
            return false;
        } finally {
            rs.close();
        }
    }
}

Related Tutorials