Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Table name as parameter to PreparedStatement

 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic