Java DataSource getTables(DataSource datasource)

Here you can find the source of getTables(DataSource datasource)

Description

get Tables

License

Apache License

Declaration

public static List<String> getTables(DataSource datasource) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.List;

import java.util.concurrent.CopyOnWriteArrayList;
import javax.sql.DataSource;

public class Main {
    private static List<String> _tnames = null;

    public static List<String> getTables(DataSource datasource) throws SQLException {
        if (_tnames != null) {
            return _tnames;
        }/* w w w  .j  a  va  2s  .  co m*/
        _tnames = new CopyOnWriteArrayList<String>();
        Connection conn = null;
        ResultSet rs = null;
        try {
            conn = datasource.getConnection();
            DatabaseMetaData dmd = conn.getMetaData();
            String[] types = { "TABLE" };
            //         rs = dmd.getTables(conn.getCatalog(), conn.getSchema(), "%", types);
            rs = dmd.getTables(conn.getCatalog(), null, "%", types);

            while (rs.next()) {
                String tname = rs.getString("TABLE_NAME");
                _tnames.add(tname);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.close();
            }
            if (rs != null) {
                rs.close();
            }
        }
        return _tnames;
    }
}

Related

  1. getJdbcUrlFromDataSource(DataSource dataSource)
  2. getJNDIConnectionByContainer(String dataSource)
  3. getProductAllCount(DataSource ds, String key, String sql_allcount)
  4. getSchemaSeparator(DataSource dataSource)
  5. getTableColumnCount(DataSource dataSource, String selectedTable)
  6. hasTable(DataSource ds, String table)
  7. initDataSource()
  8. initDatasource(String jndiName)
  9. initDb(DataSource ds)