DatabaseMetaData.getColumns returning an empty ResultSet for synonyms

后端 未结 2 1921
野趣味
野趣味 2021-01-06 18:17

The method getColumns() on the metadata is returning an empty ResultSet for Synonyms (for tables and views it\'s returning the list of columns properly).

<
相关标签:
2条回答
  • 2021-01-06 18:35

    use this way

    PreparedStatement pt=coneection.preparestatement("select * from table_name");
    Resultset rs=pt.executeQuery();
    ResultsetMetaData rsmd=rs.getMetaData();
    System.out.println(rsmd.getColumnName(coulmn_number));
    System.out.println(rsmd.getColumnType(column_number));
    

    Another way

    PreparedStatement pt=coneection.preparestatement("select * from table_name");
        Resultset rs=pt.executeQuery();
        ResultsetMetaData rsmd=pt.getMetaData();
        System.out.println(rsmd.getColumnName(coulmn_number));
        System.out.println(rsmd.getColumnType(column_number));
    
    0 讨论(0)
  • 2021-01-06 18:37

    By default the Oracle drivers does not return information on synonyms in getColumns(). This is documented in the Oracle 11g JDBC Developer's Guide under Performance Extensions:

    Considerations for getColumns

    By default, the getColumns method does not retrieve information about the columns if a synonym is specified. To enable the retrieval of information if a synonym is specified, you must call the setIncludeSynonyms method on the connection as follows:

    ( (oracle.jdbc.driver.OracleConnection)conn ).setIncludeSynonyms(true)
    

    This will cause all subsequent getColumns method calls on the connection to include synonyms. This is similar to setRemarksReporting. Alternatively, you can set the includeSynonyms connection property. This is similar to the remarksReporting connection property.

    However, bear in mind that if includeSynonyms is true, then the name of the object returned in the table_name column will be the synonym name, if a synonym exists. This is true even if you pass the table name to getColumns.

    Note that this last item is really important to keep in mind!

    0 讨论(0)
提交回复
热议问题