gpt4 book ai didi

java - 我想编写代码来用Java更新mysql数据。这是我的代码。它给出这样的错误 :

转载 作者:行者123 更新时间:2023-11-29 15:35:27 25 4
gpt4 key购买 nike

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
DefaultTableModel d1 = (DefaultTableModel)jTable2.getModel();
int selectIndex = jTable2.getSelectedRow();
int id = Integer.parseInt(d1.getValueAt(selectIndex, 0).toString());

String bname = txtname.getText();
CategoryItem citem = (CategoryItem) txtcategory.getSelectedItem();
AuthorItem aitem = (AuthorItem) txtauthor.getSelectedItem();
PublisherItem pitem = (PublisherItem) txtpub.getSelectedItem();

String contents = txtcontent.getText();
String pages = txtno.getText();
String edition = txtedition.getText();

try {
pst = con.prepareStatement("update book set bname= ? , category= ? , author= ? , publisher= ? , contents= ? , pages= ? , edition= ? , where id= ? ");
pst.setString(1, bname);
pst.setInt(2, citem.id);
pst.setInt(3, aitem.id);
pst.setInt(4, pitem.id);
pst.setString(5, contents);
pst.setString(6, pages);
pst.setString(7, edition);
pst.setInt(8,id);
int k=pst.executeUpdate();

if(k==1)
{
JOptionPane.showMessageDialog(this,"Book Added");
txtname.setText("");
txtcategory.setSelectedIndex(-1);
txtauthor.setSelectedIndex(-1);
txtpub.setSelectedIndex(-1);
txtcontent.setText("");
txtno.setText("");
txtedition.setText("");
}
else
{
JOptionPane.showMessageDialog(this,"Error");
}
// TODO add your handling code here:
} catch (SQLException ex) {
Logger.getLogger(Book.class.getName()).log(Level.SEVERE, null, ex);
}
}
// and it gives error like this
//com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check //the manual that corresponds to your MariaDB server version for the right syntax to use near 'where id= //1' at line 1

最佳答案

此错误的可能原因是您编写了错误的查询。请看下面:

 pst = con.prepareStatement("update book set bname= ? , category= ? , author= ? , publisher= ? , contents= ? , pages= ? , edition= ? , where id= ? ");

edition= ? , where

删除 where 之前的 ,

<小时/>

对 future 的提示,以免陷入 future

  1. 尝试自己逐行调试代码并识别行号。
  2. 仔细阅读异常。
  3. 如果问题出在 SQL 中(您可以通过异常名称来确定,如 MySQLSyntaxErrorException),则首先在数据库控制台手动执行查询。

关于java - 我想编写代码来用Java更新mysql数据。这是我的代码。它给出这样的错误 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58297027/

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