3

I want to get the selected value from JComboBox, search it in database and update the quantity in database with the value in JTextField. Here is the code :

 Object selected = jComboBox1.getSelectedItem(); String album = (String)selected; int qty=Integer.parseInt(jTextField7.getText()); String query2="update productlist set QtyAvail=? " + "where Album=?"; try { PreparedStatement ps2=con.prepareStatement(query2); ps2.setInt(1, qty); ps2.setString(2,album); int res1=ps2.executeUpdate(); } catch(Exception e) { e.printStackTrace(); } 

I'm getting this error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 1" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at AddProductPanel.jButton2ActionPerformed(AddProductPanel.java:341) at AddProductPanel.access$4(AddProductPanel.java:335) at AddProductPanel$5.actionPerformed(AddProductPanel.java:133) 

I entered value '1' in the text field.

4 Answers 4

5

There is a white space in the the string input to parseInt() , use trim()

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

Comments

4

I want to get the selected value from JComboBox, search it in database and update the quantity in database with the value in JTextField.

  • use JFormattedTextField with Number Formatter

  • or maybe better will be usage of JSpinner with SpinnerNumberModel

  • avoids to parse between data types, you can control over the inputted value (min - max - ev. the range - decimal form)

1 Comment

"or maybe better will be usage of JSpinner with SpinnerNumberModel" I was waiting for someone to suggest that (best IMO) alternative to the general problem. :)
2

Your string contains a white space.

Use trim before parse :

int qty=Integer.parseInt(jTextField7.getText().trim()); 

Comments

1

@Nivedita Gautam : It seems that a blank space is appended before 1. Try to use the following statement:

int qty = Integer.parseInt(jTextField7.getText().trim()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.