3

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).

This happens on Oracle 11g Express and using the latest Oracle JDBC driver (11.2.3).

Does this happen for other SQL servers too?

Any help/ideas to solve this issue are welcome.

4

2 回答 2

9

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!

于 2013-08-31T07:38:19.327 回答
1

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));
于 2013-08-31T07:33:13.903 回答