Results 1 to 5 of 5
- 12-07-2012, 02:31 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 55
- Rep Power
- 0
Can someone help me with an 'if' statement?
I got a code that inserts value from jTextField into my database:
Can someone help me write an IF statement to check if that field is empty?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); }
Something like this
Also, if i have multiple fields, can i write 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 { }
Java Code:if (jTextField1 || jTextfield2 ||jTextField3 != "" { } else { }
- 12-07-2012, 03:13 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 12-07-2012, 03:33 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 55
- Rep Power
- 0
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.
- 12-07-2012, 06:40 PM #4
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).
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-07-2012, 06:45 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 55
- Rep Power
- 0
Similar Threads
-
I want to convert this If-Else Statement program to Switch Statement
By UmairBaloch in forum New To JavaReplies: 3Last Post: 11-19-2012, 08:12 PM -
the switch statement and unreachable statement error
By name in forum New To JavaReplies: 2Last Post: 03-26-2012, 04:27 PM -
Mod and If Statement
By Valerie1067 in forum New To JavaReplies: 8Last Post: 03-25-2012, 08:00 AM -
If statement, please help??
By soc86 in forum New To JavaReplies: 5Last Post: 11-23-2008, 02:58 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks