I know that the SQL Query "INSERT INTO (col1, col2) ('someValue', DEFAULT)" will make sure that the value in col2 of the new record be whatever the default value for that column is. Is there a way to do this with a PreparedStatement?
I'm looking to use the following query with a PreparedStatement: "INSERT INTO (col1, col2) (?, ?)"
However, there is no PreparedStatement.setDefault(int). Hardcoding DEFAULT into the query, or only inserting the non-default columns would defeat the whole point of using PreparedStatements.
If you want to use a default value, don't specify the field in the SQL query - the database should then use its default value.
posted 13 years ago
Thanks for the reply Dave!
Unfortunately that defeats the point of using a PreparedStatement in preference of a plain old Statement. I would like to have a single prepared statement for this type of query, and execute it multiple times in a loop, setting different values from different columns. During some iterations, I will need to insert using the column's default values, which the java code doesn't know. If I don't set one of the (?)s using setXXX, I get an exception. There are actually something like 18 different columns in the table that can be default, so I can't just use two different PreparedStatements.