Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

   }
like image 972
Ching29 Avatar asked Oct 22 '12 05:10

Ching29


People also ask

What type of value is returned by the executeQuery () method?

executeQuery(): This method is used to execute statements that returns tabular data (example select). It returns an object of the class ResultSet.

What is executeQuery ()?

executeQuery() : This method is used to retrieve data from database using SELECT query. This method returns the ResultSet object that returns the data according to the query.

What is the return type of executeQuery () method void?

executeQuery. Executes the given SQL statement, which returns a single ResultSet object.

Can we use executeQuery for insert?

When executing select queries we should use executeQuery method so that if someone tries to execute insert/update statement it will throw java. sql. SQLException with message “executeQuery method can not be used for update”.


2 Answers

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

pstatement = connection.prepareStatement(queryString);

this would be the right way:

 pstatement = connection.prepareStatement(queryString);
 pstatement.setString(1, search);
 rs = pstatement.executeQuery();
like image 133
kirschmichel Avatar answered Sep 20 '22 11:09

kirschmichel


try

 rs = pstatement.executeQuery();

you have already specified query while creating preparedstatement in

 pstatement = connection.prepareStatement(queryString);
like image 35
banjara Avatar answered Sep 19 '22 11:09

banjara