我在嘗試從我的數據庫連接和檢索數據時遇到此類錯誤。executeQuery()方法不能在PreparedStatement或CallableStatement上引用參數。錯誤
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);
}
}
刪除'executeUpdate()'調用。它會產生一個錯誤,因爲'PreparedStatement'不包含SQL更新語句。 – EJP