Q:

SQL Select where timestamp column is between two timestamps

I'm trying to get a SQL statement to select rows where a timestamp is between two given timestamps. here is what i tried. (this is on a Java servlet, and i java timestamp objects
SELECT * FROM dates WHERE dateAvailable >= " + from.toString() + " AND dateAvailable <= " + to.toString();

where from and to are timestamp objects
this is giving me a syntax error when trying to prepare the statement, please help

A:

JDBC has built-in types for this. Use
PreparedStatement ps = con.prepareStatement(
              "SELECT * FROM dates WHERE dateAvailable BETWEEN ? AND ?"); 
ps.setTimestamp(1, from);
ps.setTimestamp(2, to);

You specify two ? placeholder. You can set these to your TimeStamp elements.
Because it might not be immediately obvious, one purpose of using PreparedStatement is it helps prevent SQL injection. Read the answers here for more details.

The reason yours wasn't working is that Timestamp#toString() has a specific format that does not work with SQL.

