java 方法 executeQuery() 不能在 PreparedStatement 或 CallableStatement 上接受参数。错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13005460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:07:09  来源:igfitidea点击:

The method executeQuery() cannot take arguments on a PreparedStatement or CallableStatement. Error

javasql-serverjakarta-eetomcat7

提问by Ching29

I am having this kind of error when trying to connect and retrieve data from my database.

尝试从我的数据库连接和检索数据时遇到这种错误。

The method executeQuery() cannot take arguments on a PreparedStatement or CallableStatement.

方法 executeQuery() 不能在 PreparedStatement 或 CallableStatement 上接受参数。

My code goes like this.

我的代码是这样的。

String search = request.getParameter("searchstudent");
out.println(search);

String connectionURL = "jdbc:sqlserver://localhost:1433;databaseName=Chingdb;   integratedSecurity=true;";
 Connection connection = null;
 PreparedStatement pstatement = null;
 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
 ResultSet rs = null;
int updateQuery = 0;


if(request.getParameter("editstudent")!= null){ 
    try {           
     connection = DriverManager.getConnection(connectionURL, "root", "root");
     String queryString = "SELECT P_ID, lname, fname, mname FROM stu_info Where lname = ?";
     pstatement = connection.prepareStatement(queryString);
     pstatement.setString(1, search);
     rs = pstatement.executeQuery(queryString);
        updateQuery = pstatement.executeUpdate();
        %>
        <TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
        <%
        while (rs.next()) {
        %>
        <TR>
        <TD><%=rs.getInt(1)%></TD>
        <TD><%=rs.getString(2)%></TD>
        <TD><%=rs.getString(3)%></TD>
        <TD><%=rs.getString(4)%></TD>
        </TR></TABLE>
        <% 

        rs.close();
        pstatement.close();
        connection.close();

        }
        }

    catch(Exception e){
    out.println(e);
    }

   }

回答by kirschmichel

you don't need the queryString the second time, because you "told" the preparedStatement the String with this:

你第二次不需要 queryString,因为你用这个“告诉”preparedStatement 字符串:

pstatement = connection.prepareStatement(queryString);

this would be the right way:

这将是正确的方法:

 pstatement = connection.prepareStatement(queryString);
 pstatement.setString(1, search);
 rs = pstatement.executeQuery();

回答by banjara

try

尝试

 rs = pstatement.executeQuery();

you have already specified query while creating preparedstatement in

您在创建preparedstatement时已经指定了查询

 pstatement = connection.prepareStatement(queryString);

回答by divya

Just add { }for your querystring.i.e

只需{ }为您的 querystring.ie添加

String queryString = "{SELECT P_ID, lname, fname, mname FROM stu_info Where lname = ?}";

execute()and executeUpdate()will both work. executeQuery()only works when your procedure returns a result set.

execute()并且executeUpdate()都将起作用。executeQuery()仅当您的过程返回结果集时才有效。