small SQL problem

Hello,

I have a small SQL problem...

I am designing an online bank using servlets for a university project and it allows customers to view their statements. They select which of their account numbers they want to view the statement for and the start and end date of the statement.

The problem lies with the dates. Here is the SQL:

SELECT date, details, amount, balance

FROM HISTORY

WHERE bankaccnumber=13494925 And date>=1/1/01 And date<=31/1/01;

All of the books I have looked at show dates in '1/1/01' format but whenever I try it this way I get a 'Data type mismatch in criteria expression' error (the 'date' field in the Database IS a Date type).

Although, whenever I run the query in Access and prompt the user to enter the start and end date, it works fine.

I have spoken to a few people and no-one seems to know why it is not working.

Any ideas?

Thanks

[951 byte] By [curraldo] at [2007-9-26 6:48:39]
# 1

If your database is MS Access and you don't expect to switch to something else, then write this:

SELECT date, details, amount, balance

FROM HISTORY

WHERE bankaccnumber=13494925 And [date]>=#1/1/01# And [date]<=#1/31/01#

Note that you MUST format your dates as MM/DD/YY and not as DD/MM/YY, that's an Access rule, and that you will probably have to "quote" your column name "date", which I think is a reserved word in SQL and hence a bad choice for column name.

Personally I always use PreparedStatements. That way my SQL would look like this:

SELECT date, details, amount, balance

FROM HISTORY

WHERE bankaccnumber=13494925 And date>=? And date<=?

and I would use the setDate() method to fill in the parameters. Since this method uses a Date as a parameter, I don't need to fight with date formats, the JDBC driver handles that for me.

DrClap at 2007-7-1 16:14:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...