Results 1 to 2 of 2
  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    178
    Rep Power
    1

    Default How to insert Date in DB

    Java Code:
    public class Date_DB {
    	
    	private String name;
    	private Date dateOfCommission;
    	private Date dateOfdecommission;
    	private Date dateOfGridAccess;
    	private Date dateOfGridOutlet;
    //Setters und Getters
    }

    Java Code:
    PreparedStatement ps = connection.prepareStatement("INSERT INTO USERS  values (?, ?, ?, ?, ? )");
    ps.setString(1, obj.getName());
    ps.setDate(2,  obj.getDateOfCommission());  ////Error (red cross)
    ps.setDate(3,  obj.getDateOfdecommission());  //Error (red cross)
    ps.setDate(4,  obj.getDateOfGridAccess());  //Error (red cross)
    ps.setDate(5,  obj.getDateOfGridOutlet());  //Error (red cross)
    As you see in the code, it displays error by .setDate() i have no idea why, but ifcast it Date like this:
    Java Code:
    ps.setDate(2, (Date) obj.getDateOfCommission());
    then every thing is ok but at run the code, i get error
    XML Code:
    java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
    Any Ideas?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How to insert Date in DB

    Have you read the API for PreparedStatement? What are the Types of the parameters to its setDate(...) method?

    Then read the API for java.sql.Date and see what you need to construct one.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 11-30-2010, 10:51 PM
  2. insert date
    By swamy4311 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-02-2010, 09:18 AM
  3. How to insert DATE into mysql using JSP?
    By sivakumar_sakam in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-27-2009, 07:52 PM
  4. Problem when insert Date to database(SQL)
    By hungleon88 in forum JDBC
    Replies: 6
    Last Post: 04-29-2009, 03:53 PM
  5. Replies: 1
    Last Post: 10-10-2008, 08:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •