Insert date into Oracle with PrepareStatement

Posted on 2004-11-07
Last Modified: 2006-11-17
I am trying to get three variables from 3 drop down meus in a form.  The drop down menus are labled as day, month, and year.  Then I need to insert the daye into the date field in oracle of type date.

My code looks like this:

$MYSQL_edit = "UPDATE news SET news_header=?, subtitle=?, news_teaser=?, news_body=?, news_photo=?, photo_text=?, incld=?, incldhome=?, us=?, europe=?, canada=?, asia=?, intercont=?, location1=?, location2=?, location3=?, datestamp=? WHERE news_id=?";
String $datestamp = $year+"-"+$month+"-"+$day;
PreparedStatement editStatement = conn.prepareStatement($MYSQL_edit);
editStatement.setString(1, $news_header);
editStatement.setString(2, $subtitle);
editStatement.setString(3, $news_teaser);
editStatement.setString(4, $news_body);
editStatement.setString(5, $news_photo);
editStatement.setString(6, $photo_text);
editStatement.setInt(7, $incld_int);
editStatement.setInt(8, $incldhome_int);
editStatement.setInt(9, $us_int);
editStatement.setInt(10, $europe_int);
editStatement.setInt(11, $canada_int);
editStatement.setInt(12, $asia_int);
editStatement.setInt(13, $intercont_int);
editStatement.setInt(14, $location1_int);
editStatement.setInt(15, $location2_int);
editStatement.setInt(16, $location3_int);
editStatement.setString(17, $datestamp);
editStatement.setInt(18, $next_num);
int rs2 = editStatement.executeUpdate();

The error I am getting is this:

SQLException(news-edit): ORA-01861: literal does not match format string
Do I have to use a special command to insert the date type field?

Can someone please help me with my code or show me some code that will work?

Question by:mderbin
    LVL 92

    Accepted Solution

    Calendar cal = Calendar.getInstance();
    cal.set(year, month, day);
    java.sql.Date date = new java.sql.Date(cal.getTime().getTime());
    editStatement.setDate(17, date);
    LVL 6

    Expert Comment

    1) download servlet.jar
    2) go to file -> new->project
    3) click on java project in the widow that opens up and do next
    4) specify the project name and do next
    5) go to the libraries and select "ADD EXTERNAL JAR" and specify the location of servlet.jar and do finish
    6) NOw run your code.

    LVL 6

    Expert Comment

    sorry!!!! was meant to be posted for a different question .....
     you may use this function to format your date type. ..

     call using >> easyDateFormat("MM/dd/yy",datestamp);
      public String easyDateFormat (String format, Date dt)
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String datenewformat = formatter.format(dt);
        return  datenewformat;
    LVL 2

    Expert Comment

    editStatement.setDate(17, new java.sql.Date((new java.util.Date()).getTime()));

    you shoud also be aware that you dont need to prefix you variables with $ in java, since all variables are declared then the compiler knows variables explicitly and the use of the $ goes against establised java coding standards. Usually variables and methods are declared with a lowercase letterfirst and each word in the name starting with an uppercase method. e.g:-
    int thisIsMyVariable=0; // variable declaration
    public void thisIsMyMethod(String thisIsMyParameter) throws Exception{...} // method declaration.

    only class name start in uppercase e.g:-
    class ThisIsMyClass {}
    when it is used you use the lower case equivalent:-
    ThisIsMyClass thisIsMyClass = new ThisIsMyClass();

    but i guess you didn't aks this anyway :) , some people get huffy though as they are used to reading java code this way.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    In the modern office, employees tend to move around the workplace a lot more freely. Conferences, collaborative groups, flexible seating and working from home require a new level of mobility. Technology has not only changed the behavior and the expe…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now