Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dynamic update statement mysql

 
ahmed basyouny
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I use dynamic update prepared statement in java

something like


Can I do that
I'm using my SQ L
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Something like that, anyway. If you're just asking whether a PreparedStatement can be used to do an UPDATE operation, then the answer is yes. Or were you asking something else?
 
ahmed basyouny
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no i know what Prepared Statement can do i ask if i can handle it that way cause every time it give me syntax error
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual syntax for update statement generally is:

update table_name set column_name = some_value where <some condition>

I suspect that your problem stems from not complying with this syntax. The best strategy in this case is to run the SQL query in an SQL client (TOAD, for example, or whatever client comes with MySQL). Once you have a syntactically correct SQL query, you'll put it into a Java source and replace the literals with question marks.

(Also in JDBC, SQL queries are usually not terminated by a semicolon.)
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahmed basyouny wrote:no i know what Prepared Statement can do i ask if i can handle it that way cause every time it give me syntax error


Well, if you are asking about some specific code then don't post "something like that". Post the actual code you are asking about, that's much easier for everybody.
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me also point out that you can't just replace any part of the SQL with a question mark and expect the PreparedStatement to just replace the question mark by whatever string you specify. The text of the PreparedStatement has to be something recognizable to the database software, so for example you can't use "Select * from ? where password is NULL" and then provide the table name at run time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic