Java 向客户表插入数据时出错

Java 向客户表插入数据时出错,java,mysql,jdbc,sql-insert,Java,Mysql,Jdbc,Sql Insert,我正在开发一个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

我正在开发一个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'


谢谢大家!

@jacob,您不能在引号中直接传递日期对象。相反,您需要使用DateFormatter将其格式化为字符串类型。一旦您将日期转换为字符串类型,则可以将该值传递给列


一个建议:不要使用这个“,”,。。。查询..它可能面临SQL注入。相反,您使用参数化查询,这样您的查询就不会被更改。

是的,查询中缺少右括号

但将转义字符放在单引号(')中,若数据有单引号,则查询将给出错误

 "INSERT INTO customer(fname, lname, email, phone, country, dob, city, postal, address1, address2, password, regDate, status) "
        + "VALUES ('"+escape( customer.getFname() )+"', '"
                 +escape( customer.getLname() ) +"', '"
                 +escape( customer.getEmail() ) +"', '"
                 +escape( customer.getPhone() ) +"', '"
                 +escape( customer.getCountry() )+"',"
                     +"'"+customer.getDob() +"', '"
                     +escape( customer.getCity() ) +"', '"
                     +escape( customer.getPostal() ) +"', '"
                     +escape( customer.getAddress1() ) +"', '"
                     +escape( customer.getAddress2()) +"',"
                    + "'"+escape( customer.getPassword() )+"', NOW(), 'active' )";


public static String escape(String val) {
    return (val==null?null:val.replaceAll("'", "''"));
}

. 它容易出错,并且容易受到SQL注入的攻击。您应该使用支持单独参数的语句。前面的注释是指带有占位符的
PreparedStatement
(问号,
)。请参阅。将
日期
值作为字符串传递是非常糟糕的advice@a_horse_with_no_name,你可以投票支持我的评论!我说了同样的话:-)你只是建议:“你需要把它格式化成字符串类型”和“一旦你把日期转换成字符串类型,你就可以把这个值传递给列”。正确的方法是使用带参数的预处理语句,而不是自己尝试引用值。