• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null and Blank in Database

 
Rashmi Trivedi
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends
I have one confusion about Null and Blank. According to my experience, null fields in Database are not welcomed. Is that correct? I experienced that if reference that null fields in you sql query from servlet, sql query get confused and may not return any thing.
Some of my friends told me that fields can be blank in Database. Blank is 0 length string. Is that true?
Rashmi
 
Sreenivasa Majji
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Null and blank are two different things. I have no idea what kinds of problems you are facing regarding nulls in servlets. Some sample code may help on this.
Null is not 0 length string.
String str1 = "";
String str2 = null;
Here str1 is a string with zero length, where as str2 is null.
Originally posted by Rashmi Trivedi:
Dear Friends
I have one confusion about Null and Blank. According to my experience, null fields in Database are not welcomed. Is that correct? I experienced that if reference that null fields in you sql query from servlet, sql query get confused and may not return any thing.
Some of my friends told me that fields can be blank in Database. Blank is 0 length string. Is that true?
Rashmi


------------------
Sreenivasa Kumar Majji
Sun Certified Java Programmer
SCJP Mock Test
 
Rashmi Trivedi
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sreenivasa
Thank you for your reply. The problem i am having is this:
I have some fields on HTML page which are optional. If there is no input from user than i don't bother logging that information into database and if user does than i do log that into database.
I have if statement inside my servlet to check user input:
if(fName !=null) {
//execute insert query
}// end if
This statement doesn't work the way it suppose to. It doesn't matter if user have any input or not it execute insert query.
I also tried following code but same result:
if(fName !="") {
//execute insert query
}// end if
Hope this make sense...
Regards
Rashmi
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rashmi,
you've got two seperate things here - nulls/blanks coming from the http request and nulls/blanks coming from the database. it's still difficult to tell what exactly you're problem is.
note well though: null means "no value", but "" (zero length string) is a value.
a date can also be null - have no value - but it can't be zero length (can it?)
 
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rashmi,
The problem you have defined is still not clear but I think you are looking to avoid executing a query if the data passed / received is null or blank.
You can check something like

Cheers,
Vijay
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear rashmi
u can try this too
if(fname.equals(""))
{
//execute query
}
good luck
malhar
Originally posted by Rashmi Trivedi:

if(fName !=null) {
//execute insert query
}// end if
This statement doesn't work the way it suppose to. It doesn't matter if user have any input or not it execute insert query.
I also tried following code but same result:
if(fName !="") {
//execute insert query
}// end if
Hope this make sense...
Regards
Rashmi

 
Rashmi Trivedi
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends
Thank you for input. I really appreciate it. I will try all the suggestion that i got from you and get back to you if it works. I have another problem which i need to fix first.
Once again thank you.
Regards,
Rashmi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic