Table name as paramter to PreparedStatement?

Can a table name be provided as a parameter to a Prepared Statement? We are using Oracle 10 and have data stored in different schemas. The tables in each schema are identical, but depending on the customer querying the database we need to view the data in one schema or another. Follows is the code we are using:

Connection con = session.connection();

PreparedStatement stmt = con.prepareStatement("select * from ?");

stmt.setString(1, customerSchema+".visit");

ResultSet rs = stmt.executeQuery();

and then we get the exception:

ORA-00903: invalid table name

Is what I am trying to do possible without reverting to changing the Prepared Statement call to:

PreparedStatement stmt = con.prepareStatement("select * from " + customerSchema+".visit");

The reason I would avoid the above code, is because we want to use Hibernate and use the mapping file for SQL statement, but the problem I have is SQL/JDBC focused, since Hibernate can't do something that JDBC can't do.

BTW I am dealing with a legacy database, so while it would be nice to correct the database design, there is too much already in place to do so at this time.

[1293 byte] By [ajmasxa] at [2007-11-27 8:35:42]
# 1
No, you can't do this.Why do you think you need to?
dwga at 2007-7-12 20:32:23 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
I suspect you can limit scope via the user that you log in with.And I suspect that with different customers it would be a good idea to limit access this way anyways.
jschella at 2007-7-12 20:32:23 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

I am dealing with a system where each each client is allocated a separate schema (the data is not private to the customer, so no ethics issues here). We now need to create an admin tool that can create reports, grabbing the data from the various schemas. I was hopping to be able to have an admin user that can access all the schemas, without having to list all the login names and passwords somewhere. In doing so I would be able to query each table of a given type in the various tables. So if I have a table called 'MyTable', then we would have:

SELECT * from mySchemaA.myTable

SELECT * from mySchemaB.myTable

etc

While we can argue over what was done in the past over the way the database was set up, the truth its already there and we have to deal with the result.

Currently the two alternative solutions I am looking at are:

- separate JNDI entries, in the application server, that the application needs to know about

- modifying the parameters in code prior to creating the PreparedStatement

ajmasxa at 2007-7-12 20:32:23 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...