2017-08-20 1 views
1

Je joue avec quelques dates et requêtes avec jdbctemplate et j'obtiens l'erreur ci-dessous.ORA-01810: le code de format apparaît deux fois: 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); 

c'est la sortie

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] 

Quelqu'un peut-il me faire savoir où vais-je tort. Je vois que dans les journaux de la requête est imprimé comme

AND CREATED<TO_dATE('2017-08-20 1124:59:59','yyyy-MM-dd hh24:mm:ss') 

1124, je pense que c'est le problème, mais tout ce que je fais, je ne suis pas en mesure de supprimer cette 11.

+0

il y a 'T' dans votre chaîne datetime, mais pas dans le modèle de formatage ' 2017-08-20T23: 59: 59.99' vs 'aaaa-MM-jj hh24: mm: ss' – are

+0

Utiliser une instruction préparée et passer un instance de l'instance 'java.sql.Timestamp'. Ne jamais transmettre des dates, des horodatages ou des nombres comme des chaînes. –

+0

ceci est juste pour quelque chose de mon propre que je ne fais rien pour la production mais juste pour mon information pourquoi nous ne devrions jamais passer des dates, des timestamps, des nombres comme des chaînes? – user641887

Répondre

4

1) Vous devez changer: mm: à: mi: dans le masque de format:

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

2) Après cela, vous devez modifier votre section d'heures.

3

24 heures est formaté en Java avec HH, utilisez:

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