• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Table name as parameter to PreparedStatement

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic