This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Table name as parameter to PreparedStatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Table name as parameter to PreparedStatement" Watch "Table name as parameter to PreparedStatement" New topic
Author

Table name as parameter to PreparedStatement

Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
Is it possible to pass the table name as a parameter to a prepared statement?
Something on the order of:

It seems reasonable to me, but when I try this with Oracle's thin client I get an SQLException complaining about a non-existing table.
[ December 08, 2003: Message edited by: Jon Strayer ]

Jon
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
As you found out, that usage is not permissable in JDBC. However, there is no real advantage of using a PreparedStatement in this situation, since the database couldn't pre-compile and cache the statement.
Is there a reason you want/need to do this? Just curious to see if there might be a "better" way to do what you are trying to do ...
Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
I need to read a set of records from a file and update the database from them. The fields being updated are the same in all the tables, so I wanted
to the same PreparedStatement for all of them. It seemed like a clean solution.
As it is, I changed to using plain old Statements and just assemble the sql at the same point I was setting the values in the PareparedStatement.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
If you really have your heart set on using PreparedStatements, then one possibility is to create a separate PreparedStatement for each table, storing them in a HashMap with the table name as a key. Then as you read the records from a file, you pull use the table name to select the appropriate PreparedStatement.
The PreparedStatements could be pre-configured (if you knew exactly which tables you'd be processing), or you could create them as needed.
When you finish processing all records, simply iterate through the elements of the HashMap and close each of the PreparedStatements.
Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
I thought about that, but it seemd like way too much complexity when I didn't really know if I needed it or not.
I just switched to plain old Statements. So far it seems to be fast enough.
 
 
subject: Table name as parameter to PreparedStatement
 
Similar Threads
escaping quotes, single quotes in a string
How to use Bind Variable with FROM and UPDATE clauses in PreparedStatement???
SQL formatting question
Stopping embedded sql
Solved : How delete oldest records