为什么我为预准备语句收到语法错误? [英] why do I get a syntax error for prepared statement?

查看:98
本文介绍了为什么我为预准备语句收到语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个准备好的语句,但是它给出了语法错误?我无法理解什么是错的。它应该传递电影名称并将结果作为该电影的导演

I have written a prepared statement but its giving a syntax error at ?. I am not able to understand whats wrong.It should pass a movie name and get the result as directors of that movie

stmt=getConnection().createStatement();
        String sql="SELECT directors FROM moviedata WHERE moviedata.title = ?";
        PreparedStatement preparedStatement=conn.prepareStatement(sql);
        preparedStatement.setString(1,movieName);
        rs=preparedStatement.executeQuery(sql);


推荐答案

问题在于:

rs=preparedStatement.executeQuery(sql);

你不应该传递SQL String executeQuery(),因为预准备语句已经包含带有的SQL String ? / code>占位符替换为 movieName 的值。

You shouldn't pass the SQL String to executeQuery(), since the prepared statement already contains the SQL String with the ? placeholder replaced by the value of movieName.

使用:

rs=preparedStatement.executeQuery();

这篇关于为什么我为预准备语句收到语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆