简体   繁体   中英

Unable to insert record into mysql database

Hi I am trying to insert data into mysql database upon a click of a button which name is 'request' button. However I can't do that and an error keeps popping up. You can scroll below for both the code and the error and there is a picture below showing the columns in my database.

Code here:

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);
        }

    }  

Error:

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)

Image on mysql database: (The records are already pre-added beforehand, only problem here is when I try to insert a new record, an error occurs and it does not get sent to the database. Another thing to take note of is that userid is a foreign key.)

在此处输入图像描述

i think the column userid is required or not_null or something similar that requires you to always have default value. The solution for this is either modify the table to include default value(or make the column nullable ) or pass data into your insert statement.

You're NOT providing any value for userid field. Change your query to something like this and provide a value for userid column.

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

If userid needs to be NULL in this case and that's why it's not included in your query, then you need to change your table to allow NULL in userid column so that your original query can work as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM