簡體   English   中英

我想寫代碼來更新 Java 中的 mysql 數據。 這是我的代碼。 它給出了這樣的錯誤:

[英]i want to write to code to update mysql data in Java. Here is my code. and it gives error like this:

 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= ? ");

版本=? , 在哪里

在 where 之前刪除


未來的提示,這樣就不會卡在未來

  1. 嘗試自己逐行調試代碼並識別行號。
  2. 仔細閱讀異常。
  3. 如果問題出在 SQL 中(您可以通過此處的異常名稱確定MySQLSyntaxErrorException ),然后首先在數據庫控制台手動執行查詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM