79224948

Date: 2024-11-26 00:28:07
Score: 0.5
Natty:
Report link

Does jdbcTemplate.update support a sub select in insert queries?

JDBC Template doesn't care what SQL you use... this is just passed to the database to process - so if your database supports the syntax (which you should try outside java in your database's shell)

What the problem, as @talex points out, is that you are not defining any bind variables in your SQL statement

Change your statement from:

String myInsert = "INSERT INTO myTable(col1, col2, col3,) values( val1, (SELECT thisVal from mySecondTable where myColumn = val2), val3)"

to

String myInsert = "INSERT INTO myTable(col1, col2, col3,) values( ?, (SELECT thisVal from mySecondTable where myColumn = ?), ?)"

This your program should work. See this tutorial: https://www.baeldung.com/spring-jdbc-jdbctemplate#1-basic-queries (many others exist)

Reasons:
  • Blacklisted phrase (1): this tutorial
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @talex
  • High reputation (-1):
Posted by: AndrewL