2015-08-19 38 views
2

我試圖做一個JDBC的測試。我從schema.sql文件創建表,我從data.sql文件插入數據到表中。當我試圖做一個測試,我得到一個錯誤:SQLDataException:從HSQLDB無效的日期時間格式

java.sql.SQLDataException: data exception: invalid datetime format 

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'engine' defined in class path resource [META-INF/spring/mydocuments-jdbc-context.xml]: Cannot resolve reference to bean 'documentDAO' while setting bean property 'documentDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'documentDAO' defined in class path resource [META-INF/spring/mydocuments-jdbc-context.xml]: Invocation of init method failed; nested exception is java.lang.RuntimeException: java.sql.SQLDataException: data exception: invalid datetime format 

我不明白爲什麼,因爲我的日期格式是正確的:例如

'2014-02-24 11:52' 

所以,在文檔中是關於日期格式的例子。 這裏是我的我的文檔-JDBC-context.xml的

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 


    <context:property-placeholder location="jdbc.properties"/> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${jdbc.driverClassName}"/> 
     <property name="url" value="${jdbc.url}"/> 
     <property name="username" value="${jdbc.username}"/> 
     <property name="password" value="${jdbc.password}"/> 
    </bean> 

    <bean id="engine" class="spring.service.SearchEngineService"> 
     <property name="documentDAO" ref="documentDAO"/> 
    </bean> 

    <bean id="documentDAO" class="spring.data.DocumentRepository" init-method="initialize"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="schema" value="classpath:META-INF/data/schema.sql"/> 
     <property name="data" value="classpath:META-INF/data/data.sql"/> 
     <property name="queryAll"> 
      <value> 
       select d.documentId, d.name, d.location, d.description as doc_desc, d.typeId, d.created, d.modified, 
       t.name as type_name, t.description as type_desc, t.extension from documents d 
       join types t 
       on d.typeId = t.typeId 
      </value> 
     </property> 
    </bean> 

</beans> 

,這裏是我的data.sql文件

INSERT INTO types (typeId, name, description, extension) VALUES ('41e2d211-6396-4f23-9690-77bc2820d84b', 'PDF', 'Portable Document Format', '.pdf'); 
INSERT INTO documents (documentId, name, location, description, typeId, created, modified) VALUES ('431cddbf-f3c0-4076-8c1c-564e7dce16c9', 'Pro Spring Security Book', 'http://www.apress.com/9781430248187', 'Excellent Book', '4980d2e4-a424-4ff4-a0b2-476039682f43', '2014-02-14', '2014-02-20'); 
+0

什麼樣的列類型是'documents.created'和'documents.modified'? – Benvorth

+0

創建日期時間NOT NULL, 修改日期時間NOT NULL, – Algeroth

回答

3

嘗試修改您的data.sql文件,以有效的datetime值,而不只是日期值:

INSERT INTO documents (
    documentId, name, location, description, typeId, created, modified) 
    VALUES ('431cddbf-f3c0-4076-8c1c-564e7dce16c9', 'Pro Spring Security Book', 
    'http://www.apress.com/9781430248187', 
    'Excellent Book', '4980d2e4-a424-4ff4-a0b2-476039682f43', 
    '2014-02-14 00:00:00', '2014-02-20 00:00:00' 
); 
+0

哇,它的工作!但是,我看不出我和你的代碼有什麼不同,你能解釋一下嗎?因爲在另一個例子中,我添加了幾分鐘和兩秒鐘,但它不起作用。 – Algeroth

+0

在你的代碼中,'created'和'modified'的最後兩個值只是'2014-02-14'和'2014-02-20' - 沒有時間。你的方式數據庫認爲它會得到一個只有數據的字段。 – Benvorth

+0

非常感謝:) – Algeroth

相關問題