Prepared expression. Can I skip a parameter?

I use queries like

"UPDATE MAILSCH.MESSAGE " + "SET IDFOLDER=?, SUBJECT=?, CONTENT=?, CREATIONTIME=?, AD_FROM=?, AD_TO=?, STATUS=? " + "WHERE IDMESSAGE=?"; 

Can I skip IDFOLDER without changing the request?

+6
java prepared-statement
source share
4 answers

Unfortunately not. Positional parameters ('?') - this is exactly what is determined by their position or the order in which they appear in the request. If you remove the IDFOLDER =? Identifier, you will assign incorrect parameters to the rest of the request and possibly get an exception, since the number of assigned parameters does not match the number expected in the request.

I assume that you cannot change the source code, since this is the easiest route - change SQL and then JDBC parameters to match. If you need to use the same number of parameters, you can write a query in such a way as not to change the IDFOLDER value, but use the first parameter.

 SET IDFOLDER=CASE ISNULL(?) WHEN 0 THEN IDFOLDER ELSE IDFOLDER END 

If your JDBC driver supports named parameters, this may give you a cleaner alternative.

+3
source share

No, you can’t. You need to conditionally insert this part of SQL into the SQL string, if necessary.

+4
source share

No, you have to write a second query that does not include the IDFOLDER column. All parameters must be related.

+4
source share

The javadoc does not explicitly state that you cannot, but when I tried it, I got this exception:

 java.sql.SQLException: Parameter not set 
0
source share

All Articles