Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prepared Statement problem

 
jim gemlo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use a prepared statment with variables in the sql string and cant get it to work. I can get it to work if I do it like this

This will remove the value from the selected row as required. But when I try to pass two values, a string and a variable it doesnt work. I do not get any error from the code either.


Any Ideas?

[ September 27, 2004: Message edited by: jim gemlo ]
[ September 27, 2004: Message edited by: jim gemlo ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd guess you can't dynamically set the column name that way. The point of the prepared statement is that the database can take time to optimize the statement and use the optimized version over and over. If you go changing column names it can't be sure the optimization is still good.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the JDBC forum...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan is correct. You can only substitute values in a prepared statements. Not field names or table names.
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of adding column name dynamically in prepared statement add it in your sql query. Then make prepared statememen and use like

StringBuffer sbSqlUpdateServerFact = new StringBuffer("UPDATE dbo.SERVER_FACT SET bkup_key = null WHERE ").append(" srvr_fact_key or your variable to hold column name ").append("= ?");

PreparedStatement stmtUpdateSrvrFact = conn.prepareStatement(sbSqlUpdateServerFact.toString());

stmtUpdateSrvrFact.setString(1, srvr_fact_key);//variable used to hold vaule

desired valuestmtUpdateSrvrFact.executeUpdate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic