[Microsoft][ODBC Microsoft Access Driver] 查询表达式中的语法错误(缺少运算符)

[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression

需要关于 JAVA 的更新查询的帮助,刚开始学习这个但遇到问题

执行时出现以下错误

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

MS Access 数据库文件中的数据未更新

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();
        }
    }

您在整个查询中缺少字符串值的空格和撇号,例如

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

正在评估 "Degree =BAWHERE ID ="

语法无效。

编辑:我只能猜测 getter 方法中的 Student 对象 returns 字符串,所以试试这个。

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;

将您的 UPDATE 语句更改为如下所示

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;

但是您的查询没有多大意义,因为您正在设置 ID = w 并检查 WHERE ID = w