【问题标题】:Unable to insert record into mysql database无法将记录插入mysql数据库
【发布时间】:2020-08-08 19:13:33
【问题描述】:

您好,我正在尝试通过单击名称为“请求”按钮的按钮将数据插入 mysql 数据库。但是我不能这样做,并且不断弹出错误。您可以在下方滚动查看代码和错误,下面有一张图片显示了我的数据库中的列。

代码在这里:

private void RequestButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        String type = txttype.getSelectedItem().toString();
        String name = txtname.getText();
        String quantity = txtquantity.getText();
        
        try {
            // String query = "INSERT INTO `orders`(`id`, `itemtype`, `itemname`, `quantity_ordered`, `userid`, `status`) VALUES (?,?,?,?,?,?)";
            Class.forName("com.mysql.jdbc.Driver");
            con1 = DriverManager.getConnection("jdbc:mysql://localhost/restock", "root", "password");
            pst = con1.prepareStatement("insert into orders (itemtype, itemname, quantity_ordered) values(?,?,?)");
            pst.setString(1, type);
            pst.setString(2, name);
            pst.setString(3, quantity); 
            pst.executeUpdate();
            JOptionPane.showMessageDialog(null, "Item has been requested");
              
            // Set the textfields to empty upon button click
            txttype.setSelectedIndex(-1);
            txtname.setText("");
            txtquantity.setText("");
            txttype.requestFocus();
           
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
        }

    }  

错误:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Aug 09, 2020 3:04:50 AM pleasework.usermainpage RequestButtonActionPerformed
SEVERE: null
java.sql.SQLException: Field 'userid' doesn't have a default value
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025)
    at pleasework.usermainpage.RequestButtonActionPerformed(usermainpage.java:382)
    at pleasework.usermainpage.access$400(usermainpage.java:24)
    at pleasework.usermainpage$5.actionPerformed(usermainpage.java:211)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6539)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6304)
    at java.awt.Container.processEvent(Container.java:2239)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2297)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
    at java.awt.Container.dispatchEventImpl(Container.java:2283)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

mysql数据库上的图像:(记录已经预先添加,这里唯一的问题是当我尝试插入新记录时,发生错误并且它没有被发送到数据库。另外需要注意的事情是那个userid是一个外键。)

【问题讨论】:

  • 我不确定 MySQL,但从消息看来 userid 是一个 not null 列,您没有为它传递任何值。
  • 尝试传递 userid 列的值或为该列设置默认值或使其可为空。
  • 您的表中显然有一个名为 userid 的字段,但您的插入查询没有为该字段提供值,并且没有定义默认值。因此,要么为列定义一个默认值(例如一个自动递增的值),要么在插入查询中提供该值
  • @derpirscher 指出,我可能会尝试对此进行自动增量,如果它正常工作并更新您

标签: java mysql sql


【解决方案1】:

我认为userid 列是requirednot_null 或类似的东西,要求您始终具有默认值。解决方案是修改表以包含默认值(或将列设为nullable)或将数据传递到您的插入语句中。

【讨论】:

    【解决方案2】:

    您没有为 userid 字段提供任何值。将您的查询更改为类似的内容并为userid 列提供值。

       pst = con1.prepareStatement("insert into orders (userid, itemtype, itemname, quantity_ordered) values(?,?,?,?)");
    

    如果在这种情况下userid 需要为NULL,这就是它不包含在您的查询中的原因,那么您需要更改您的表以允许NULLuserid 列中,以便您的原始查询可以工作符合预期。

    【讨论】:

    • 因为 userid 是一个外键,我没有看到任何通过插入提供值的原因,我可以在其 ON DELETE 和 ON UPDATE 设置上将 userid 的设置更改为 null 吗?
    • @JacksonEtherchainStake 我不明白这个问题。您可以使您的外键可以为空,但您需要为其提供一个值。如果您不想在 INSERTS 中提供值,请不要将其包含在 INSERT 语句中(并确保该列可以为 NULL)
    猜你喜欢
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多