• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to handle Null values?

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In HTML Form(Add data) in which I have text box,radio,checkbox, etc .And I am reading getting form data and putting it in mysql database using prepareStatement. and I have another form(view data) in which I am fetching data from database and displying it on view data form using Resultset.
I am having following problems
1) If any one of the form field are empty the getParameter() returns Null and it saves null into database but when I try to fetch data from database which is null , I am getting Null pointer exception.
How to handle this?
2)I have one Date field and another DateTime filed in mysql database. While storing data using preparedstatement is stores DateTime and DAte values but while retriving from date using Resultset which has only getDate() method it truncates the time part from datetime field.
I also like to know how to convert String to float and Long.
any help in these problem are greatly appreciated.
Thanks,
Padmashree
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Padma
For your first probelm with the null values from the database there are two solutions:
1 -- Instead of storing a null value in the database store some sort of defaul value like 0 or something else to indicate there is no data.
2 -- After you read in a field form the adatabase that might have been null use the wasNull( ) method of the ResultSet to see if the value you just read was null and handle it appropriately.
To convert a String to a Float of those wrapper classes ahve costructors that take a String and create a Float or a Long. they alos both have a valueOf method that takes a String and returns the correct object.
If this is so you can put them in the database then the valueOf method would be best so you dont create new object that you dont really need.
pStmt.setFloat(1, valueOf("stringFloat"));
keep in mind that the valueOf can throw a NumberFormatException that you'll have to catch.
For your date problem, the only thing I can think of off hand, is to just get it as a String and then just parse it yourself.
hope those help
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...
2)I have one Date field and another DateTime filed in mysql database. While storing data using preparedstatement is stores DateTime and DAte values but while retriving from date using Resultset which has only getDate() method it truncates the time part from datetime field.


this problem can be solved in 2 steps.
1. write the Date field to the database using the setDate() method, and write the DateTime field to the database using setTimestamp() method (other wise it won't write the time portion to the field and will default to 0:00:00...).
2. retrieve the Date field from the database using getDate() method and retrieve the DateTime field using the getTimestamp() method.
Jamie
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In reply to 1)
If at all possible don't stored nulls in the database at all, use empty strings i.e. ''
Set the properties of the string fields in the database to default to an empty string rather than NULL.
IMHO this makes grabbing data from input forms to the database a lot easier.
 
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had same problem as you and I validate my form in my HTML code and my Java program you have to stop user if the form is empty see my code for validate html form so in general:
1. use validate form in your HTML code
2. put if condition for chcking if variable is null then write a message for user
3.choose NOT NULL in your database for that field
I hope I was part of help...
 
padma patil
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I have updated all table fields and set default value to "". and It works .
Thanks a lot for such a valuable response.
-
padma
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic