1
votes

Need help with this update query on JAVA, just started learning this but having problems

Getting following error upon execution

[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'B.AWHERE ID =4'.

and data is not updating in MS Access database file

public void update(Student s)
    {
        int w = Integer.parseInt(s.getID());

        String query = "UPDATE Student SET ID =" + w + "," + "FirstName =" + s.getFirstName() + "," + "LastName =" + s.getLastName() + "," + "Address =" + s.getAddress() + "," + "Gender =" + s.getGender() + "," + "DOB =" + s.getDOB() + "," + "Degree =" + s.getDegree() + "WHERE ID =" + w;

        try
        {
            stmt.executeUpdate(query);
        }
        catch(SQLException e)
        {
            System.out.println("Problem in Query");
            e.printStackTrace();
        }
    }
2

2 Answers

0
votes

Change your UPDATE statement to be like below

String query = "UPDATE Student SET "FirstName = '" + s.getFirstName() + 
"'," + "LastName = '" + s.getLastName() + 
"'," + "Address = '" + s.getAddress() + 
"'," + "Gender = '" + s.getGender() + 
"'," + "DOB = '" + s.getDOB() + 
"'," + "Degree = '" + s.getDegree() + 
"' WHERE ID = " + w;

But your query won't make much sense cause, you are setting ID = w and also checking WHERE ID = w

0
votes

You are missing spaces and apostrophes for string values in your whole query E.g.

 "Degree =" + s.getDegree() + "WHERE ID ="

is being evaluated to "Degree =BAWHERE ID ="

which is invalid syntax.

EDIT: I can only guess that your Student object returns strings in the getter methods so try this.

String query = "UPDATE Student SET ID =" + w + ","
    + "FirstName =\"" + s.getFirstName() + "\","
    + "LastName =\"" + s.getLastName() + "\","
    + "Address =\"" + s.getAddress() + "\","
    + "Gender =\"" + s.getGender() + "\","
    + "DOB =\"" + s.getDOB() + "\","
    + "Degree =\"" + s.getDegree() + "\" "
    + "WHERE ID =" + w;