【问题标题】:preparedstatement, resultset, and query issue [closed]准备好的语句,结果集和查询问题[关闭]
【发布时间】: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


    【解决方案1】:

    您的查询中缺少空格

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

    正如你所拥有的那样,这个词会变成propertiesWHERE

    【讨论】:

    • 我这辈子从未感到如此愚蠢。我整天都在尝试解决这个问题,认为我的代码/逻辑有问题。谢谢。
    • 发生在我们所有人身上
    • @Brad 这是一个美好的时刻,当您意识到System.out.println(或体面的日志 API)和一些调试功能的强大功能:P(无意冒犯)
    • @ScaryWombat 是的,但是你最后一次在 SO 上发布它是什么时候;)
    • @MadProgrammer 哈哈,我知道,相信我我尝试过使用它们,但是 netbeans 和 tomcat 并没有提供最好的输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多