【发布时间】:2019-04-25 12:27:34
【问题描述】:
我正在开发一个 Java Web Restful 应用程序,当我插入数据时,它会出现类似这样的错误..
"com.mysql.jdbc.exceptions.jdbc4.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 '' at line 1"
我不知道我的 SQL 语法有什么问题
我的 SQL 查询看起来像这样......
String sql = "INSERT INTO customer(fname, lname, email, phone, country, dob, city, postal, address1, address2, password, regDate, status) "
+ "VALUES ('"+customer.getFname()+"', '"+customer.getLname()+"', '"+customer.getEmail()+"', '"+customer.getPhone()+"', '"+customer.getCountry()+"',"
+ "'"+customer.getDob()+"', '"+customer.getCity()+"', '"+customer.getPostal()+"', '"+customer.getAddress1()+"', '"+customer.getAddress2()+"',"
+ "'"+customer.getPassword()+"', NOW(), 'active'";
除了 'dob(date)'、'regDate(date)' 之外,所有列类型都有 '(varchar)'..
谢谢!
【问题讨论】:
-
You should not try and glue values directly into your SQL。它容易出错并且容易受到 SQL 注入的影响。您应该使用支持单独参数的语句。
-
前面的评论指的是
PreparedStatement,带有占位符(问号,?)。见Oracle Tutorial。
标签: java mysql jdbc sql-insert