gpt4 book ai didi

java - 使用绑定(bind)变量创建 SQL 查询时出错 - Spring 中的 JDBCTemplate

转载 作者:行者123 更新时间:2023-12-02 03:30:58 24 4
gpt4 key购买 nike

我对 Spring - JDBC 非常初学者。我正在尝试使用具有绑定(bind)变量以及 IN 条件的查询从表中检索employee_id。

我收到 SQLException

" invalid column type" - Caused by: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [select employee_id from table_employee where age=:varTwo and marks in (:varOne) and name =:varThree]; SQL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type

请你告诉我哪里错了。

我尝试使用 Long 、 Integer 、 String 等类型,但仍然收到“无效的列类型”

年龄是 - NUMBER

分数是 - NUMBER

名称是 - VARCHAR

MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("varOne", varOne);
parameters.addValue("varTwo", Long.parseLong(varTwo));
parameters.addValue("varThree", varThree);
Long employeeId = jdbcTemplate.queryForObject("select employee_id from table_employee where age=:varTwo and marks in (:varOne) and name =:varThree" , Long.class , parameters);

我应该得到这个 SQL 的结果作为“员工 ID”。

最佳答案

我认为varOne的类型可能是Collection。当您想在 SQL 查询中使用变量(尤其是 IN)时,您应该确保变量的类型正确。

关于java - 使用绑定(bind)变量创建 SQL 查询时出错 - Spring 中的 JDBCTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56885313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com