2017-10-28 171 views
2

我正尝试使用预准备语句插入到车辆表中。 This is the table shown in PHPMyAdminJDBC准备好的语句语法错误

这是我的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”列的值?

回答

0

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

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