伙计们,我试图更新数据到我的数据库中,我已经创建了插入和删除选项,但创建这个更新查询后,我的数据没有在数据库中更新,所以我请求你的帮助,请帮助我,我的问题是在视频和教程在线可用的数据更新使用where语句,其中他们使用一个整数数据类型,但我的数据库没有任何整数字段,我只有字符串字段和数据没有被更新,请帮助更新按钮的代码张贴在下面。enter code here
JButton ud_btn_Update = new JButton("Update");
ud_btn_Update.setFont(new Font("Tahoma", Font.BOLD, 20));
ud_btn_Update.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try { String query="update Customers set "
+ "Name='"+ud_tf_Name.getText()+"' "
+ ",Address='"+ud_tf_Address.getText()+"' "
+ ",Gstin='"+ud_tf_Gstin.getText()+"' "
+ ",Discount='"+ud_tf_Discount.getText()+"' "
+ ",State='"+ud_tf_State.getText()+"' "
+ ",StateCode='"+ud_tf_StateCode.getText()+"' "
+ " where Name='"+ud_tf_Name.getText()+"' ";
PreparedStatement pst = connection.prepareStatement(query);
pst.execute();
JOptionPane.showMessageDialog(null, "Data Updated!");
pst.close();
} catch(Exception e4) {
e4.printStackTrace();
}
你的代码在语法上似乎是正确的,如果表存在,并且表和列名没有拼写错误,它应该工作。可能导致代码不能更新表中的行的原因是 where
条款。 这个值。
ud_tf_Name.getText()
很明显是你要替换列中旧值的新名字。name
,对吗?所以,它不存在于表格中,而这个 where
子句不会返回任何记录,也不会更新任何内容。你必须将旧的名字保存在一个字符串变量中,比如说 oldname
并将其用于 where
子句。
............................................
+ " where Name='"+oldname+"' ";
你也要学会使用 与准备好的声明的参数 以创建更安全的代码。