What is the JDBC syntax for using a date literal or variable in a standard Statement?

Joe Sam Shirah

While you are generally advised to use a PreparedStatement and let the driver handle the syntax, the following is the standard form for regular Statements. The general escape syntax for standard SQL date columns is:

{d 'yyyy-mm-dd'}

For literals:

String sSQL = 
"SELECT colName FROM aTable " +
"WHERE  colDate = {d '2001-10-21'}"

For variables ( note that toString() is implicitly called on the date variable ):
java.sql.Date jsqlDate;
... // set date
String sSQL = 
"SELECT colName FROM aTable " +
"WHERE  colDate = {d '" + jsqlDate + "'}" 
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.