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
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
... 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
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.
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...