JDBC中的PreparedStatement:值插入

时间:2016-02-21 13:33:31

标签: java jdbc

我正在尝试使用JDBC编写我的简单宠物项目

这是我准备好的声明:

String query = "SELECT c.id as categoryId, c.`name`, p.id as productId, p.title, p.price, p.`status` FROM product p " +
                "JOIN product_categories pc ON p.id = pc.product_id " +
                "JOIN category c ON pc.category_id = c.id " +
                "JOIN shop s ON c.shop_id = s.id " +
                "WHERE s.`name` = ?";
        PreparedStatement preparedStatement = conn.prepareStatement(query);
        preparedStatement.setString(1, shop.getName());
        preparedStatement.executeQuery(query);

然而,我每次都会这样:

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

我做错了什么?

1 个答案:

答案 0 :(得分:4)

删除已在语句

中指定的查询字符串
preparedStatement.executeQuery();

Statement.executeQuery(query)仅适用于未执行参数替换的未准备Statements,产生显示的错误