preparedstatement,resultset和query问题

时间:2017-02-24 02:34:45

标签: java mysql servlets prepared-statement resultset

此方法应采用两个用户输入"低和"高"来自jsp页面,并在此方法中使用它们来获取价格介于" low"之间的属性列表。和"高"。

我在tomcat-log中看到的一个错误是:

MySQLSyntaxErrorException:SQL语法中有错误;查看与您的MariaDB服务器版本对应的手册,以获得在' BETWEEN 100000.0和300000.0'附近使用的正确语法。在第1行

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);
}
}

1 个答案:

答案 0 :(得分:2)

您的查询中缺少空格

String query = "select * from properties"
    + " WHERE price BETWEEN ? and ?";

如你所知,这个词会变成propertiesWHERE

相关问题