I am creating a method to update a row in a SQL Server 2008 database. The SQL String looks something like this:
private static final String UPDATE_ROW =
"UPDATE MyTable SET FieldOne = ?, FieldTwo = ? " +
"WHERE IDField = ?";
It’s simplified, of course. But the rub is that not all of the fields will necessarily change. I know that with straight SQL you can just put the field name in for the value and nothing will change; however, I don’t know how to do this with the Java PreparedStatement.
I could work around it by calling one update for each field to be changed (there are up to ten) for each row, but that is just fugly and I would really like to avoid it. Can anyone tell me how to put the field name in as a parameter value, or at least give me a clean solution?
I couldn’t find a way to do what I described, so I ended up reading the values of the things I was updating and passing in those values.