MySql 使用 AND 或 OR 条件手动构建 SQL 查询时出现 syntaxErrorException

MySql syntaxErrorException when manually building SQL query with AND or OR conditions

我正在使用 JSP、Servlet 和 Mysql 开发一个简单的 Web 应用程序。我在 Mysql 数据库中有 table 调用的注释。我只想从 Mysql 获取基于 'OR' 和 'AND' 的记录。但不幸的是,我遇到了 jdbc-Mysql 语法异常。我正在编写 SQL 查询,如下所示:

dbconnection=new dbconnection();
            connection=dbconnection.setConnection();
            Transcript_ID=request.getParameter("Transcript_ID");
            Condition_1=request.getParameter("Condition_1"); // Can be "OR" or "AND".
            Gene_Symbol=request.getParameter("Gene_Symbol"); 
            Condition_2=request.getParameter("Condition_2"); // Can be "OR" or "AND".
            Molecular_Function=request.getParameter("Molecular_Function");
            Condition_3=request.getParameter("Condition_3"); // Can be "OR" or "AND".
            Biological_Process=request.getParameter("Biological_Process");
            stmt=connection.createStatement();
            query="select * from annotations where ID='"+Transcript_ID+"','"+Condition_1+"',Gene_Symbol='"+Gene_Symbol+"','"+Condition_2+"',"+ "Molecular_Function='"+Molecular_Function+"','"+Condition_3+"',Biological_Process='"+Biological_Process+"'";

我在控制台中得到的错误是

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 ''OR',Gene_Symbol='tmx3','OR',Molecular_Function='','OR',Biological_Process=''' at line 1

任何人都可以指导我 sql 语法中的错误所在。

您不是用 AND / OR 连接,而是用逗号代替。

请让我们看看sql语句生成代码。

不要在 sql 语句中添加“,”,删除逗号并添加 and/or 等条件,或者您可以采用 Gene_Symbol in('abc','cba') 像这样

从 where 条件中删除逗号

query="select * from annotations where ID='"+Transcript_ID+"' "+Condition_1+" Gene_Symbol='"+Gene_Symbol+"' "+Condition_2+" "+ "Molecular_Function='"+Molecular_Function+"' "+Condition_3+" Biological_Process='"+Biological_Process+"'";