File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Date used as String datatype Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Date used as String datatype" Watch "Date used as String datatype" New topic
Author

Date used as String datatype

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
What problem i can face if i store date as STRING (in java & varchar2 in database)


kunal
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

I'd say:

  • You won't be able to perform date-time arithmetics easily.
  • You'll be able to store strings that do not represent a valid date (with dates, you'd get an exception instead, which would help you to pinpoint the origin of the error).
  • Performance will suffer in some databases, as cardinality estimates for query plan optimization will be much worse than with proper data types.
  • Unless you're really careful, you'll have implicit conversions in SQL, which are evil and can expose you to some arcane forms of SQL injection atacks.

  • Unless you never manipulate the dates in other ways than displaying them, I'd strongly discourage this. If you ever need to add/subtract time, convert a date to local timezone or even search a range of dates in the database, use proper data types.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    Thanks for your reply Martin Vajsar.

    What do you say about date as String datatype in java code and Date datatype in database?
    Martin Vajsar
    Sheriff

    Joined: Aug 22, 2010
    Posts: 3610
        
      60

    Slightly better then the previous option, in my opinion, but I still wouldn't do it.

    JDK unfortunately lacks good date/time support. I'd suggest to check the JodaTime library, it makes handling the dates in Java a much pleasant experience.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    Slightly better then the previous option, in my opinion, but I still wouldn't do it.


    What are your reasons? I am using it in a project, so, your reasons will help me to decide much better
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18570
        
        8

    Let's turn it around. What is your compelling reason to store a date as a String object in your code? You're the one who has to justify this. Nobody has to provide reasons for storing a date in a Date object.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    Paul, you got me.

    Frankly speaking i found it( store a date as a String object) simple and easy to use. But, i still wonder, what problems it can cause.


    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18570
        
        8

    It's a typical beginner idea to store everything as strings. Eventually they get to the chapter in their book which tells them how to design and create classes and objects, and then they don't do that any more.

    There's no point in going through the possible things which can go wrong if you don't use proper object-oriented design in an object-oriented language, at least not in my opinion.
    Martin Vajsar
    Sheriff

    Joined: Aug 22, 2010
    Posts: 3610
        
      60

    Kunal Lakhani wrote:
    Slightly better then the previous option, in my opinion, but I still wouldn't do it.

    What are your reasons? I am using it in a project, so, your reasons will help me to decide much better

    Why is it a bit better? Because the four points I listed earlier won't apply at the database side. So if you get an invalid date string and try to store it, it will fail and you won't get a nonsensical date value in the database. Big plus to me.

    Why only slightly better? Because most of these four points would still apply on the Java side.

    If you try JodaTime, you'll find you handling dates can be very easy and simple in Java. In my opinion, if your application is date-time intensive, it is a must. Heck, even if all you need is to add one day to a date in your application, my advice is: use JodaTime. But I promise I'll stop plugging it in this thread from now on.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    Thanks Paul Clapham and Martin Vajsar.

    Martin Vajsar, i will try my best to dive in JodaTime
     
    GeeCON Prague 2014
     
    subject: Date used as String datatype