我从 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();
关于java - 使用带时间戳的 sql 时 ps.executeQuery(sql) 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28316526/