S
S
Sland Show2019-06-25 11:09:26
Java
Sland Show, 2019-06-25 11:09:26

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));");

I declare parameters:
Map<String, String> parameters = new HashMap();
        parameters.put("test_id", "test");

Update:
jdbcTemplate.update(
                "INSERT INTO test (id, test_id) VALUES (1, :test_id)",
                parameters
        );

At the exit:
"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]"

I also tried this option , but it still didn't work.
How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sland Show, 2019-06-25
@SlandShow

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 question

Ask a Question

731 491 924 answers to any question