• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

can we set default values for variables??

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i have a HTML form which has some text fields in it..the user can enter some or all of the fields and submit it..depending on the values entered..a search has to be made from the database...i captured the non-entered values in another jsp page using
String name = getParameter("<field_name>")
i'm getting an empty string into name.
now need to use the variable name in the SQL statement in the JSP page as follows...
"SELECT * FROM <table_name> WHERE SHEET_NAME = '" +name +"' or '"+name+"' IS NULL AND so on...";
but as name is empty... the condition for checking null is failing...
can we set default vaues for the variables used so that when nothing is selected, the variable has a null value...
can someone help me solve the problem...plz treat this as urgent
thanks
--sumana
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok:
1o) Are you expecting an empty value in your name value?
String name = request.getParameter("name")
if you use this piece of code and your form with the name input field was empty, you should be getting null.
maybe I didn't understand your problem.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your jsp page use something like this
if(name.trim()=="") name=null;
similarly check for other parameters too.The fire the SQL query.
 
sumana ar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i'm getting an empty string bcz when i use the form action as get..the url is appended something like this...
URL?name=&id=20

so now do u think i'm getting a null into the name variable..
--sumana
 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sumana,
I believe what may be happening is that in your HTML form you have something like:
<input type="text" name="name" value="">
Therefore what will happen is when you submit the form the value for name will be set to an emptry String as specified by the form. What you can try is to remove the value="" from the form and then if nothing is entered name will be NULL.
The query will still not work as in your SQL you will end uo with:
OR NULL IS NULL and I am not sure if that is valid to do.
hth
 
Idly Vada
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sumana ar:
i think i'm getting an empty string bcz when i use the form action as get..the url is appended something like this...
URL?name=&id=20
so now do u think i'm getting a null into the name variable..


Even if you use POST also same thing will happen. I think therez no way send null value from form to the jsp. Even if you send NULL, it will be sent as the string null rather than null value.
 
sumana ar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for all those sugessions...
actually i havn't given any value attribute to the text field and so an empty string is passed when nothing is selected.
the problem is solved and anyways thanks for the help
sumana
 
Attractive, successful people love this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic