2
How can I convert an empty text field (Jtextfield for example) to an integer, and then insert it into the database?
I have fields in the database defined as int, but when the user does not fill in a jTextField an empty String is not accepted in the database. How can I solve this problem ?
Using Netbeans 8.0.1
Updating:
Integer numOfContas = StringUtils.isNullOrEmpty(jFormattedNIFOfContas.getText().trim()) ? null : Integer.parseInt(jFormattedNIFOfContas.getText().trim());
System.out.println("Nif Of Contas: " + numOfContas);
The numOfContas variable already has null, but is interpreted as String because it gives the following error:
Nif Of Contas: null
468
Dados não inseridos!!
nov 17, 2014 11:31:24 AM faturacao.ConfEmpresa jButtonRegistarActionPerformed
SEVERE: null
java.sql.SQLException: Incorrect integer value: 'null' for column 'NIF_Of_Contas' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1618)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1549)
at faturacao.ConfEmpresa.jButtonRegistarActionPerformed(ConfEmpresa.java:3050)
at faturacao.ConfEmpresa.access$6300(ConfEmpresa.java:44)
at faturacao.ConfEmpresa$62.actionPerformed(ConfEmpresa.java:1749)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Any suggestions?
I’m not getting :s anyway thanks for the help
– Hugo Machado
@Hugomachado gave a nice update on the answer, a peek. And let us know where you are having trouble, that I try to improve the answer.
– Bacco
Remember that
int
will not work with null, butInteger
differential null from zero.– Bacco
I used the wrapper to give me null, and I made a print for the console and the actual value is null, anyway, gives me the error: Nif Of Accounts: null 455 Data not entered!! nov 17, 2014 10:11:32 AM billing.Confempresa jButtonRegistarActionPerformed SEVERE: null java.sql.Sqlexception: Incorrect integer value: 'null' for column 'Nif_of_contas' at Row 1
– Hugo Machado
So, in this case you use the third example I gave, and at INSERT time you use the flag to mount the query either with the numeric value, or with the word null even. If you can, add the Insert part at the end of the question (editing it). The impression is that either you are using the wrong bank interface, or the field is not supporting.
– Bacco