executeQuery()方法不能在PreparedStatement或CallableStatement上获取参数。错误 [英] The method executeQuery() cannot take arguments on a PreparedStatement or CallableStatement. Error

查看:326
本文介绍了executeQuery()方法不能在PreparedStatement或CallableStatement上获取参数。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试连接和检索数据库中的数据时出现此类错误。

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

方法executeQuery()无法在PreparedStatement或CallableStatement上获取参数。

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

我的代码是这样的。

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);
    }

   }


推荐答案

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

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

pstatement = connection.prepareStatement(queryString);

这将是正确的方式:

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

这篇关于executeQuery()方法不能在PreparedStatement或CallableStatement上获取参数。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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