check Table Exists - Java JDBC

Java examples for JDBC:Table

Description

check Table Exists

Demo Code


//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 checkTableExists(Connection conn, String tableName) {
        try {/*from w ww .j  av  a 2 s  .co m*/
            DatabaseMetaData meta = conn.getMetaData();
            ResultSet rset = meta.getTables(null, null, null,
                    new String[] { "TABLE" });
            while (rset.next()) {
                if (rset.getString("TABLE_NAME")
                        .equalsIgnoreCase(tableName)) {
                    return true;
                }
            }
        } catch (SQLException ex) {
            throw new RuntimeException(ex.getMessage(), ex);
        }
        return false;
    }
}

Related Tutorials