使用带时间戳的 sql 时 ps.executeQuery(sql) 处出错
Error at ps.executeQuery(sql) while using sql with timestamps
我正在从 JSP 获取日期并将它们转换为时间戳
DBConnection = ConnMgr.getConnection();
Timestamp timestamp1 = new java.sql.Timestamp(date1.getTime());
Timestamp timestamp2 = new java.sql.Timestamp(date2.getTime());
我有一个sql声明和准备的staement如下
String sql = " select * from images where uploadedOn>=? and uploadedOn<=?;";
ps = DBConnection.prepareStatement(sql);
ps.setTimestamp(1, timestamp1);
ps.setTimestamp(2, timestamp2);
System.out.println(ps);
此打印语句正在控制台中打印此
select * from images where uploadedOn>='2015-02-02 00:00:00' and uploadedOn<='2015-02-05 00:00:00';
复制到 mySQL 工作台和 运行 时工作正常。但它在以下语句中给出了错误
rs = ps.executeQuery(sql);
错误是
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? and uploadedOn<=?' at line 1
从昨天开始就在为此苦苦挣扎。谁能帮我解决这个问题?
语句已经准备好您不需要执行另一个查询
rs = ps.executeQuery();
我正在从 JSP 获取日期并将它们转换为时间戳
DBConnection = ConnMgr.getConnection();
Timestamp timestamp1 = new java.sql.Timestamp(date1.getTime());
Timestamp timestamp2 = new java.sql.Timestamp(date2.getTime());
我有一个sql声明和准备的staement如下
String sql = " select * from images where uploadedOn>=? and uploadedOn<=?;";
ps = DBConnection.prepareStatement(sql);
ps.setTimestamp(1, timestamp1);
ps.setTimestamp(2, timestamp2);
System.out.println(ps);
此打印语句正在控制台中打印此
select * from images where uploadedOn>='2015-02-02 00:00:00' and uploadedOn<='2015-02-05 00:00:00';
复制到 mySQL 工作台和 运行 时工作正常。但它在以下语句中给出了错误
rs = ps.executeQuery(sql);
错误是
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? and uploadedOn<=?' at line 1
从昨天开始就在为此苦苦挣扎。谁能帮我解决这个问题?
语句已经准备好您不需要执行另一个查询
rs = ps.executeQuery();