【发布时间】:2018-08-20 14:14:43
【问题描述】:
public void Update(String name, String mobile, String email, String car, String model, String year, String color, String plate_no, String plateletters,String gear, String km, String date,String licesnse,String status, String notes){
Connection conn = null;
try{
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
System.out.println("connected");
Statement stmt = (Statement) conn.createStatement();
String ins = "update car"
+ "set Name = '"+name+"' , "
+ "Email = '"+email+"' , "
+ "Car = '"+car+"' , "
+ "Model = '"+model+"' , "
+ "Year = '"+year+"' , "
+ "Color = '"+color+"' , "
+ "Plateno = '"+plate_no+"' , "
+ "Plateletters = '"+plateletters+"' , "
+ "Gearbox = '"+gear+"' , "
+ "Km = '"+km+"' , "
+ "Date = '"+date+"' , "
+ "License = '"+licesnse+"' , "
+ "Status = '"+status+"' , "
+ "Notes = '"+notes+"' "
+ "where Mobile = '"+mobile+"' ";
stmt.execute(ins);
}catch(SQLException e){
JOptionPane.showMessageDialog(null, e.getMessage());
System.err.println(e);
return;
}
}
**我已经尝试过准备好的声明和stmt.executeUpdate(ins);,但没有任何效果!!
请帮忙!**
错误:
java.sql.SQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 '= 'ahmed' 、 Email = 'ahmed@hotmail.com' 、 Car = 'honda' 、 Model = 'hondaz' 、 Yea' 在第 1 行附近使用正确的语法
【问题讨论】:
-
那么为什么这个问题上的PHP标签或者phpMyAdmin会来那个
-
标签垃圾邮件只会吸引反对票。您应该只标记使用的语言。
-
您错过了使用准备好的语句的要点。通过语句,您使用
?占位符作为列值,然后将Java 变量绑定到这些占位符。 JDBC trails on prepared statements 可能是你接下来应该去的地方,看看你哪里出错了。 -
你在
car和set之间缺少一个空格,所以实际语句是update carset Name,这是一个语法错误。但是请学习如何正确使用准备好的语句,因为您当前的代码是不安全的,因为它容易受到 SQL 注入的影响。