我正在尝试使用HSQLDB和spring JDBC模板.它工作正常,直到我使用Java 8的LocalDateTime类.我有这个代码:import org.hsqldb.jdbc.JDBCDataSource;import org.springframework.core.io.ClassPathResource;import or...

我正在尝试使用HSQLDB和spring JDBC模板.它工作正常,直到我使用Java 8的LocalDateTime类.
我有这个代码:
import org.hsqldb.jdbc.JDBCDataSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import java.time.LocalDateTime;
public class Test
{
public static void main(String[] args) throws Exception
{
JDBCDataSource dataSource = new JDBCDataSource();
dataSource.setUser("SA");
dataSource.setPassword("");
dataSource.setUrl("jdbc:hsqldb:mem:db");
Resource resource = new ClassPathResource("/test.sql");
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(resource);
databasePopulator.execute(dataSource);
JdbcTemplate template = new JdbcTemplate(dataSource);
template.update("INSERT INTO test VALUES (?)", LocalDateTime.now());
}
}
该脚本如下所示:
CREATE TABLE test
(
datetime DATETIME NOT NULL,
);
当我尝试运行它时,我会得到异常:
org.hsqldb.HsqlException: incompatible data type in conversion
在app后端我使用LocalDateTime.我怎样才能做到这一点?
解决方法:
您应该能够通过使用Timestamp.valueOf()将LocalDateTime转换为java.sql.Timestamp来解决此问题:
JdbcTemplate template = new JdbcTemplate(dataSource);
template.update("INSERT INTO test VALUES (?)", Timestamp.valueOf(LocalDateTime.now()));
织梦狗教程
本文标题为:java – HSQLDB,LocalDateTime,JdbcTemplate


基础教程推荐
猜你喜欢
- Java深入讲解Bean作用域与生命周期 2023-01-18
- 从零搭建SpringBoot+MyBatisPlus快速开发脚手架 2023-01-18
- JavaCV实现读取视频信息及自动截取封面图详解 2022-12-14
- Eureka源码阅读Client启动入口注册续约及定时任务 2023-06-16
- 关于文件上传MultipartBody的使用方法 2022-12-14
- lazy init控制加载在Spring中如何实现源码分析 2023-05-13
- Java设计模式之单例和原型 2023-05-31
- Java聊天室之实现获取Socket功能 2023-06-17
- 关于二分法查找Java的实现及解析 2023-03-06
- Spring多定时任务@Scheduled执行阻塞问题解决 2022-11-14