我正在尝试从表中收集数据,然后运行两个日期之间的语句。该语句在 java 字符串中创建,然后由 resultSet 变量使用 statement.executeQuery() 以字符串作为参数。这就是我为两个日期创建字符串的方式:
Calendar today = Calendar.getInstance();
int month = today.get(Calendar.MONTH);
int year = today.get(Calendar.YEAR);
int endOfMonth = today.getActualMaximum(Calendar.DATE);
String sql;
if (month < 10) {
sql = "SELECT * FROM ORDERS WHERE DATE BETWEEN " + "#0" + month + "/01/" + year + "#" + " AND " + "#0" + month + "/" + endOfMonth + "/" + year + "#";
当我打印这个字符串时,结果是这样的:
SELECT * FROM ORDERS WHERE DATE BETWEEN #03/01/2015# AND #03/30/2015#
但是,当我通过 executeQuery 方法运行此字符串时,会发生此错误:
java.sql.SQLSyntaxErrorException: Lexical error at line 1, column 41. Encountered: "#" (35), after : "".