Java JDBC Database Metadata listAllTables(Connection connection)

Here you can find the source of listAllTables(Connection connection)

Description

list All Tables

License

Open Source License

Declaration

public static ArrayList<String> listAllTables(Connection connection) throws SQLException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Robert "Unlogic" Olofsson (unlogic@unlogic.se).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 ******************************************************************************/

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

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Main {
    public static ArrayList<String> listAllTables(Connection connection) throws SQLException {

        ResultSet rs = null;/*from w w  w  .ja  va 2s  .c om*/

        try {
            DatabaseMetaData meta = connection.getMetaData();

            rs = meta.getTables(null, null, null, null);

            ArrayList<String> tableList = new ArrayList<String>();

            while (rs.next()) {

                tableList.add(rs.getString(3));
            }

            return tableList;

        } finally {
            closeResultSet(rs);
            closeConnection(connection);
        }
    }

    public static void closeResultSet(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
        }
    }

    public static void closeConnection(Connection connection) {
        try {
            if (connection != null && !connection.isClosed()) {
                connection.close();
            }
        } catch (SQLException e) {
        }
    }
}

Related

  1. isIdGenerated(DatabaseMetaData metaData)
  2. isMysql(DatabaseMetaData metadata)
  3. isOracleForSid(DatabaseMetaData metadata, String oracleProduct)
  4. isSybase(DatabaseMetaData metadata)
  5. isSybaseDb(DatabaseMetaData metaData)
  6. printExistingTables(Connection conn)
  7. printMetaData(Connection con)
  8. printTableExistence(String name, Connection conn)
  9. sequenceExists(String seqName, Connection conn)