Example usage for java.sql DatabaseMetaData getProcedures

List of usage examples for java.sql DatabaseMetaData getProcedures

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData getProcedures.

Prototype

ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) throws SQLException;

Source Link

Document

Retrieves a description of the stored procedures available in the given catalog.

Usage

From source file:ro.nextreports.designer.dbviewer.DefaultDBViewer.java

public DBInfo getDBInfo(String schemaName, int mask, Connection con) throws NextSqlException {

    String info = "";
    List<String> keywords = new ArrayList<String>();
    List<DBTable> tables = new ArrayList<DBTable>();
    List<DBProcedure> procedures = new ArrayList<DBProcedure>();
    Dialect dialect;/*from   w ww .j a  va2 s. c om*/

    try {
        dialect = DialectUtil.getDialect(con);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new NextSqlException("Could not get Dialect.", ex);
    }

    try {
        DatabaseMetaData dbmd = con.getMetaData();

        if ((mask & DBInfo.INFO) == DBInfo.INFO) {
            StringBuffer sb = new StringBuffer();
            sb.append(I18NSupport.getString("database.product")).append(dbmd.getDatabaseProductName())
                    .append("\r\n");
            sb.append(I18NSupport.getString("database.product.version"))
                    .append(dbmd.getDatabaseProductVersion()).append("\r\n");
            sb.append(I18NSupport.getString("database.driver.name")).append(dbmd.getDriverName())
                    .append("\r\n");
            sb.append(I18NSupport.getString("database.driver.version")).append(dbmd.getDriverVersion())
                    .append("\r\n");
            info = sb.toString();
        }

        if ((mask & DBInfo.SUPPORTED_KEYWORDS) == DBInfo.SUPPORTED_KEYWORDS) {
            StringTokenizer st = new StringTokenizer(dbmd.getSQLKeywords(), ",");
            while (st.hasMoreTokens()) {
                keywords.add(st.nextToken());
            }
        }

        // Get a ResultSet that contains all of the tables in this database
        // We specify a table_type of "TABLE" to prevent seeing system tables,
        // views and so forth
        boolean tableMask = ((mask & DBInfo.TABLES) == DBInfo.TABLES);
        boolean viewMask = ((mask & DBInfo.VIEWS) == DBInfo.VIEWS);
        if (tableMask || viewMask) {
            String[] tableTypes;
            if (tableMask && viewMask) {
                tableTypes = new String[] { "TABLE", "VIEW" };
            } else if (tableMask) {
                tableTypes = new String[] { "TABLE" };
            } else {
                tableTypes = new String[] { "VIEW" };
            }

            String pattern = tableMask ? Globals.getTableNamePattern() : Globals.getViewNamePattern();
            ResultSet allTables = dbmd.getTables(null, schemaName, pattern, tableTypes);
            try {
                while (allTables.next()) {
                    String table_name = allTables.getString("TABLE_NAME");
                    String table_type = allTables.getString("TABLE_TYPE");

                    // discard recycle bin tables
                    String ignoreTablePrefix = dialect.getRecycleBinTablePrefix();
                    if ((table_name == null)
                            || ((ignoreTablePrefix != null) && table_name.startsWith(ignoreTablePrefix))) {
                        continue;
                    }

                    if ((mask & DBInfo.INDEXES) == DBInfo.INDEXES) {
                        ResultSet indexList = null;
                        try {
                            // Get a list of all the indexes for this table
                            indexList = dbmd.getIndexInfo(null, schemaName, table_name, false, false);
                            List<DBIndex> indexes = new ArrayList<DBIndex>();
                            while (indexList.next()) {
                                String index_name = indexList.getString("INDEX_NAME");
                                String column_name = indexList.getString("COLUMN_NAME");
                                if (!index_name.equals("null")) {
                                    DBIndex index = new DBIndex(index_name, column_name);
                                    indexes.add(index);
                                }
                            }
                            DBTable table = new DBTable(schemaName, table_name, table_type, indexes);
                            tables.add(table);

                        } catch (SQLException e) {
                            throw new NextSqlException("SQL Exception: " + e.getMessage(), e);
                        } finally {
                            closeResultSet(indexList);
                        }

                    } else {
                        DBTable table = new DBTable(schemaName, table_name, table_type);
                        tables.add(table);
                    }
                }
            } catch (SQLException e) {
                throw new NextSqlException("SQL Exception: " + e.getMessage(), e);
            } finally {
                closeResultSet(allTables);
            }

        }

        boolean procedureMask = ((mask & DBInfo.PROCEDURES) == DBInfo.PROCEDURES);
        if (procedureMask) {
            String pattern = Globals.getProcedureNamePattern();
            if (pattern == null) {
                pattern = "%";
            }
            ResultSet rs = dbmd.getProcedures(null, schemaName, pattern);
            try {
                while (rs.next()) {
                    String spName = rs.getString("PROCEDURE_NAME");
                    int spType = rs.getInt("PROCEDURE_TYPE");
                    String catalog = rs.getString("PROCEDURE_CAT");
                    //                        System.out.println("Stored Procedure Name: " + spName);
                    //                        if (spType == DatabaseMetaData.procedureReturnsResult) {
                    //                            System.out.println("procedure Returns Result");
                    //                        } else if (spType == DatabaseMetaData.procedureNoResult) {
                    //                            System.out.println("procedure No Result");
                    //                        } else {
                    //                            System.out.println("procedure Result unknown");
                    //                        }
                    procedures.add(new DBProcedure(schemaName, catalog, spName, spType));
                }
            } catch (SQLException e) {
                throw new NextSqlException("SQL Exception: " + e.getMessage(), e);
            } finally {
                closeResultSet(rs);
            }
        }

    } catch (SQLException e) {
        LOG.error(e.getMessage(), e);
        e.printStackTrace();
        throw new NextSqlException("SQL Exception: " + e.getMessage(), e);
    }

    return new DBInfo(info, tables, procedures, keywords);
}