Search code examples
javadatabasexamppswingx

UpdateQuery doesn't execute. How to fix this code?


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;


Solution

  • 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.