我正在尝试使用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 IOC容器的Bean管理XML自动装配过程 2022-12-02
- Spring mvc实现Restful返回json格式数据实例详解 2023-08-01
- java连接postgresql数据库代码及maven配置方式 2023-06-01
- Java InheritableThreadLocal使用示例详解 2023-06-01
- Spring @Order注解使用详解 2023-04-12
- springboot实现指定mybatis中mapper文件扫描路径 2022-12-10
- Java NIO中四大核心组件的使用详解 2023-07-15
- Java模板方法讲解 2023-10-08
- Linux Centos安装jdk 之前照着网上安装了好多次 结果都是javac编译失败,出现javac :command not found 2023-09-01
- Java中HashMap如何解决哈希冲突 2022-11-19
