我想写代码来更新Java中的mysql数据。这是我的代码。它给出了这样的错误:

时间:2019-10-09 04:22:02

标签: java mysql syntax-error mysql-error-1064 mysqlupgrade

 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

1 个答案:

答案 0 :(得分:0)

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

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

edition =? ,

在之前删除


提示未来,以免陷入未来

  1. 尝试自己一行一行地调试代码并标识行号。
  2. 仔细阅读异常。
  3. 如果SQL中有问题(您可以在此处通过 MySQLSyntaxErrorException 确定异常名称),然后首先在数据库控制台上手动执行查询。