I want to run a query on an Oracle database and for each column in the result set, I want to know the schema that the column came from. I tried the following:
ResultSetMetaData rsMetadata = rs.getMetaData();
String schemaName = rsMetadata.getSchemaName(1)
However, this returns an empty string. Is there any work around to get the schema name?
Edit in response to OMG Ponies:
The tool we are developing takes data from a database and analyzes the data to find the most informative subset for a given question. We then create a query which returns only the rows that are informative for the given question. For instance if we had a customer database and wanted to know which customers are most likely to discontinue their service, our tool can create a query which returns 5% of the customer records which can then be run through high powered analytics algorithms. The benefit is that we run our analysis on only a subset of the data which of course will save time. It turns out as well that the high powered analytics algorithms work better now because the first step was essentially filtering the noise out of our data.
So in response to OMG Ponies, the user specifies the database connection information and query as inputs to our tool. Because they can specify any query they like, it would be possible for a user to connect to connect to schema foo, and then run the following query:
SELECT* FROM bar.customer;
If for some reason eye color and gender were predictors of people discontinuing their service, the resulting query that our system generates might look like this:
SELECT * FROM bar.customer WHERE bar.customer.eye_color='blue'
AND bar.customer.gender='M'
It would be nice to know the schema for each column in the results set so we can make sure our query will run correctly. We could assume the schema is the same as the schema used in the database connection and that should be fine 99% of the time. I’m just concerned for that 1% of the time a user might do something unexpected like run a query against another schema.
According to an old Oracle code sample:
That implies to me that
ResultSetMetaDatawill not have those methods for an Oracle either, at least when using an Oracle driver. (I tried with the OCI driver to see if that made a difference, but apparently not).There’s a WebLogic 8 document that suggests it could be done, but that type 4 driver has been deprecated in later releases. So it’s possible you may still be able to find a third-party driver that supports
getSchemaName()against Oracle, but it seems unlikely.