[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

Insert date into Oracle with PrepareStatement

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;
out.println("datestamp="+$datestamp);
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?

Thanks,
MD
0
mderbin
Asked:
mderbin
  • 2
1 Solution
 
objectsCommented:
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(year, month, day);
java.sql.Date date = new java.sql.Date(cal.getTime().getTime());
editStatement.setDate(17, date);
0
 
CodingExpertsCommented:
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.


CE
0
 
CodingExpertsCommented:
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;
  }
0
 
siliconeagleCommented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now