我正在尝试使用在java中创建的表单将条目插入数据库。我可以毫无问题地从数据库中进行选择,但是我很难插入它。我认为,问题在于我没有正确地转换整数,或者SQL不会将int插入到Number字段中。我不确定。我一直收到“AWT-EventQueue-0”错误,但它没有指定位置。
以下是我的代码片段,用于转换输入文字:
public void addItem() throws ClassNotFoundException, SQLException{
Item i1;
DataAccess DA = new DataAccess();
this.txtOutput.setText(DA.testResult);
i1 = new Item();
i1.setItemId(Integer.parseInt(this.txtItemId.getText()));
i1.setCategoryId(Integer.parseInt(this.txtCategoryId.getText()));
i1.setItemName(this.txtItemName.getText());
i1.setItemDesc(this.txtItemDesc.getText());
i1.setUnitPrice(Integer.parseInt(this.txtUnitPrice.getText()));
i1.setUnitStock(Integer.parseInt(this.txtUnitStock.getText()));
try {
i1.saveItem();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.txtItemId.setText("");
this.txtCategoryId.setText("");
this.txtItemName.setText("");
this.txtItemDesc.setText("");
this.txtUnitPrice.setText("");
this.txtUnitStock.setText("");
}
此外,这是我用来将数据插入我的数据库的代码:
public int saveItem() throws ClassNotFoundException, SQLException{
int rowsAffected=0;
String strSQL = "INSERT INTO item VALUES (sc_item_item_id_seq.NEXTVAL," + this.getCategoryId() + ",'" + this.getItemName() + "','" + this.getItemDesc() + "'," + this.getUnitPrice() + "," + this.getUnitStock() + ")";
DataAccess DA = new DataAccess();
rowsAffected = DA.modifyDatabase(strSQL);
return rowsAffected;
}
就像我说的那样,我试图将整数输入到我的数据库中类型为Number的字段中。这是可行的,还是我弄错了?
另外值得注意的是,我在另一个表上没有问题,但该表只接受字符串。
任何帮助都将不胜感激。
谢谢!
编辑:
这是错误:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at javagui.views.frmItem.addItem(frmItem.java:243)
at javagui.views.frmItem$1.actionPerformed(frmItem.java:101)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:1)
是的,你正确地做到了;插入不带引号的数字类型值。但是,我认为您的错误来自于您Integer.parseInt()
中的addItem()
。请检查您尝试解析为int的所有字符串是否都没有错误。您还应该将这些Integer.parseInt()
语句放在try/catch(NumberFormatException)
块中以捕获这些转换错误。
此外,由于您正在查找要查找的异常的try/catch
,因此您无需指定函数在函数头中引发的内容。那就是告诉上层调用函数“嘿,我可能抛出这些异常,所以你需要抓住它们。”
答案 1 :(得分:0)
尝试阅读并理解堆栈跟踪。
它告诉您输入字符串NumberFormatException
的问题是""
:
java.lang.NumberFormatException: For input string: ""
,在致电Integer.parseInt
时发生:
at java.lang.Integer.parseInt(Unknown Source)
在您的班级addItem
的方法frmItem
中,位于frmItem.java文件的第243行:
at javagui.views.frmItem.addItem(frmItem.java:243)
因此它与数据库无关:您将空字符串传递给parseInt
。错误消息和堆栈跟踪非常有用:您必须阅读它们并尝试理解它们。