get Tables from Oracle user_tables - Java java.sql

Java examples for java.sql:Oracle

Description

get Tables from Oracle user_tables

Demo Code


//package com.java2s;
import java.sql.*;
import java.util.ArrayList;

import java.util.List;

public class Main {

    public static List<String> getTables(Connection conn) {
        ResultSet rs = null;/*from ww  w .  j a v  a 2  s  . co m*/
        PreparedStatement pstmt = null;
        List<String> result = new ArrayList<String>();
        try {
            if (getDatabaseMetaData(conn).getDatabaseProductName()
                    .equalsIgnoreCase("oracle")) {
                pstmt = conn
                        .prepareStatement("select table_name from user_tables");
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    result.add(rs.getString("TABLE_NAME"));
                }
                return result;
            }

            rs = getDatabaseMetaData(conn).getTables(null, null, null,
                    new String[] { "TABLE" });
            while (rs.next()) {
                result.add(rs.getString("TABLE_NAME"));
            }
        } catch (Exception e) {
            throw new RuntimeException("?");
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException("?");
            }
        }
        return result;
    }

    private static DatabaseMetaData getDatabaseMetaData(Connection conn)
            throws SQLException {
        return conn.getMetaData();
    }
}

Related Tutorials