[英]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.您好我正在尝试通过单击名称为“请求”按钮的按钮将数据插入 mysql 数据库。 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.) mysql 数据库上的图像:(记录已经预先添加,这里唯一的问题是当我尝试插入新记录时,发生错误并且它没有被发送到数据库。另外需要注意的是 userid是外键。)
i think the column userid
is required
or not_null
or something similar that requires you to always have default value.我认为列
userid
是required
的或not_null
或类似的东西,要求您始终具有默认值。 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.解决方案是修改表以包含默认值(或使列可为
nullable
)或将数据传递到您的插入语句中。
You're NOT providing any value for userid
field.您没有为
userid
字段提供任何值。 Change your query to something like this and provide a value for userid
column.将您的查询更改为类似的内容,并为
userid
列提供一个值。
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.如果在这种情况下
userid
需要是NULL
并且这就是它不包含在查询中的原因,那么您需要更改表以允许userid
列中的NULL
以便您的原始查询可以按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.