Lew said:
That is *the* story.
And the OP said he needed to determine if a date was from a given
year, not that he needed to format it in ISO format.
Read it again. There were two questions. First the one you said,
the second one how to get dates in ISO-formats:
| so how can i convert 2008 to this format
|
| sDateFrom = "2008-01-01";
| sDateTo = "2008-12-31";
I saw nothing in
his post that mandated ISO format as such; in fact, the reference to
using Strings was his focus on a suboptimal implementation of an
overarching goal to extract only records from a particular year.
I was focused on the second answer, because even with Prepared-
Statements you need Calendar and java.sql.Date.
given that the OP said:
i [sic] need search from the database the results which are in the year
2008.
Without knowing the database, you can't assume that there is a
"EXTRACT( YEAR FROM dcol ) = 2008" possible and if you want
Other than the fact that it *is* the SQL standard.
So? Depite the fact, that you have to assume that above statement
might not work with all databases (I don't take my time to prove
that by trying them all out right now), there is another difference
between the DB-independent solution and yours:
----- snip
explain select * from dw_log where EXTRACT( YEAR FROM date_as_ts ) = 2009
table: dw_log
type: ALL
possible_keys: null
key: null
key-len: null
ref: null
rows: 7911
Extra: Using where
----- snip
explain select * from dw_log where date_as_ts >= '2009-01-01' and date_as_ts <= '2009_12-31' limit 10
table: dw_log
type: range
possible_keys: dateindex_1
key: dateindex_1
key-len: 9
ref: null
rows: 1
Extra: Using where
----- snip
See the difference? At least this is the case for MySQL. Maybe
PostgreSQL behaves differently (they know interesting stuff
like Conditional Indizes, so I can't say), but tables with more
than - let's say - 10,000,000 entries and you will run into problems
with extract(year).
So looking at your suggestions how to solve that, I have to assume
that the tables of your applications never grow that big to let
you see the impact on performance with these kind of statements.
One can, of course, know which database they are using and just use
the syntax for that RDBMS. It is very rare that an application needs
to support more than one DMBS.
It's rare in your area but don't talk for others here, please.
It is rare to need to be database independent.
No. As soon as you had a migration from one database to another
once, you will see that database-independent programming has
its benefits, even if the conditions were clear at the beginning
of the project.
Changing the database is quite common, it happened just last month
with one of our customers (from MaxDB to Oracle).
The latter being an antipattern. One should always use
PreparedStatements.
Sure, but again, there are moments, where a PreparedStatement
is not needed. Without knowing the specific parameters, you
can't deny the usage.
Not at all. How do you create a Parameter for a PreparedStatement
that is used for a column of type date(time) with a specific date?
java.sql.Date.valueOf? Always? Goofy.
And that matters because ...?
It's still goofy, due to the conversion to a 'String'.
If you want a String in ISO-format you have to convert it to a String.
java.sql.Date.toString() is the most uncomplicated way without
the need of using SimpleDateFormat etc.
Sure. I'm actually doing this, e.g. to be able to return null
when trying to read in datetimes from MySQL in combination with
some versions of the JDBC-driver. Instead of returning null
itself, it tries to parse 0000-00-00 00:00:00.0000 as timestamp
leading to an exception.
It's ugly, ugly, ugly and should be avoided at all costs.
On every database-related project I have ever worked on in ten years
of professional Java programming for many clients,
I don't want to question your professionalism (how could I, I don't
know you), but: A man smoking for 20 years is not a professional
smoker, but an idiot with 20 years of experience. In other words:
I think that people using the time they already work with something
are simply running out of arguments.
I have *always*
known what database would be deployed.
So you're lucky and admit that you don't have *any* experience with
the development of database-independent applications. I do, so you
might just believe some of the points above. The most annoing thing
that can happen is a database-migration where you have to rewrite
more or less every statement you use, starting with easy things
like the replacement of "!=" with "<>" and really hard stuff like
recreating the logic behind algorithms that create SQL-statements
dynamically.
Hardly an egregious condition.
It is, you're just lucky.
Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!