我在這裏搜索找到我的錯誤的解決方案,但沒有人符合我的問題,有沒有人幫我找到我的代碼中的錯誤!?錯誤:參數索引超出範圍(2>參數數量,這是1)
textField_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
Object selected = list_1.getSelectedValue();
Connection conn = null;
conn=DriverManager.getConnection("jdbc:mysql://localhost/flyer","root","000");
Statement st= conn.createStatement();
String query = "INSERT INTO flyer_item (discount) SELECT price*? FROM `item` where item_name='?' ";
// PreparedStatement ps = null;
int i = Integer.valueOf((textField_1.getText()));
i=i/100;
java.sql.PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1,i);
ps.setString(2, selected.toString());
ps.executeUpdate();
st.executeUpdate(query);
} catch (SQLException se){
System.out.println(se.getMessage());
}
} });
注意:mysql語句在工作臺中成功運行。 謝謝
太感謝你了,我是試過,但有錯誤:{您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在''附近使用正確的語法'? FROM item where item_name =?'在第1行} – sara
看起來這個錯誤來自**'st.executeUpdate(query);'**你不想要那一行。刪除。代碼使用*準備語句*(**'ps' **)。你根本不需要定義**'st' **。刪除行**'Statement st ='**以及。 – spencer7593
是真的,最後謝謝你的工作沒有任何錯誤出現!但仍然查詢不會在我的桌子上做任何事情。無論如何非常感謝您的幫助。 – sara