簡體   English   中英

使用MySQL和Java的where子句中的未知列錯誤

[英]Unknown column error in where clause, using MySQL and Java

以下是我的代碼行:

ResultSet rs3 = stmt6.executeQuery("SELECT * FROM ShopSystem.Order where s_id="+s_id+" AND status="+Pending);

我收到以下錯誤:

Unknown column 'Pending' in 'where clause'

可能是什么原因...我聽不懂..

毫無疑問, status是一個字符串,因此需要將其與字符串進行比較。 使用定界符:

SELECT * FROM ShopSystem.Order where s_id="+s_id+" AND status='"+Pending+"'"

或者更好的方法是,學習如何編寫使用參數替換將參數值放入SQL字符串的代碼。

更改為

AND status = '" + Pending + "'"

您需要將字符串放在引號中。 否則,數據庫會認為您的意思是列名。

但是實際上您應該使用Prepared Statements 然后,您無需像這樣將查詢打補丁在一起,也不必擔心參數並轉義它們...

不要串聯! 使用准備好的陳述

PreparedStatement stm = conn.prepareStatement("SELECT * FROM ShopSystem.Order where s_id = ? AND status = ?");
stm.setInt(1, s_id);
stm.setString(2, Pending.name());
ResultSet rs = stm.executeQuery();

在這種情況下,您必須使用PreparedStatement

// use the ? for the 2 entries values
    String selectSQL = new String("SELECT * FROM ShopSystem.Order where s_id=? AND status=?")
    preparedStatement = dbConnection.prepareStatement(selectSQL);
// in order you must incialise them here                
                 preparedStatement.setString(1, "s_id");
                preparedStatement.setString(2, "Pending");
//execute your resultset    `enter code here`
ResultSet rs = preparedStatement.executeQuery();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM