Convert Empty string to integer to insert into database

Asked

Viewed 1,315 times

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?

2 answers

1

You can use this syntax:

int numero = StringUtils.isNotBlank(minhaString) ? Integer.parseInt(minhaString) : 0;

Thus the parseInt is used only if the minhaString is not empty.


Theoretically, as stated in the comments, this syntax should work in Netbeans:

int numero = StringUtils.isNullOrEmpty(minhaString) ? 0 : Integer.parseInt(minhaString);

If you want null, use the wrapper Integer:

Integer numero = StringUtils.isNullOrEmpty(minhaString) ? null : Integer.parseInt(minhaString);


Also, as the comments, it may be necessary a if to differentiate the null zero when inserting into DB:

int numero;
boolean flagNulo;

if( isNullOrEmpty(minhaString) ) {
   numero = 0;
   flagNulo = true;
} else {
   numero = Integer.parseInt(minhaString);
   flagNulo = false;
}

And at the time of inserting in DB, test the flag.

  • I’m not getting :s anyway thanks for the help

  • @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.

  • Remember that int will not work with null, but Integer differential null from zero.

  • 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

  • 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.

0

Uses a if:

    int valor;
    if (JTextField.getText().equals("")){
        valor=0;
    }else {
       valor=Integer.parseInt(JTextField.getText());
       //aqui até devias de verificar se por acaso o texto não contem nenhum texto, 
       //porque se o utilizador se enganar será lançada uma excepção 
}
  • But in BD it’s different to be 0 or null. This value = 0 will put 0 in the BD and not the right null ?

  • and what is the logic of saving null and not 0? null may cause you problems

  • Let’s imagine that I have a field to enter a value that in the case has to be 0. If the field is not mandatory, if you do not fill it will enter the value 0 and not indicate that the field has not been filled in (null in BD )

  • and if you insert -1, for fields that have not been filled? I don’t know if it is possible, but I think it was always better than leaving completely empty

  • It will be a solution to send the -1, but for now what they ask me is to try not to enter other values, that is, if it is not filled in the comic book it has to appear null

Browser other questions tagged

You are not signed in. Login or sign up in order to post.