ORA-01810:格式代码出现两次:jdbctemplate

时间:2017-08-20 09:03:21

标签: sql oracle jdbc type-conversion

我正在玩一些日期并使用jdbctemplate查询,我收到以下错误。

// creating a LocalDate object with specific date & time   

LocalDate date = LocalDate.of(LocalDate.now().getYear(), LocalDate.now().getMonth(), LocalDate.now().getDayOfMonth());

System.out.println("date $$$$$$$$$$$" +date);

LocalDateTime datetime = date.atTime(LocalTime.MAX);
System.out.println("datetime " +datetime);

// is not required since above steps is the same. 
datetime.withHour(23).withMinute(59).withSecond(59);

System.out.println("datetime with " +datetime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh24:mm:ss");
System.out.println("formatter" +datetime.format(formatter));


 return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM test where status in ('ACTIVE','ACTIVE_P') AND CREATED<TO_dATE('"+datetime.format(formatter)+"','yyyy-MM-dd hh24:mm:ss')",Integer.class);

这是输出

date $$$$$$$$$$$2017-08-20
datetime 2017-08-20T23:59:59.999999999
datetime with 2017-08-20T23:59:59.999999999
formatter2017-08-20 1124:59:59
2017-08-20 01:53:00.029  INFO 12604 --- [pool-1-thread-1] com.zaxxer.hikari.HikariDataSource       : hikari-subscription - Started.
2017-08-20 01:53:00.034  WARN 12604 --- [pool-1-thread-1] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found, trying direct instantiation.
2017-08-20 01:53:00.949  INFO 12604 --- [pool-1-thread-1] com.zaxxer.hikari.pool.PoolBase          : hikari-subscription - Driver does not support get/set network timeout for connections. (oracle.jdbc.driver.T4CConnection.getNetworkTimeout()I)
2017-08-20 01:53:02.190  INFO 12604 --- [pool-1-thread-1] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2017-08-20 01:53:02.274  INFO 12604 --- [pool-1-thread-1] o.s.jdbc.support.SQLErrorCodesFactory    : SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
2017-08-20 01:53:02.283 ERROR 12604 --- [pool-1-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.

org.springframework.dao.DataIntegrityViolationException: StatementCallback; SQL [SELECT COUNT(*) FROM test where status in ('ACTIVE','ACTIVE_P') AND CREATED<TO_dATE('2017-08-20 1124:59:59','yyyy-MM-dd hh24:mm:ss')]; ORA-01810: format code appears twice
; nested exception is java.sql.SQLDataException: ORA-01810: format code appears twice

    at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:82) ~[spring-jdbc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) ~[spring-jdbc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:419) ~[spring-jdbc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:474) ~[spring-jdbc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:484) ~[spring-jdbc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:494) ~[spring-jdbc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:500) ~[spring-jdbc-4.3.10.RELEASE.jar:4.3.10.RELEASE]

有人能让我知道我哪里错了。我在日志中看到查询打印为

AND CREATED<TO_dATE('2017-08-20 1124:59:59','yyyy-MM-dd hh24:mm:ss')
1124,我认为这是问题,但无论我做什么,我都无法删除那个。

2 个答案:

答案 0 :(得分:5)

1)您必须更改:mm:to:mi:格式为掩码:

SELECT TO_DATE('2017-08-20 23:59:59','yyyy-MM-dd hh24:mi:ss') FROM DUAL

2)之后你必须编辑你的小时部分。

答案 1 :(得分:3)

使用HH在Java中格式化24小时,使用:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");