0

I'm trying to insert date format into Oracle from Java EE Eclipes. I'm able to insert a null value but not date value. The date prints out in the console okay but does not update the database. Here are my queries:

Inserting Date value: (NOT WORKING) Console output for uDate = 20/Sep/16

public void updateUnsubscribe(String empid) { 
  DateFormat df = new SimpleDateFormat("dd/MMM/yy");
  String uDate = df.format(new Date());
  String unSub = "udate MYDB set edate ='" + uDate + '" where emp = "empid"';
}

Inserting NULL value: (WORKING) Console output for uDate = null

public void updateSubscribe(String empid) { 
  Date toNull = null;
  String sub = "udate MYDB set edate ='" + toNull + '" where emp = "empid"';
}

I don't get any errors, at all, the only issue is the date field not inserting the date. What am I missing? Am I not formatting my date field correctly.

3
  • Your DateFormat is probably wrong (try d.d./MM/yy) Commented Sep 20, 2016 at 11:39
  • what's your date format in oracle? is it same as dd/MMM/yy? Commented Sep 20, 2016 at 11:40
  • output is like this 'Tue SEp 20 11:45:02 UTC 2016' Commented Sep 20, 2016 at 11:46

1 Answer 1

2

You could use oracle to_date function to convert varchar to date value, for your example:

public void updateUnsubscribe(String empid) { 
  DateFormat df = new SimpleDateFormat("dd/MM/yy");
  String uDate = df.format(new Date());
  String unSub = "udate MYDB set edate =to_date('" + uDate + ', 'dd/MM/yyyy')" where emp = "empid"';
}

But rather that, it is better to use JDBC PreparedStatement for this task:

public void updateUnsubscribe(String empid) { 
  PreparedStatement ps = conn.prepareStatement("update MYDB set edate=? where emp=?");
  ps.setTimestamp(1, new java.sql.Timestamp(new Date()));
  ps.setString(2, empid);
  ps.executeUpdate();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.