【问题标题】:Getting MySQL syntax error after submitting the form提交表单后出现 MySQL 语法错误
【发布时间】:2016-11-23 10:00:38
【问题描述】:

我编写了一个简单的 java 代码来接受来自表单的参数并将其存储在一个表中。这是代码:

String fname = request.getParameter("username");
String mail = request.getParameter("email");
String country = request.getParameter("country");
String pword = request.getParameter("password");

Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/foodforthought", "root",
        "********");                                            
Statement statement = connection.createStatement();
try {
    int i = statement.executeUpdate("insert into users (username,email,country,password) values ("+fname+"','"+mail+"','"+country+"','"+pword+")");
    out.println("Successfully registered");
} catch (Exception e) {
    out.println(e);
    e.printStackTrace();
}  

Error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'India',')' at line 1  

country 的值为India,来自表单。我该如何解决?

【问题讨论】:

  • 您在第一个括号之后和values 部分的最后一个括号之前缺少引号。最好使用PreparedStatement 来避免这个问题和可能的注入。
  • '"+pword+" 你最后少了一个单引号。
  • int i = statement.executeUpdate("insert into users (username,email,country,password) values ('"+fname+"','"+mail+"','"+country+"','"+pword+"')");

标签: java mysql jdbc


【解决方案1】:

你的单引号是错误的。

但是永远不要使用从表单中读取的值插入到你的数据库中,你可能会遭受 SQL 注入

http://www.w3schools.com/Sql/sql_injection.asp

使用准备好的语句,将参数正确解析为特定类型

一个例子:

  String query = "insert into dept(deptnum, deptname, deptloc) values(?, ?, ?)";
  PreparedStatement pstmt = conn.prepareStatement(query); // create a statement
  pstmt.setInt(1, 1); // set input parameter 1
  pstmt.setString(2, "deptname"); // set input parameter 2
  pstmt.setString(3, "deptLocation"); // set input parameter 3
  pstmt.executeUpdate(); // execute insert statement

【讨论】:

    【解决方案2】:

    您忘记了查询中的' 字符:

    ("+fname+"','"+mail+"','"+country+"','"+pword+")
     ^ here                              and here ^
    

    改成('"+fname+"','"+mail+"','"+country+"','"+pword+"')

    或者最好使用PreparedStatement来避免这种错误和SQL注入问题。

    String sql = "insert into users (username, email, country, password) values (?, ?, ?, ?)";
    PreparedStatement preparedStatement = connection.prepareStatement(sql); 
    
    // Insert values safe and indirectly to avoid mistakes and SQL injection
    preparedStatement.setString(1, fname);
    preparedStatement.setString(2, mail);
    preparedStatement.setString(3, country);
    preparedStatement.setString(4, pword);
    
    // Perform the update
    int count = preparedStatement.executeUpdate();
    

    【讨论】:

    • ResultSet rs = preparedStatement.executeUpdate(); 行给出错误:Type mismatch: cannot convert from int to ResultSet
    • 对,我犯了一个错误。谢谢你:)
    • 现在,它抛出另一个错误:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'username' in 'field list'
    • 这意味着您的数据库中缺少username 列,或者SQL语句中的错误(这是没有人知道的)。请纠正它。此外,它与我的答案无关。
    猜你喜欢
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2014-02-25
    相关资源
    最近更新 更多