简体   繁体   中英

parameterized query in SQL prepared statement

I have a java program that connects to a database and I'm trying to update something in the database using prepared statements and parameterized queries. Here is part my code :

updateValSetId = con.prepareStatement("UPDATE COLUMNNAME " +
            "SET COLUMNDISPLAYNAME = ? + ' Value Set Identifier' " +
            "WHERE COLUMNDISPLAYNAME = ? + 'VALSETID' and TABLENAME = ?");

the first couple values I'm putting in for the question mark arguments are 1- Account, 2- ACCT, the third one doesn't matter. MY QUESTION IS---> is there any way to combine the question mark to a string value? the addition sign doesn't work I get the error " ORA=01722: invalid number "

after I looked up what that error meant I changed my code to something like this :

 updateValSetId = con.prepareStatement("UPDATE COLUMNNAME " +
            "SET COLUMNDISPLAYNAME = '? Value Set Identifier' " +
            "WHERE COLUMNDISPLAYNAME = '?VALSETID' and TABLENAME = ?");

that didn't work either. So again is there any way to combine the question marks with strings?

Thanks!

EDIT----------> I decided to take out the string text after the ? and put it in a different spot:

updateValSetId.setString(1, f.getValue() + " Value Set Identifier");
               updateValSetId.setString(2, f.getKey() + "VALSETID");
               updateValSetId.setString(3, e.getKey());
               updateValSetId.executeUpdate();

This is after my prepared statements, when I'm assigning the values to the ? parameter. the 'f' and the 'e' are hashmaps that I have data stored in, and I'm wondering why the above code doesn't work either when I add the string to the value i get from getValue and getKey. I don't get any errors, it compiles and runs but it doesn't update the values I want it to in the database. For example, ACCT is the first key and Account is the first value, so when they get passed in they should end up being added to the string I have after the getters, and therefore the database should update ACCTVALSETID to Account Value Set Identifier, right? What am I missing?

Thanks!

The edit I made is actually correct, I had errors in other parts of my code, the following code works:

updateValSetId.setString(1, f.getValue() + " Value Set Identifier");
           updateValSetId.setString(2, f.getKey() + "VALSETID");
           updateValSetId.setString(3, e.getKey());
           updateValSetId.executeUpdate();

So I guess you can't add string values to the ? parameter, but this works exactly the same as if you were adding it to the ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM