java – 使用JTextField – 无法将值转换为int
发布时间:2020-12-15 05:02:54 所属栏目:Java 来源:网络整理
导读:我想从JComboBox中获取所选值,在数据库中搜索它并使用JTextField中的值更新数据库中的数量.这是代码: Object selected = jComboBox1.getSelectedItem(); String album = (String)selected; int qty=Integer.parseInt(jTextField7.getText()); String query2
我想从JComboBox中获取所选值,在数据库中搜索它并使用JTextField中的值更新数据库中的数量.这是代码:
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(); } 我收到这个错误: 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) 我在文本字段中输入值“1”. 解决方法
在输入到parseInt()的字符串中有一个空格,使用trim()
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |