Reputation: 11
The DB persist java.util.date
and i need a query to get all the data between 2 Dates with only using java.util.date.
I use the JPA 2.1
String sql = "SELECT * FROM table WHERE created > '"+date1+"' AND created < '"+date2+"'";
This query provides always a List with 0 elements
Upvotes: 1
Views: 601
Reputation: 45309
The problem is that your dates are being added to the SQL query as string values. The database is getting something like:
String sql = "SELECT * FROM table WHERE created > 'Wed May 25 14:33:18 SAST 2016' and created < 'Wed May 25 14:33:18 SAST 2016'";
The database is seeing it as a string and that explains why it isn't working.
The single best way to resolve it is use bind variables::
String sql = ""SELECT * FROM table WHERE created > ? AND created < ?"
//Create the query object using that SQL string, then set values
sqlQuery.setParameter(1, date1);
sqlQuery.setParameter(2, date2);
Then execute the prepared statement.
Upvotes: 3