2017-02-24 71 views
0

該方法應該從jsp頁面獲取兩個用戶輸入「低」和「高」,並在此方法中使用它們來獲取價格介於「低」 。和「高」準備陳述,結果集和查詢問題

一個錯誤我在Tomcat的日誌中看到的是:

MySQLSyntaxErrorException:您的SQL語法錯誤;檢查對應於您的MariaDB的服務器版本的權利手冊語法在第1行'BETWEEN 100000.0和300000.0'附近使用

100000.0 an d 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); 
} 
} 

回答

2

你缺少你查詢的空間

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

,你有它的話會成爲propertiesWHERE

+0

從來沒有在我的生活我覺得很笨。我整天試圖解決這個問題,認爲我的代碼/邏輯有問題。謝謝。 –

+0

發生在我們身上 –

+0

@Brad當你意識到'System.out.println'(或一個體面的API)的功能和一些調試:P(沒有任何意圖) – MadProgrammer