我想使用 JDBI/Dropwizard 在 maraiDB 数据库中插入一行。
我的表是用
CREATE TABLE parameter (
job_id INT references job(id),
name VARCHAR(150) NOT NULL,
content VARCHAR(150) NOT NULL
);
并使用接口将数据插入数据库
@SqlUpdate("INSERT INTO parameter (job_id, name , content) VALUES " +
"(:job_id, :name , :content)")
long insert(
@Bind("job_id") int job_id,
@Bind("name") String name,
@Bind("content") String content);
现在当我通过以下方式调用该方法时
private final ParameterJDBI parameterJDBI;
public void insert() {
parameterJDBI.insert(1, "name", "value");
}
我收到错误
ERROR [2018-06-14 15:39:46,083] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: 700d318fa5724df6
! java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
我还将签名更改为 long 并将第一个参数更改为1L
但错误仍然存在。我不明白长对象是从哪里来的。
您正在使用返回 int(更新/插入的行数(的@SqlUpdate。因此,您的方法声明应返回 int 或 void。
如果要返回从 SQL 语句生成的键,则应添加@GetGeneratedKeys