2017-07-27 43 views
1

我试图使用HSQLDB,与春季JDBC模板一起。它工作正常,直到我使用Java 8的LocalDateTime类。HSQLDB,LocalDateTime,JdbcTemplate

我有这样的代码:

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 

在应用程序后端我使用LocalDateTime。我该如何做这项工作?

回答

0

您应该能够通过转换成LocalDateTime使用Timestamp.valueOf()一个java.sql.Timestamp来解决这个问题:

JdbcTemplate template = new JdbcTemplate(dataSource); 
template.update("INSERT INTO test VALUES (?)", Timestamp.valueOf(LocalDateTime.now())); 
+0

谢谢你,它的工作原理。与MySQL它甚至与LocalDateTime工作:) – BadQuestion