⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databasemetadata.java

📁 一个网上书店程序!实现网上购书结算等! 由jsp+javabean+mysql组成! 功能很完善
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

    /**
     * Does a catalog appear at the start of a qualified table name? (Otherwise
     * it appears at the end)
     *
     * @return true if it appears at the start
     *
     * @throws java.sql.SQLException DOCUMENT ME!
     */
    public boolean isCatalogAtStart() throws java.sql.SQLException {
        return true;
    }

    /**
     * What's the separator between catalog and table name?
     *
     * @return the separator string
     *
     * @throws java.sql.SQLException DOCUMENT ME!
     */
    public String getCatalogSeparator() throws java.sql.SQLException {
        return ".";
    }

    /**
     * What's the database vendor's preferred term for "catalog"?
     *
     * @return the vendor term
     *
     * @throws java.sql.SQLException DOCUMENT ME!
     */
    public String getCatalogTerm() throws java.sql.SQLException {
        return "database";
    }

    /**
     * Get the catalog names available in this database.  The results are
     * ordered by catalog name.
     * 
     * <P>
     * The catalog column is:
     * 
     * <OL>
     * <li>
     * <B>TABLE_CAT</B> String => catalog name
     * </li>
     * </ol>
     * </p>
     *
     * @return ResultSet each row has a single String column that is a catalog
     *         name
     *
     * @throws java.sql.SQLException DOCUMENT ME!
     */
    public java.sql.ResultSet getCatalogs() throws java.sql.SQLException {
        java.sql.ResultSet results = null;
        java.sql.Statement stmt = null;

        try {
            stmt = this.conn.createStatement();
            
			if (stmt.getMaxRows() != 0) {
				stmt.setMaxRows(0);
			}
			
            results = stmt.executeQuery("SHOW DATABASES");

            java.sql.ResultSetMetaData resultsMD = results.getMetaData();
            Field[] fields = new Field[1];
            fields[0] = new Field("", "TABLE_CAT", Types.VARCHAR,
                    resultsMD.getColumnDisplaySize(1));

            ArrayList tuples = new ArrayList();

            while (results.next()) {
                byte[][] rowVal = new byte[1][];
                rowVal[0] = results.getBytes(1);
                tuples.add(rowVal);
            }

            return buildResultSet(fields, tuples);
        } finally {
            if (results != null) {
                try {
                    results.close();
                } catch (SQLException sqlEx) {
                    AssertionFailedException.shouldNotHappen(sqlEx);
                }

                results = null;
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException sqlEx) {
                    AssertionFailedException.shouldNotHappen(sqlEx);
                }

                stmt = null;
            }
        }
    }

    /**
     * Get a description of the access rights for a table's columns.
     * 
     * <P>
     * Only privileges matching the column name criteria are returned.  They
     * are ordered by COLUMN_NAME and PRIVILEGE.
     * </p>
     * 
     * <P>
     * Each privilige description has the following columns:
     * 
     * <OL>
     * <li>
     * <B>TABLE_CAT</B> String => table catalog (may be null)
     * </li>
     * <li>
     * <B>TABLE_SCHEM</B> String => table schema (may be null)
     * </li>
     * <li>
     * <B>TABLE_NAME</B> String => table name
     * </li>
     * <li>
     * <B>COLUMN_NAME</B> String => column name
     * </li>
     * <li>
     * <B>GRANTOR</B> => grantor of access (may be null)
     * </li>
     * <li>
     * <B>GRANTEE</B> String => grantee of access
     * </li>
     * <li>
     * <B>PRIVILEGE</B> String => name of access (SELECT, INSERT, UPDATE,
     * REFRENCES, ...)
     * </li>
     * <li>
     * <B>IS_GRANTABLE</B> String => "YES" if grantee is permitted to grant to
     * others; "NO" if not; null if unknown
     * </li>
     * </ol>
     * </p>
     *
     * @param catalog a catalog name; "" retrieves those without a catalog
     * @param schema a schema name; "" retrieves those without a schema
     * @param table a table name
     * @param columnNamePattern a column name pattern
     *
     * @return ResultSet each row is a column privilege description
     *
     * @throws java.sql.SQLException if a database access error occurs
     *
     * @see #getSearchStringEscape
     */
    public java.sql.ResultSet getColumnPrivileges(String catalog,
        String schema, String table, String columnNamePattern)
        throws java.sql.SQLException {
        Field[] fields = new Field[8];
        fields[0] = new Field("", "TABLE_CAT", Types.CHAR, 64);
        fields[1] = new Field("", "TABLE_SCHEM", Types.CHAR, 1);
        fields[2] = new Field("", "TABLE_NAME", Types.CHAR, 64);
        fields[3] = new Field("", "COLUMN_NAME", Types.CHAR, 64);
        fields[4] = new Field("", "GRANTOR", Types.CHAR, 77);
        fields[5] = new Field("", "GRANTEE", Types.CHAR, 77);
        fields[6] = new Field("", "PRIVILEGE", Types.CHAR, 64);
        fields[7] = new Field("", "IS_GRANTABLE", Types.CHAR, 3);

        StringBuffer grantQuery = new StringBuffer(
                "SELECT c.host, c.db, t.grantor, c.user, "
                + "c.table_name, c.column_name, c.column_priv "
                + "from mysql.columns_priv c, mysql.tables_priv t "
                + "where c.host = t.host and c.db = t.db and "
                + "c.table_name = t.table_name ");

        if ((catalog != null) && (catalog.length() != 0)) {
            grantQuery.append(" AND c.db='");
            grantQuery.append(catalog);
            grantQuery.append("' ");
            ;
        }

        grantQuery.append(" AND c.table_name ='");
        grantQuery.append(table);
        grantQuery.append("' AND c.column_name like '");
        grantQuery.append(columnNamePattern);
        grantQuery.append("'");

        Statement stmt = null;
        ResultSet results = null;
        ArrayList grantRows = new ArrayList();

        try {
            stmt = this.conn.createStatement();
            
			if (stmt.getMaxRows() != 0) {
				stmt.setMaxRows(0);
			}
			
            results = stmt.executeQuery(grantQuery.toString());

            while (results.next()) {
                String host = results.getString(1);
                String database = results.getString(2);
                String grantor = results.getString(3);
                String user = results.getString(4);

                if ((user == null) || (user.length() == 0)) {
                    user = "%";
                }

                StringBuffer fullUser = new StringBuffer(user);

                if ((host != null) && this.conn.useHostsInPrivileges()) {
                    fullUser.append("@");
                    fullUser.append(host);
                }

                String columnName = results.getString(6);
                String allPrivileges = results.getString(7);

                if (allPrivileges != null) {
                    allPrivileges = allPrivileges.toUpperCase();

                    StringTokenizer st = new StringTokenizer(allPrivileges, ",");

                    while (st.hasMoreTokens()) {
                        String privilege = st.nextToken().trim();
                        byte[][] tuple = new byte[8][];
                        tuple[0] = s2b(database);
                        tuple[1] = null;
                        tuple[2] = s2b(table);
                        tuple[3] = s2b(columnName);

                        if (grantor != null) {
                            tuple[4] = s2b(grantor);
                        } else {
                            tuple[4] = null;
                        }

                        tuple[5] = s2b(fullUser.toString());
                        tuple[6] = s2b(privilege);
                        tuple[7] = null;
                        grantRows.add(tuple);
                    }
                }
            }
        } finally {
            if (results != null) {
                try {
                    results.close();
                } catch (Exception ex) {
                    ;
                }

                results = null;
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception ex) {
                    ;
                }

                stmt = null;
            }
        }

        return buildResultSet(fields, grantRows);
    }

    /**
     * Get a description of table columns available in a catalog.
     * 
     * <P>
     * Only column descriptions matching the catalog, schema, table and column
     * name criteria are returned.  They are ordered by TABLE_SCHEM,
     * TABLE_NAME and ORDINAL_POSITION.
     * </p>
     * 
     * <P>
     * Each column description has the following columns:
     * 
     * <OL>
     * <li>
     * <B>TABLE_CAT</B> String => table catalog (may be null)
     * </li>
     * <li>
     * <B>TABLE_SCHEM</B> String => table schema (may be null)
     * </li>
     * <li>
     * <B>TABLE_NAME</B> String => table name
     * </li>
     * <li>
     * <B>COLUMN_NAME</B> String => column name
     * </li>
     * <li>
     * <B>DATA_TYPE</B> short => SQL type from java.sql.Types
     * </li>
     * <li>
     * <B>TYPE_NAME</B> String => Data source dependent type name
     * </li>
     * <li>
     * <B>COLUMN_SIZE</B> int => column size.  For char or date types this is
     * the maximum number of characters, for numeric or decimal types this is
     * precision.
     * </li>
     * <li>
     * <B>BUFFER_LENGTH</B> is not used.
     * </li>
     * <li>
     * <B>DECIMAL_DIGITS</B> int => the number of fractional digits
     * </li>
     * <li>
     * <B>NUM_PREC_RADIX</B> int => Radix (typically either 10 or 2)
     * </li>
     * <li>
     * <B>NULLABLE</B> int => is NULL allowed?
     * 
     * <UL>
     * <li>
     * columnNoNulls - might not allow NULL values
     * </li>
     * <li>
     * columnNullable - definitely allows NULL values
     * </li>
     * <li>
     * columnNullableUnknown - nullability unknown
     * </li>
     * </ul>
     * 
     * </li>
     * <li>
     * <B>REMARKS</B> String => comment describing column (may be null)
     * </li>
     * <li>
     * <B>COLUMN_DEF</B> String => default value (may be null)
     * </li>
     * <li>
     * <B>SQL_DATA_TYPE</B> int => unused
     * </li>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -