Java JDBC Database Metadata getQualifiedTableName(DatabaseMetaData dbmd, String catalog, String schema, String table, boolean useQuotes)

Here you can find the source of getQualifiedTableName(DatabaseMetaData dbmd, String catalog, String schema, String table, boolean useQuotes)

Description

Return a fully qualified table name.

License

Open Source License

Parameter

Parameter Description
dbmd DatabaseMetaData
catalog String
schema String
table String
useQuotes boolean

Exception

Parameter Description
SQLException an exception

Return

The qualified table name.

Declaration

public static String getQualifiedTableName(DatabaseMetaData dbmd,
        String catalog, String schema, String table, boolean useQuotes)
        throws SQLException 

Method Source Code

//package com.java2s;
/*//from ww w. jav a 2s.co m
 * Copyright (c) 2008-2011 Simon Ritchie.
 * All rights reserved. 
 * 
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program.  If not, see http://www.gnu.org/licenses/>.
 */

import java.sql.*;

public class Main {
    /**
     * Return a fully qualified table name. This method examines the DatabaseMetaData to
     * determine whether catalog and schema should be included in the qualified table name.
     * If quotes are required, the appropriate quote characters for the database will be
     * used to support the name. 
     * 
     * @param dbmd DatabaseMetaData
     * @param catalog String
     * @param schema String
     * @param table String
     * @param useQuotes boolean
     * @return The qualified table name.
     * @throws SQLException 
     */
    public static String getQualifiedTableName(DatabaseMetaData dbmd,
            String catalog, String schema, String table, boolean useQuotes)
            throws SQLException {
        boolean catalogSupported = dbmd
                .supportsCatalogsInTableDefinitions();
        boolean schemaSupported = dbmd.supportsSchemasInTableDefinitions();
        String quote = dbmd.getIdentifierQuoteString();
        String separator = dbmd.getCatalogSeparator();
        if (separator == null || separator.trim().length() == 0) {
            separator = ".";
        }

        StringBuilder sb = new StringBuilder();
        if (catalogSupported && catalog != null) {
            if (useQuotes) {
                sb.append(quote);
            }
            sb.append(catalog);
            if (useQuotes) {
                sb.append(quote);
            }
            sb.append(separator);
        }
        if (schemaSupported && schema != null) {
            if (useQuotes) {
                sb.append(quote);
            }
            sb.append(schema);
            if (useQuotes) {
                sb.append(quote);
            }
            sb.append(separator);
        }
        if (useQuotes) {
            sb.append(quote);
        }
        sb.append(table);
        if (useQuotes) {
            sb.append(quote);
        }
        return sb.toString();
    }
}

Related

  1. getOnly1PrimaryKey(Connection conn, String tableName)
  2. getPrimaryKey(DatabaseMetaData metadata, String tableName)
  3. getPrimaryKeyColumn(DatabaseMetaData md, String TableName)
  4. getPrimaryKeyColumnNames(Connection connection, String tableName)
  5. getPrimaryKeys(Connection conn, String tableName)
  6. getSchemaPattern(final DatabaseMetaData dbData, String schemaName)
  7. getSchemas(Connection c)
  8. getSimplifiedURL(final DatabaseMetaData metadata)
  9. getTableColumns(Connection connection, String selectedTable)