准备语句、结果集和查询问题
preparedstatement, resultset, and query issue
下面的方法应该从 jsp 页面获取两个用户输入 "low and "high",并使用它们来获取价格介于 "low" 和 "high".
我在 tomcat-log 中看到的一个错误是:
MySQLSyntaxErrorException:您的 SQL 语法有误;检查
与您的 MariaDB 服务器版本相对应的手册,以获取正确的语法
在第 1 行
附近使用 'BETWEEN 100000.0 and 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);
}
}
您的查询中缺少 space
String query = "select * from properties"
+ " WHERE price BETWEEN ? and ?";
按照你的意思,这个词会变成 propertiesWHERE
下面的方法应该从 jsp 页面获取两个用户输入 "low and "high",并使用它们来获取价格介于 "low" 和 "high".
我在 tomcat-log 中看到的一个错误是:
MySQLSyntaxErrorException:您的 SQL 语法有误;检查 与您的 MariaDB 服务器版本相对应的手册,以获取正确的语法 在第 1 行
附近使用 'BETWEEN 100000.0 and 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);
}
}
您的查询中缺少 space
String query = "select * from properties"
+ " WHERE price BETWEEN ? and ?";
按照你的意思,这个词会变成 propertiesWHERE