gpt4 book ai didi

java - 如何在java中删除MySQL数据库中选中的记录(行)

转载 作者:行者123 更新时间:2023-11-29 05:06:47 27 4
gpt4 key购买 nike

如何删除MySQL数据库选中的记录(行)我用这个代码

connectDB();
try{
java.sql.PreparedStatement stmt = con.prepareStatement("DELETE FROM tbl_codes WHERE No=? ");
String sql = "Delete FROM user WHERE No= "+txt_search_code.getText();
pstmt = con.prepareStatement(sql);
pstmt.setString(1,txt_search_code.getText());
pstmt.execute();
JOptionPane.showMessageDialog(null, " Your new records Deleted Succsessfully!!");
} catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
closeDB();

但是它得到这样的错误-

java.sql.SQLException:Parameter index out of range(1>number of parameters,which is 0)

那么我该如何解决这个问题....(请举例)谢谢

最佳答案

试试这个:

connectDB();
try{
java.sql.PreparedStatement stmt = con.prepareStatement("DELETE FROM tbl_codes WHERE No=? ");
stmt.setString(1,txt_search_code.getText());
stmt.execute();
JOptionPane.showMessageDialog(null, " Your new records Deleted Succsessfully!!");
} catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
closeDB();

关于java - 如何在java中删除MySQL数据库中选中的记录(行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46419181/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com