-1

I want to get the date in DATE column and want to compare with the current date. if the date is less than current date then I want to SET the value of PERMISSION column allow. But my Query doesn't execute.

Which I have tried is given in my below code.

Date date1 = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
//current date
Date date = new Date();
date1 = date;
CurrentDate.setText(date1.toString());
String UpdateQuery = null;
String Allow = "allow";
UpdateQuery = "UPDATE company SET permission = '"+Allow+"' WHERE date < ?";
pst = conn.prepareStatement(UpdateQuery);                 
SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd");
String addDate = CurrentDate.getText();
pst.setString(2, addDate);
pst.executeUpdate();
System.out.println("Updated");

I want to get a value of permission table to SET to "allow" when the update query is executing;

Backstreet Imrul
  • 102
  • 2
  • 15

1 Answers1

1

pst.setString(2, addDate);

I think this should be changed to:

pst.setString(1, addDate);

because you only have one parameter in your prepared statement.

Also, when comparing dates, you need to enclose them in single quotes, so changing your UpdateQuery string to
"UPDATE company SET permission = '"+Allow+"' WHERE date < '?' ";

is also a necessary step.

Nika Narushvili
  • 126
  • 1
  • 5
  • I changed it to 1... but didn't get any changes to result. – Backstreet Imrul Jan 18 '19 at 11:56
  • 1
    Try changing **UpdateQuery** to `"UPDATE company SET permission = '"+Allow+"' WHERE date < '?' ";` Databases generally need the date to be in single quotes, or else it will be evaluated as a math expression, i.e. 2000-05-20 without single quotes is just 1975. – Nika Narushvili Jan 18 '19 at 12:00
  • Then it could be some other database connectivity issue, are other queries from your app executed correctly? – Nika Narushvili Jan 18 '19 at 12:11
  • Bro... I just find out and fixed this issue.... yes bro.... My date format in java program was incompatible with xampp date format – Backstreet Imrul Jan 18 '19 at 12:31