在SQL语句结束时缺少分号(;)

问题描述 投票:-1回答:1

我试图通过将我的表中的值与我从ID号中捕获的有关用户的变量进行比较,将值插入数据库中另一个表的表中。

我收到以下错误:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Missing semicolon (;) at end of SQL statement.

以下是我用来尝试上述代码的代码:

    Statement stmt1 = con.createStatement();

    String mySqlStatement1 = "INSERT INTO Entrant_Group VALUES (Group_Description) WHERE Group_MinAge <= " + details.getAge() + "AND Group_MaxAge >= " + details.getAge() + "AND Group_Gender = 'details.getGender()'";
    stmt1.executeUpdate(mySqlStatement1);
java sql sql-server sqlexception
1个回答
3
投票

暂时搁置关于SQL注入攻击的标准警告,你的陈述的问题是你的引号不正确,并且在你的ANDs之前没有空格:

String mySqlStatement1 = "INSERT INTO Entrant_Group VALUES (Group_Description) WHERE Group_MinAge <= "
 +   details.getAge()
 +   " AND Group_MaxAge >= " // Add space
 +   details.getAge()
 +   " AND Group_Gender = '" // Add space
 +   details.getGender()     // This part was enclosed in double-quotes
 +   "';";                   // Add semicolon at the end

要防止注入攻击,您应该参数化查询,并将值绑定到预准备语句的参数:

String mySqlStatement1 = "INSERT INTO Entrant_Group VALUES (Group_Description) WHERE Group_MinAge <= ? AND Group_MaxAge >= ? AND Group_Gender = ?;";
© www.soinside.com 2019 - 2024. All rights reserved.