java - 库存管理系统、MySQL、Netbeans 上的更新按钮出现问题

标签 java mysql sql netbeans

我有一个简单的库存管理系统,可以通过 mySQL 数据库插入、编辑和删除项目。当我尝试更新任何信息时,它会运行此错误。

Apr 03, 2019 3:49:08 PM java_project_1_2.Main_Window btn_updateActionPerformed
SEVERE: null
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 = '115'' at line 1
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
 at com.mysql.jdbc.Util.getInstance(Util.java:386)
.....

我的库存管理系统还处理照片,因此在没有任何照片的情况下更新时,我自然有第二个代码块。但是当我这样做时,它会以弹出窗口的形式抛出另一个错误,提示“没有为参数 7 指定值。

这是我的更新按钮的所有代码。

private void btn_updateActionPerformed(java.awt.event.ActionEvent evt) {

        if(checkInputs() && txt_id.getText() != null)
        {
            String UpdateQuery = null;
            PreparedStatement ps = null;
            Connection con = getConnection();

            //update without image
            if(ImgPath == null)
            {
                try {
                    UpdateQuery = "UPDATE products SET name = ?, price = ?"
                            + ", add_date = ?, category = ?, quantity = ?, WHERE id = ?";
                    ps = con.prepareStatement(UpdateQuery);

                    ps.setString(1, txt_name.getText());
                    ps.setString(2, txt_price.getText());

                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String addDate = dateFormat.format(txt_AddDate.getDate());

                    ps.setString(3, addDate);

                    ps.setInt(4, Integer.parseInt(txt_id.getText()));

                    String value = combo_category.getSelectedItem().toString();
                    ps.setString(5, value);

                    ps.setString(6, txt_quantity.getText());


                    ps.executeUpdate();
                    Show_Products_In_JTable();
                    JOptionPane.showMessageDialog(null, "Product Updated");

                } catch (SQLException ex) {
                    Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
            //update with Image
            else{
                try{
                InputStream img = new FileInputStream(new File(ImgPath));

                UpdateQuery = "UPDATE products SET name = ?, price = ?"
                            + ", add_date = ?,image = ?, category = ?, quantity = ?, WHERE id = ?";

                    ps = con.prepareStatement(UpdateQuery);

                    ps.setString(1, txt_name.getText());
                    ps.setString(2, txt_price.getText());

                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String addDate = dateFormat.format(txt_AddDate.getDate());

                    ps.setString(3, addDate);

                    ps.setBlob(4, img);

                    String value = combo_category.getSelectedItem().toString();
                    ps.setString(5, value);


                    ps.setInt(6, Integer.parseInt(txt_id.getText()));

                    ps.executeUpdate();
                    Show_Products_In_JTable();
                    JOptionPane.showMessageDialog(null, "Product Updated");

            }catch(Exception ex)      
            {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }
        }else{
            JOptionPane.showMessageDialog(null, "One or More Fields Are Exmpty Or Wrong");
        } 
    }

最佳答案

不应在任何语句关键字前放置逗号。

private void btn_updateActionPerformed(java.awt.event.ActionEvent evt) {

    if(checkInputs() && txt_id.getText() != null)
    {
        String UpdateQuery = null;
        PreparedStatement ps = null;
        Connection con = getConnection();

        //update without image
        if(ImgPath == null)
        {
            try {
                UpdateQuery = "UPDATE products SET name = ?, price = ?"
                        + ", add_date = ?, category = ?, quantity = ? WHERE id = ?";
                ps = con.prepareStatement(UpdateQuery);

                ps.setString(1, txt_name.getText());
                ps.setString(2, txt_price.getText());

                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String addDate = dateFormat.format(txt_AddDate.getDate());

                ps.setString(3, addDate);

                ps.setInt(4, Integer.parseInt(txt_id.getText()));

                String value = combo_category.getSelectedItem().toString();
                ps.setString(5, value);

                ps.setString(6, txt_quantity.getText());


                ps.executeUpdate();
                Show_Products_In_JTable();
                JOptionPane.showMessageDialog(null, "Product Updated");

            } catch (SQLException ex) {
                Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        //update with Image
        else{
            try{
            InputStream img = new FileInputStream(new File(ImgPath));

            UpdateQuery = "UPDATE products SET name = ?, price = ?"
                        + ", add_date = ?,image = ?, category = ?, quantity = ? WHERE id = ?";

                ps = con.prepareStatement(UpdateQuery);

                ps.setString(1, txt_name.getText());
                ps.setString(2, txt_price.getText());

                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String addDate = dateFormat.format(txt_AddDate.getDate());

                ps.setString(3, addDate);

                ps.setBlob(4, img);

                String value = combo_category.getSelectedItem().toString();
                ps.setString(5, value);


                ps.setInt(6, Integer.parseInt(txt_id.getText()));

                ps.executeUpdate();
                Show_Products_In_JTable();
                JOptionPane.showMessageDialog(null, "Product Updated");

        }catch(Exception ex)      
        {
                JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }
    }else{
        JOptionPane.showMessageDialog(null, "One or More Fields Are Exmpty Or Wrong");
    } 
}

关于java - 库存管理系统、MySQL、Netbeans 上的更新按钮出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55498191/

相关文章:

php - 在 wordpress 中使用 mysql 自定义查询获取 WP_Query 对象

php - 显示基于数据库中两个表的下拉选择值

sql - 尺寸变化缓慢,总是更新

php - 与 zend Framework 2 相关的数据库表

Java CardLayout : Is it possible to use same object on two different cards?

java - 避免使用instanceof,但将函数参数限制为父类(super class)的一组子类

java - 与 netbeans jframes 链接的数据库

java - 如何向 JOptionPane 按钮添加鼠标监听器?

java - 如何通过 Java 客户端和 Python 服务器通过套接字创建 IPC?

php - 这两个查询哪一个更快?