Results 1 to 7 of 7
  1. #1
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default 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 !!! :(

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    You can only use the placeholders for values, not for tables, columns, schemas, or anything else of that sort. So the
    Java Code:
    Set ? = ?
    is wrong. That first ? won't work the way you might think it would. What it does is query winds up looking like this
    Java Code:
    "UPDATE Demo_table SET 'Mark' = 100 WHERE Name like 'DBSJ'"
    where you actually wanted
    Java 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.)

  3. #3
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default

    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.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    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.

  5. #5
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default

    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.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Show your entire SQL statement code.

  7. #7
    jason wang is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    I think it should like this:
    String str = " UPDATE Demo_table SET Mark = ? WHERE Name like ? ";
    PrepareStatement preStm = cn.prepareStatement(str);
    preStm.setDouble(1,100);
    preStm.setString(2,"DBSJ");
    I don't know whether the "Mark" is a column, if it is , then it should work.

Similar Threads

  1. problems with prepareStatement
    By Freddie in forum JDBC
    Replies: 0
    Last Post: 05-31-2007, 05:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •