简体   繁体   English

用MySQL准备的语句?

[英]Prepared statements with MySQL?

I am having some trouble with, what I believe to by syntax, for prepared statements. 我对预备语句的语法感到有些麻烦,我认为这是语法问题。

I have the following code 我有以下代码

String query2="SELECT lname FROM school_student WHERE sid = ? ORDER BY sid;";

PreparedStatement ps = cn.prepareStatement(query2);
ps.setInt(1, 3);
ResultSet rs = ps.executeQuery(query2);

The problem I am having is that I am getting this error message: 我遇到的问题是我收到此错误消息:

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

However, when I substitute the "?" 但是,当我用“?”代替时 in my query for a 3, the query works fine with no error and gives me what I want. 在查询3的查询中,查询工作正常,没有错误,并给出了我想要的内容。 There seems to be something wrong with how I am setting the value of the "?" 我如何设置“?”的值似乎有问题。 in my query? 在我的查询? Am I using the wrong syntax? 我使用的语法错误吗?

Simply use 只需使用

ps.executeQuery();

(ie use the overloaded executeQuery() method which doesn't take any argument). (即,使用不带任何参数的重载executeQuery()方法)。 You already passed the query when preparing the statement. 在准备语句时,您已经通过了查询。

use this query :- 使用此查询:

String query2 = "SELECT lname FROM school_student WHERE sid = "+attribute+" ORDER BY sid;"; 字符串查询2 =“从school_student WHERE sid中选择SELECT名称=“ + attribute +” ORDER BY sid;“;

and simply use 并简单地使用

ps.executeQuery(); ps.executeQuery();

我认为在准备查询时,这是语法问题,请尝试使用该语法。...string query2 =“ SELECT lname FROM school_student WHERE sid = + variablename + ORDER BY sid;”

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM