Answer the question
In order to leave comments, you need to log in
Why doesn't JdbcTemplate execute query with parameters?
The task is to write a parameterized SQL query using the JDBC Template.
I create a table:
jdbcTemplate.execute(" CREATE TABLE test (id BIGINT PRIMARY KEY, test_id VARCHAR(50));");
Map<String, String> parameters = new HashMap();
parameters.put("test_id", "test");
jdbcTemplate.update(
"INSERT INTO test (id, test_id) VALUES (1, :test_id)",
parameters
);
"PreparedStatementCallback; bad SQL grammar [INSERT INTO test (id, test_id) VALUES (1, :test_id)]; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement \"INSERT INTO TEST (ID, TEST_ID) VALUES (1, :[*]TEST_ID) \"; expected \"), DEFAULT, NOT, EXISTS, INTERSECTS, SELECT, FROM, WITH\"; SQL statement:\nINSERT INTO test (id, test_id) VALUES (1, :test_id) [42001-197]"
Answer the question
In order to leave comments, you need to log in
We need a different template altogether, JDBC can't do that.
https://www.javatpoint.com/spring-NamedParameterJd...
jdbcTemplate.execute(" CREATE TABLE test (id BIGINT PRIMARY KEY, test_id VARCHAR(50));");
Map<String, String> parameters = new HashMap();
parameters.put("test_id", "test");
jdbcTemplate2.execute(
"INSERT INTO test (id, test_id) VALUES (1, :test_id)",
parameters,
(PreparedStatementCallback) PreparedStatement::executeUpdate
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question