Tohid Makari
Tohid Makari

Reputation: 2494

Caused by: org.h2.jdbc.JdbcSQLDataException: Cannot parse "TIMESTAMP" constant

I Faced a little problem with a simple request H2 in test environment that is as follows:

Exception:

Caused by: org.h2.jdbc.JdbcSQLDataException: Cannot parse "TIMESTAMP" constant ":localDateTime"
String query="select * from Article E1 WHEN E1.date> " + "':localDateTime'" "

namedParameterJdbcTemplate.queryForObject(query, mapParameters(), Object.class);

private MapSqlParameterSource mapParameters() {
    DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("localDateTime", Timestamp.valueOf(LocalDateTime.now().format(dtf1)), TIMESTAMP);

}

Upvotes: 1

Views: 5780

Answers (1)

Stephen C
Stephen C

Reputation: 719689

Your problem is the quotes around the named parameter.

+ "':localDateTime'"

should be

+ ":localDateTime"

JDBC prepared statement parameters are not recognized inside a literal. So H2 sees a date literal that looks like ':localDateTime' which is doesn't recognize as a valid date.


I cannot find a definitive reference for this (e.g. in the JDBC specs) but prepared statement parameters can only be used in the SQL where a (complete) SQL value is required; e.g. a expression or sub-expression. You cannot use a parameter within a literal, or to parameterize a column name or table name. And you cannot use one to inject an expression to be evaluated on the SQL server side.

Upvotes: 1

Related Questions