I was trying to insert a date into oracle database but I keep facing an conversion issue.
public List<Customer> getCustomerData(Date start, Date end) {
...
String query = "SELECT name FROM customers WHERE trunc(order_timestamp) between trunc(?) and trunc(?)";
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
pst.setDate(1, new java.sql.Date(sdf.parse(sdf.format(start)).getTime()));
pst.setDate(2, new java.sql.Date(sdf.parse(sdf.format(end)).getTime()));
}
From the caller I have
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date start = sdf.parse("10/10/2014");
Date end = sdf.parse("10/10/2016");
customerDao.getCustomerData(start,end);
Error I got was:
Caused by: org.h2.jdbc.JdbcSQLException: Data conversion error converting "2014-10-10"; SQL statement:
select name from customers where trunc(order_timestamp) between trunc(?) and trunc(?) [22018-160]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:158)
at org.h2.value.Value.convertTo(Value.java:852)
at org.h2.engine.FunctionAlias$JavaMethod.getValue(FunctionAlias.java:366)
at org.h2.expression.JavaFunction.getValue(JavaFunction.java:38)
at org.h2.expression.Comparison.getValue(Comparison.java:206)
at org.h2.expression.ConditionAndOr.getValue(ConditionAndOr.java:83)
at org.h2.expression.ConditionAndOr.getValue(ConditionAndOr.java:90)
at org.h2.expression.Expression.getBooleanValue(Expression.java:180)
at org.h2.command.dml.Select.queryFlat(Select.java:514)
at org.h2.command.dml.Select.queryWithoutCache(Select.java:617)
at org.h2.command.dml.Query.query(Query.java:298)
at org.h2.command.dml.Query.query(Query.java:268)
at org.h2.command.dml.Query.query(Query.java:37)
at org.h2.command.CommandContainer.query(CommandContainer.java:82)
at org.h2.command.Command.executeQuery(Command.java:185)
at org.h2.jdbc.JdbcPreparedStatement.executeQuery(JdbcPreparedStatement.java:96)
at com.customerDaoImpl.custDao.getParentContextdata(custDao.java:431)
... 29 more
Caused by: java.lang.NumberFormatException: For input string: "2014-10-10"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
at java.lang.Double.parseDouble(Double.java:510)
at org.h2.value.Value.convertTo(Value.java:831)
... 44 more
what I think happened was that getTime() will reformat the date into yyyy-MM-dd format and while the input was n MM/dd/yyyy format. But I am not sure if this is exactly what cause the issue.
Edit: H2 doesnt support trunc but you are able to write external script to be invoked.
in Customer.sql
drop ALIAS if exists TRUNC;
CREATE ALIAS TRUNC as '
import java.text.*;
@CODE
double trunc(double num) throws Exception {
return Math.floor(num*10) /10;
}
';
yyyy-MM-dd) or helper method that would try parsing by multiple formats is you're not sure about exact format of input."yyyy-MM-dd"and your pattern is"MM/dd/yyyy". How do you expect this to work?pst.setDate(1, new java.sql.Date(sdf.parse(sdf.format(start)).getTime()));when you could do this:pst.setDate(1, new java.sql.Date(start.getTime()));?