【问题标题】:JDBC Prepared Statement Syntax ErrorJDBC 准备语句语法错误
【发布时间】:2018-04-10 04:38:28
【问题描述】:

我正在尝试使用准备好的语句插入到车辆表中。 This is the table shown in PHPMyAdmin

这是我的java代码

 try {
        String sql = "INSERT INTO vehicle (vin, serial, make, model, year, reg.no., status) VALUES (?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement statement = con.prepareStatement(sql);
        statement.setString(1, vin);
        statement.setString(2, serial);
        statement.setString(3, make);
        statement.setString(4, model);
        statement.setInt(5, 10);
        statement.setInt(6, 17);
        statement.setString(7, status);
        System.out.println(statement.toString());

        int rowsInserted = statement.executeUpdate();
        if (rowsInserted > 0) {
            System.out.println("A new user was inserted successfully!");
        }
    } catch (Exception ex) {
        System.out.println(ex);
    }

这是由此产生的错误

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' status) VALUES ('dfg', 'dfgfg', 'fg', 'sd564', 10, 17, 'dsf')' at line 1

我一无所知。是否与我没有为表中的主键“id”列传递值有关?

【问题讨论】:

    标签: java mysql sql jdbc sql-insert


    【解决方案1】:

    reg.no. 不是有效的列名。如果你真的需要使用它,你应该引用它:

    INSERT INTO vehicle (vin, serial, make, model, year, `reg.no.`, status) 
    -- Here ---------------------------------------------^-------^
    VALUES (?, ?, ?, ?, ?, ?, ?)";
    

    【讨论】:

      猜你喜欢
      • 2013-12-06
      • 2014-06-18
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多