我在这里搜索找到我的错误的解决方案,但没有人符合我的问题,有没有人帮我找到我的代码中的错误!?错误:参数索引超出范围(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