The executeQuery () method cannot accept arguments to PreparedStatement or CallableStatement. error

I have this error when trying to connect and get data from my database.

The executeQuery () method cannot accept arguments to PreparedStatement or CallableStatement.

My code is as follows.

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); } } 
+4
source share
3 answers

you don’t need a String request a second time because you said the prepared String String with this:

 pstatement = connection.prepareStatement(queryString); 

this will be the correct way:

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

to try

  rs = pstatement.executeQuery(); 

you already specified the request when creating readystatement in

  pstatement = connection.prepareStatement(queryString); 
+2
source

Just add { } for your querystring.ie

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

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

+1
source

Source: https://habr.com/ru/post/1441195/


All Articles