wood burning stoves*
The moose likes JDBC and the fly likes Problem in using date in WHERE clause in SELECT query in Servlet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem in using date in WHERE clause in SELECT query in Servlet." Watch "Problem in using date in WHERE clause in SELECT query in Servlet." New topic
Author

Problem in using date in WHERE clause in SELECT query in Servlet.

Sarthak Gupta
Greenhorn

Joined: Mar 24, 2012
Posts: 6
Hello Friends!
I am new in this Forum and I have logged here for my problem.
I am developing a online system for applying for a post for my client.
But the problem is when I use 'date' in 'WHERE' clause in 'SELECT' query in servlet which is used to allow user to re-print the registration page, it is giving null pointer exception.
In this when I fill correct information it is giving null pointer exception otherwise result is 'records not exists'.
I am pasting all the codes, the print.java servlet and also print.html file for better help.

print.html


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
now print.java



in my table reg_no is bigint(40) and dob is of 'date' type. I am using mysql and xampp.
Please help me, you can check this code by implementing this code, and then date you have entered and regno in the db,use exactly that in print.html and run this code, it will give you error null pointer exception, and if you use only regno then it is running without error. and if you use regno with wrong date then again it is running without error and give 'no record find' I am not understanding what is the problem in 'date' whenever i am using right value of 'date' it is giving error.
Please help me.
Sarthak Gupta
Greenhorn

Joined: Mar 24, 2012
Posts: 6
what do you mean by that!
Neeraj Dhiman
Ranch Hand

Joined: Dec 19, 2011
Posts: 63

please use code tag for code.


Correct Me if i am wrong
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sarthak Gupta wrote:what do you mean by that!


That means: Just because your code happens to be in a servlet, that doesn't make it a servlet problem.

You've probably got one of the standard JDBC problems. Unfortunately your code isn't in the code tags, so it's very hard to read. There's also a lot of code there which doesn't even have anything to do with Java, which just makes it harder to locate your problem.

Anyway what you have to do is to look at the stack trace. It should tell you exactly what line of code threw the exception, and then you can start looking at that line to see what variable is unexpectedly null. You do have a stack trace, don't you?
Sarthak Gupta
Greenhorn

Joined: Mar 24, 2012
Posts: 6
I know Java has built-in stack trace but how to use that, I don't know.
Please guide me.
Sarthak Gupta
Greenhorn

Joined: Mar 24, 2012
Posts: 6
Neeraj Dhiman wrote:please use code tag for code.


Please guide me how to use code tag.
I'm new to this Forum.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Hi Sarthak, and welcome to the Ranch!

I've added the code tags to your post, so that you can see how does that look like. See UseCodeTags (<-- click) to learn how to use them on this site.

You're using e.printStackTrace() in your catch block, which is fine - that way the stack trace should be written to a log somewhere on your web server. I don't know xampp myself, so I cannot tell you where you'll find it, but you might try to find it out in xampp documentation. Find the stack trace in your logs and post it here.
Sarthak Gupta
Greenhorn

Joined: Mar 24, 2012
Posts: 6
Martin Vajsar wrote:Hi Sarthak, and welcome to the Ranch!

I've added the code tags to your post, so that you can see how does that look like. See UseCodeTags (<-- click) to learn how to use them on this site.

You're using e.printStackTrace() in your catch block, which is fine - that way the stack trace should be written to a log somewhere on your web server. I don't know xampp myself, so I cannot tell you where you'll find it, but you might try to find it out in xampp documentation. Find the stack trace in your logs and post it here.


Thank you for your support Martin.

the error is(tracked form printstacktrace)- java.lang.NullPointerException

Whenever I use the value which is stored in DB it is giving error otherwise it is display no record found.

I think I must post this to JDBC Forum.
Sarthak Gupta
Greenhorn

Joined: Mar 24, 2012
Posts: 6
Hey when I checked in JDBC Forum, my thread is already there, so now for all JDBC problem solvers-
please solve my also.

When ever I run the above code I got error(which I wrote just above this post), please help me
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Yes, this thread already is in the JDBC forum. One of the moderators here moved your post when he noticed it is indeed a JDBC related problem.

Did you actually find the stack trace in your logs? In this case, there will be not only the exception name (NullPointerException), but also the line number on which the exception occurred. This would help tremendously to track down the problem. It is much more probable that the "JDBC solvers" will help you if you provide this information (see also TellTheDetails).

In any case, take a closer look at this code fragment of yours:

There are several problems with it:

1) If any of these database fields is NULL, you'll get a NullPointerException, as you cannot use toString() with nulls.

2) This code loops over all records returned by the query, overwriting the values in your variables in each go without actually using them. Only the values from the last record are kept in the variables and used by the later code. This is suspicious and even if it was a desired functionality, it should be done differently.

3) Though you close the resources (rs, stmt, con), you don't do this in a finally block, therefore they don't get closed when an exception occurs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in using date in WHERE clause in SELECT query in Servlet.