Search code examples
javaoracle-databasejdbctemplate

java.sql.BatchUpdateException: ORA-01849: hour must be between 1 and 12


I am trying to store current Timestamp in Oracle database using JdbcTemplate. There are two columns in which I am trying to store timestamp , but one column is VARCHAR and one column is TIMESTAMP, but looking into the exception I am unable to find out for which column it it throwing above error, and the best part is on my local machine I am getting no such error but when I deploy my code to higher environment , I am facing such issue.

Query-

 String query = "insert into IP_CO_WR2_USR.customer_info "
            + "(EVENT_ID,COMM_CODE,customer_id,REG_ACCOUNT"
            + ",DFMSTATEMENTFLAG,ENVIRONMENT,STATUS,"
            + "INSERT_TIMESTAMP,INSERTED_BY,UPDATE_TIMESTAMP,MODELLINKDATE,"
            + "OCCURANCE_TIMESTAMP,COMM_SOURCE,REPORTENDDATE,MODELID,UAN,BENCHMARKID,"
            + "COUNTER,DFMUAN,RUNTYPE,GENERATED_EVENT_ID)"
            + " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 

Data Insertion Logic

        coJdbcTemplate.batchUpdate(query, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                CustomerInfo tranMessage = batch.get(i);
                ps.setString(8, new SimpleDateFormat("dd-MMM-yy HH.mm.ss")
                        .format(new Date()));
                ps.setString(9, StringConstants.TMTriggerManager);
                ps.setString(10, new SimpleDateFormat("dd-MMM-yy HH.mm.ss")
                        .format(new Date()));

            }

            public int getBatchSize() {
                return batch.size();
            }
        });

Solution

  • You are using a wrong format in SimpleDateFormat

    ps.setString(8, new SimpleDateFormat("dd-MMM-yy HH.mm.ss").format(new Date()));

    You need use:

    ps.setString(8, new SimpleDateFormat("dd-MMM-yy hh.mm.ss").format(new Date()));

    The "HH" in uppercase set hours between 1 and 24. 'hh' solves it.

    You can see documentation on SimpleDateFormat in https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

    You may have allowed the handling of hours between 0 and 24 in your local database and in that of the server no. Another option would be to change this in that database