首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HSQLDB,LocalDateTime,JdbcTemplate

HSQLDB,LocalDateTime,JdbcTemplate
EN

Stack Overflow用户
提问于 2017-07-27 22:02:05
回答 1查看 7.8K关注 0票数 5

我正在尝试使用HSQLDB和spring模板。在我使用Java 8的LocalDateTime类之前,它工作得很好。

我有这样的代码:

代码语言:javascript
复制
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());
        }
    }

脚本如下所示:

代码语言:javascript
复制
CREATE TABLE test
(
  datetime DATETIME NOT NULL,
);

当我尝试运行它时,我会得到一个例外:

代码语言:javascript
复制
org.hsqldb.HsqlException: incompatible data type in conversion

在应用程序后端,我使用LocalDateTime。我怎么才能把这事做好?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-27 22:10:03

您应该能够通过使用LocalDateTimejava.sql.Timestamp转换为Timestamp.valueOf()来解决这个问题。

代码语言:javascript
复制
JdbcTemplate template = new JdbcTemplate(dataSource);
template.update("INSERT INTO test VALUES (?)",  Timestamp.valueOf(LocalDateTime.now()));
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45361743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档