java - 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
但错误仍然存在。我不明白长对象来自哪里。
解决方案
您正在使用返回 int(更新/插入的行数)的 @SqlUpdate。因此,您的方法声明应返回 int 或 void。
如果要返回从 SQL 语句生成的键,则应添加@GetGeneratedKeys
推荐阅读
- java - 如何通过不同的运算符比较变量和字符串
- python-3.x - 无法将 Python 脚本转换为 .exe
- javascript - 使用 JavaScript 保存 blob 始终会保存一个空文件
- javascript - JavaScript 双击卡片
- amazon-web-services - 使用 Sqoop 将表从 AWS RDS 移动到 AWS EMR 的问题
- python - 为什么“conda upgrade”在克隆环境中表现不同?
- windows - 是否有可与 UWP 联系人选取器一起使用的不可变 ID?
- arrays - 将 JS JSON 数组解析为 Kotlin 数组
- r - R包ODEsensitive中的下标越界错误
- ios - 在我的应用播放音频期间,在 iOS 上静音其他应用的音频