0

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)
2
  • 1
    If an exception is thrown, show us its stack trace. And please use prepared statements: docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html Commented Dec 2, 2011 at 22:35
  • Do the ItemName or itemDesc fields contain unfriendly characters, such as quotes? Commented Dec 2, 2011 at 22:51

2 Answers 2

1

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 in addItem(). Please check that all the strings you are trying to parse to ints are not faulty. You should also put these Integer.parseInt() statements in try/catch(NumberFormatException) block as well to catch these conversion errors.

Also, since you are doing the try/catch for 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."

Sign up to request clarification or add additional context in comments.

3 Comments

+1! I completely forgot the exception. I delete my answer not to pollute yours :) Nice catch.
Thanks. I added the try/catch (NumberFormatException) and it seems to be catching the errors. From what I see, I don't think there are any faulty characters in the strings that are being converted. I'll keep trying. :)
I caught it. I was leaving the ItemId field blank, as I have a sequence that generates the item_id for me when inserting to the database.
0

Try reading and understanding the stack trace.

It tells you that the problem is a NumberFormatException for the input String "":

java.lang.NumberFormatException: For input string: ""

, happening when calling Integer.parseInt:

at java.lang.Integer.parseInt(Unknown Source)

in the method addItem of your class frmItem, at line 243 of the file frmItem.java:

at javagui.views.frmItem.addItem(frmItem.java:243)

It thus has nothing to do with the database: you're passing an empty String to parseInt. Error messages and stack traces are really useful: you must read them and try to understand them.

1 Comment

Thanks. I am still very new to Java. That's quite helpful. I'll look for that in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.