Results 1 to 5 of 5
  1. #1
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Can someone help me with an 'if' statement?

    I got a code that inserts value from jTextField into my database:
    Java Code:
    try {
              String sql = "INSERT into Sablonas (sablono_pav) values (?)";
              pst = conn.prepareStatement(sql); 
              pst.setString(1, treciaspanelsablonopavTextField.getText());
              pst.execute();
              treciaspanelinfoLabel.setText("Pranesimas sėkmingai išsaugotas");
            } catch (Exception e) {
              JOptionPane.showMessageDialog(null, e);  
            }
    Can someone help me write an IF statement to check if that field is empty?

    Something like this
    Java Code:
    if ( treciaspanelsablonopavTextField != "") {
    try {
              String sql = "INSERT into Sablonas (sablono_pav) values (?)";
              pst = conn.prepareStatement(sql); 
              pst.setString(1, treciaspanelsablonopavTextField.getText());
              pst.execute();
              treciaspanelinfoLabel.setText("Pranesimas sėkmingai išsaugotas");
            } catch (Exception e) {
              JOptionPane.showMessageDialog(null, e);  
            } 
    } else {
    
    }
    Also, if i have multiple fields, can i write something like this?
    Java Code:
    if (jTextField1 || jTextfield2 ||jTextField3 != "" {
    
    } else {
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Can someone help me with an 'if' statement?

    String has an isEmpty method.
    So just check the String returned by getText().

    And no, you have to check each one, so:
    Java Code:
    if (jTextField.getText().isEmpty() || jTextField2.getText().isEmpty()) etc etc
    Please do not ask for code as refusal often offends.

  3. #3
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Can someone help me with an 'if' statement?

    Thank you.
    Also maybe you can help me with this - what if i have a field that only numbers can be written in it? is there something similar like isEmpty? like isNumeric or isNotNumeric?
    Last edited by iNko; 12-07-2012 at 03:35 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Can someone help me with an 'if' statement?

    You can construct a java.utl.Scanner with the text String and query its hasNextXxx() methods. You can also use a JFormattedTextField or write a DocumentFilter for a JTextField's Document to ensure that only digits can be entered (and maybe a single decimal point, if you're dealing with floating point numeric types).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Can someone help me with an 'if' statement?

    Quote Originally Posted by DarrylBurke View Post
    You can construct a java.utl.Scanner with the text String and query its hasNextXxx() methods. You can also use a JFormattedTextField or write a DocumentFilter for a JTextField's Document to ensure that only digits can be entered (and maybe a single decimal point, if you're dealing with floating point numeric types).

    db
    Thanks, ill look into this

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  2. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  3. Mod and If Statement
    By Valerie1067 in forum New To Java
    Replies: 8
    Last Post: 03-25-2012, 08:00 AM
  4. If statement, please help??
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 11-23-2008, 02:58 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •