我正在尝试使用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
猜你喜欢
- 线上Spring CPU 高负载解决思路详解 2023-05-14
- SpringBoot集成Jasypt敏感信息加密的操作方法 2022-11-07
- java比较两个json文件的差异及说明 2023-06-06
- Java实现获取内网的所有IP地址 2023-01-02
- Java如何提供给第三方使用接口方法详解 2022-09-03
- Java与JavaScript前后端实现手机号验证码一键注册登陆抖音流程 2023-01-29
- Java图像处理之获取用户感兴趣的区域 2023-03-22
- MyBatis-Plus实现逻辑删除的示例代码 2022-11-12
- 微信小程序微信登录的实现方法详解(JAVA后台) 2023-03-07
- Mybatis如何实现@Select等注解动态组合SQL语句 2023-02-28