my code is:
java.sql.Date fromDate= new java.sql.Date(date1);
java.sql.Date toDate= new java.sql.Date(date2);
String select = "SELECT * FROM Table WHERE Date between " + fromDate+ " and" + toDate;
I´m using Derby database, and I have to run this query but return error. How can I do? Thanks.
First, stop building SQL like that. It's vulnerable to SQL injection attacks, conversion issues (which is probably the problem here) and it's hard to read.
Use parameterized SQL instead:
// TODO: Close the statement, e.g. using a try-with-resources statement
// or a finally block.
PreparedStatement statement =
conn.prepareStatement("SELECT * FROM Table WHERE Date between ? and ?");
statement.setDate(1, fromDate);
statement.setDate(2, toDate);
ResultSet results = statement.executeQuery();
// Use the results
This may well be enough to fix the problems immediately. If it's not, please give more details.
See more on this question at Stackoverflow