当前位置: 首页 > 面试题库 >

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

暴奕
2023-03-14
问题内容

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

方法executeQuery()不能在PreparedStatement或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,因为您可以用以下方式“告诉” prepareStatement字符串:

pstatement = connection.prepareStatement(queryString);

这是正确的方法

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


 类似资料:
  • 问题内容: 我正在执行一个简单的preparestatement查询执行,并抛出此错误:java.sql.SQLException:net.sourceforge.jtds.jdbc.JtdsPreparedStatement.notSupported的这种类型的语句不支持使用executeQuery(string)方法(net.sourceforge.jtds.jdbc.JtdsPrepared

  • 问题内容: 我有一个需要从我的Java程序调用的功能。我曾经将参数传递给存储的proc。我正在使用oracle瘦驱动程序(在Web逻辑服务器中根据相关的jndi条目配置)。此存储的proc没有任何OUT值。此存储的proc接受一个数字值,并根据接收到的值在db中进行很多更新。 我得到一个连接对象,然后在循环中调用此存储的proc(20次传递20个数字)。当我直接从oracle客户端调用此存储的pr

  • 由于一些奇怪的原因,hibernate生成的查询不适用于postgres,它告诉它无法找到关系/表,即使在架构中有有效的表?这个答案没有帮助。 例外情况: HQL公司 生成SQL 此查询工作正常: 基于胶片实体注释的映射: 解决方案/黑客: 评论后,问题消失了。

  • 主要内容:1. Statement对象,2. PreparedStatement对象,3. CallableStatement对象,关闭CallableStatement对象当获得了与数据库的连接后,就可以与数据库进行交互了。 JDBC ,和接口定义了可用于发送SQL或PL/SQL命令,并从数据库接收数据的方法和属性。 它们还定义了有助于在Java和SQL数据类型的数据类型差异转换的方法。 下表提供了每个接口定义,以及使用这些接口的目的的总结。 接口 推荐使用 用于对数据库进行通用访问,在运行时

  • 我试图使用ResultSet(UCanAccess支持)在GUI中填充我的组合框 每次尝试执行时,我都会遇到以下错误: 我不知道这是什么原因。我想这是ucanaccess的问题吧?链接到access数据库以供参考:http://www53.zippyshare.com/v/DMLjdpDw/file.html

  • 问题内容: 我想知道有什么区别,何时使用,和。 每种方法的最佳实践和典型方案是什么? 问题答案: 声明与PreparedStatement 使用PreparedStatement可以提高性能,但它取决于数据库。 使用PreparedStatement可以避免SQL注入。PreparedStatement如何避免或阻止SQL注入? 使用setInt,setString和prepareStatemen