I am trying to insert an entry into a database using a form created in java. I am able to select from the database without issue, but am having difficulty inserting to it. The issue, I believe, is that I am not converting the integers properly, or perhaps SQL does not take in an int into a Number field. I am not sure. I keep getting a “AWT-EventQueue-0” error, but it is not specifying where.
Here is a snippet of my code for converting my input text:
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("");
}
Also, here is the code I am using to then insert that data into my database:
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;
}
Like I said, I am trying to input integers into fields in my database that are of type Number. Is this doable, or am I mistaken?
Also of note, I have this working without issue on another table, but that table is only taking in strings.
Any help would be appreciated.
Thanks!
Edit:
Here is the error:
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)
Yes, you are doing it correctly; inserting the numeric type values without quotes. However, I believe your error is coming from the
Integer.parseInt()you are doing inaddItem(). Please check that all the strings you are trying to parse to ints are not faulty. You should also put theseInteger.parseInt()statements intry/catch(NumberFormatException)block as well to catch these conversion errors.Also, since you are doing the
try/catchfor the exceptions you are looking for, you don’t need to specify what the function throws in the function header. That is to tell the upper level calling functions “Hey, I might throw these exceptions, so you need to catch them.”