Results 1 to 7 of 7
Thread: Need help PrepareStatement
- 09-18-2008, 11:31 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 41
- Rep Power
- 0
Need help PrepareStatement
i already read somebook, search on google, and almost the code i get are:
String str = " UPDATE Demo_table SET ? = ? WHERE Name like ? ";
PrepareStatement preStm = cn.prepareStatement(str);
preStm.setString (1,"Mark");
preStm.setDouble(2,100);
preStm.setString(3,"DBSJ");
When i exactly type that code and run, it's appear 1 record updated.
But when i use query (Select * from Demo_table) in SQLServer 2005 , the record which name is "DBSJ" not updated.
Same trouble with Delete.
Only Insert work good.
please help !!! :(
- 09-18-2008, 11:48 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
You can only use the placeholders for values, not for tables, columns, schemas, or anything else of that sort. So the
is wrong. That first ? won't work the way you might think it would. What it does is query winds up looking like thisJava Code:Set ? = ?
where you actually wantedJava Code:"UPDATE Demo_table SET 'Mark' = 100 WHERE Name like 'DBSJ'"
Notice the ticks around Mark in the first example? That is where it goes wrong, and you can't change that behaviour. (Although I believe you have "Mark" and "DBSJ" reveresed there.)Java Code:"UPDATE Demo_table SET Mark = 100 WHERE Name like 'DBSJ'"
- 09-18-2008, 11:57 AM #3
Member
- Join Date
- Aug 2008
- Posts
- 41
- Rep Power
- 0
oh man!!it's exactly what you say.
so...there is no way to use the ? for table, colum name?? sad....Last edited by hungleon88; 09-18-2008 at 12:03 PM.
- 09-18-2008, 12:35 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
As I said, no.
Edit: Those are things that, if they absolutely must be dynamic, have to "cobbled" in i.e.
Java Code:String a = "Mark"; String sql = "Set " + a + " = ?";
Last edited by masijade; 09-18-2008 at 12:37 PM.
- 09-18-2008, 02:52 PM #5
Member
- Join Date
- Aug 2008
- Posts
- 41
- Rep Power
- 0
Thanks! But the code
String a = "Mark";
String sql = "Set " + a + " = ?";
not work.
error : Incorrect syntax near '='
T_T . Maybe when translate to SQL , it's doesn't work.
- 09-18-2008, 03:17 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Show your entire SQL statement code.
- 09-21-2008, 09:34 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 16
- Rep Power
- 0
I think it should like this:
I don't know whether the "Mark" is a column, if it is , then it should work.String str = " UPDATE Demo_table SET Mark = ? WHERE Name like ? ";
PrepareStatement preStm = cn.prepareStatement(str);
preStm.setDouble(1,100);
preStm.setString(2,"DBSJ");
Similar Threads
-
problems with prepareStatement
By Freddie in forum JDBCReplies: 0Last Post: 05-31-2007, 05:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks