首页 > 解决方案 > JDBI:插入数据时的长/整数异常

问题描述

我想用 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但错误仍然存​​在。我不明白长对象来自哪里。

标签: javadropwizardjdbi

解决方案


您正在使用返回 int(更新/插入的行数)的 @SqlUpdate。因此,您的方法声明应返回 int 或 void。

如果要返回从 SQL 语句生成的键,则应添加@GetGeneratedKeys


推荐阅读