简体   繁体   English

Java MYSQL准备语句错误:检查语法以在'?'附近使用 在第1行

[英]Java MYSQL Prepared Statement Error: Check syntax to use near '?' at line 1

I am trying to use the java mysql library but I am having issues using a prepared statement. 我正在尝试使用Java mysql库,但是在使用准备好的语句时遇到了问题。 I am not sure what I am missing. 我不确定我缺少什么。 Below is what I have with the MYSQL error attempting to use the prepared statement. 以下是尝试使用预处理语句时遇到的MYSQL错误。

String query = "SELECT id, clicks FROM mailer.links WHERE campaign_id=?";
    try {

        preparedStatement = connect.prepareStatement(query);
        preparedStatement.setInt(1, campaignId);
        preparedStatement.execute();
        Statement st = connect.createStatement();


        // execute the query, and get a java resultset
        ResultSet rs = st.executeQuery(query);

I am getting the following 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 '?' at line 1

It works if I do "campaign_id=" + campaignId , but is a SQL injection waiting to happen. 如果我执行“ campaign_id =” + campaignId,它会起作用,但是正在等待SQL注入。

尝试这个

ResultSet rs = preparedStatement.executeQuery();

PreparedStatement method executeQuery() itself returns a ResultSet object PreparedStatement方法executeQuery()本身返回ResultSet对象

So assign this to a ResultSet object 因此,将其分配给ResultSet对象

ResultSet rs = preparedStatement.executeQuery();

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 '?' at line 1

And this error happens because When 发生此错误是因为

ResultSet rs = st.executeQuery(query);

This statement executes it can't find any value in ? 该语句执行后在中找不到任何值? operator. 运营商。 so your query remains this "SELECT id, clicks FROM mailer.links WHERE campaign_id=?"; 因此您的查询仍然是"SELECT id, clicks FROM mailer.links WHERE campaign_id=?"; and this throws a MySQL Syntax Exception. 这会引发MySQL语法异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL Java 准备好的语句语法错误 - MySQL Java prepared statement Syntax error Java:在第1行的'?VALUES('23')'附近使用与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'?VALUES('23')'附近使用 - Java: Check the manual that corresponds to your MySQL server version for the right syntax to use near '?VALUES ('23')' at line 1 使用LIMIT命令和Java中的Prepared Statement进行MySQL语法错误 - MySQL syntax error using LIMIT command with Prepared Statement in Java 您的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在第1行使用“98”附近的正确语法 - 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 '98)' at line 1 SQL 语法错误; 检查与您的 MySQL 服务器版本相对应的手册,以了解在“? 或电话=? 在第 1 行 - error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? or tel=?' at line 1 您的SQL语法有错误; 检查与您的MySQL服务器版本相对应的手册,以在第1行的'null'附近使用正确的语法 - 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 'null' at line 1 jdbc4.MySQLSyntaxErrorException:SQL语法错误; 在第1行中,检查手动MySQL服务器版本以获取在'))'附近使用的正确语法 - jdbc4.MySQLSyntaxErrorException: error in your SQL syntax; check the manual MySQL server version for the right syntax to use near ') )' at line 1 MySql准备语句检查特定表JAVA - MySql Prepared Statement to check particular table JAVA Java Hibernate SQL错误:在第1行的'条件,描述,名称,图片,价格,状态,User_UserAddress_idUserAd'附近检查语法以使用正确的语法 - Java Hibernate SQL Error: check right syntax to use near 'condition, description, name, pictures, price, status, User_UserAddress_idUserAd' at line 1 多线程 - MySQL java连接器编写语句使用 - Multithreading - MySQL java connector prepared statement use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM