【发布时间】:2017-02-24 02:34:45
【问题描述】:
下面的方法应该从 jsp 页面获取两个用户输入“low”和“high”,并使用它们来获取价格在“low”和“high”之间的属性列表。
我在我的 tomcat 日志中看到的一个错误是:
MySQLSyntaxErrorException:您的 SQL 语法有错误;检查 与您的 MariaDB 服务器版本相对应的手册,以获得正确的语法 在第 1 行的“BETWEEN 100000.0 和 300000.0”附近使用
100000.0 和 300000.0 是我输入的输入。
public static ArrayList<Property> search(double low, double high) {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "select * from properties"
+ "WHERE price BETWEEN ? and ?";
try {
ps = connection.prepareStatement(query);
ps.setDouble(1, low); //this should set user input = ?
ps.setDouble(2, high); //this should set user input = ?
rs = ps.executeQuery();
ArrayList<Property> list = new ArrayList<>();
while (rs.next()) {
Property p = new Property();
p.setName(rs.getString("name"));
p.setPrice(rs.getDouble("price"));
list.add(p);
}
return list;
} catch (SQLException e) {
System.out.println(e);
return null;
} finally {
DBUtil.closeResultSet(rs);
DBUtil.closePreparedStatement(ps);
pool.freeConnection(connection);
}
}
【问题讨论】:
标签: java jdbc prepared-statement resultset