我试图让通配符在我的preparedstatements
中执行,但在运行时,即使使用适当的方法后,也会在结果集中返回空结果,只有当我搜索特定的关键字语句执行时。
<form action="Searchjob" method="get">
Enter desired job title <input type="text" name="find">
<input type="submit" name="submit2" value="submit">
</form>
Servlet文件: @WebServlet("/Searchjob")
public class Searchjob extends HttpServlet {
@SuppressWarnings("static-access")
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String r=(String)request.getParameter("find");
List list3=null;
Searchjob2 t2=new Searchjob2();
try {
list3=t2.Seek(r);
} catch (ClassNotFoundException ex) {
}
if(list3==null){ out.print(" <center><h1>No jobs found</center></h1>");}
else{
ArrayList<String> findr=new ArrayList<String>(list3);
Iterator itt=findr.iterator();
out.print("<html><body><center><h1>Jobs available</h1></center>");
out.println("<center><table><tr><th>Job id</th><th>Jod title</th><th>Address</th><th>Branch</th><th>Technology</th><th>Description</th><th>Experience</th><th>Salary</th></tr>");
while(itt.hasNext())
{
Ajob item=(Ajob)itt.next();
out.print("<tr><th>");
out.print(" "+item.geta()+" ");
out.print("</th>");
out.print("<th>");
out.print(" "+item.getjtit()+" ");
out.print("</th>");
out.print("<th>");
out.print(" "+item.getc()+" ");
out.print("</th>");
out.print("<th>");
out.print(" "+item.getd()+" ");
out.print("</th>");
out.print("<th>");
out.print(" "+item.gete()+" ");
out.print("</th>");
out.print("<th>");
out.print(" "+item.getf()+" ");
out.print("</th>");
out.print("<th>");
out.print(" "+item.getg()+" ");
out.print("</th>");
out.print("<th>");
out.print(" "+item.geth()+" ");
out.print("</th></tr>");
}out.print("</center></table>");
}
}}
- odbc文件:
@WebServlet("/Searchjob2")
public class Searchjob2 {
public List Seek(String r) throws ClassNotFoundException{
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system", "system");
String forSql = "%" + r + "%";
String query = "select * from Postjob where jtit like ?";
PreparedStatement PrepStmt = con.prepareStatement(query);
PrepStmt.setString(1, forSql);
ResultSet rs = PrepStmt.executeQuery();
List seekr=new ArrayList();
while(rs.next())
{
String a=rs.getString("jid");
String b=rs.getString("jtit");
String c=rs.getString("addr");
String d=rs.getString("branch");
String e=rs.getString("tech");
String f=rs.getString("descp");
String g=rs.getString("exp");
String h=rs.getString("sal");
seekr.add(new Ajob(a,b,c,d,e,f,g,h));
}
if(seekr.isEmpty()){
return null;
}else{
return seekr;
}
}
catch(SQLException e){
}
return null;
}
}
}
在服务器上运行时,如果我在文本框中输入说'manager'关键字,服务器返回结果,但在输入通配符如'themanager'或'managerguru'时,它返回空结果。通配符的语法有问题吗?
您可能需要将通配符放在单引号内
解决了,伙计们。我使用了以下语法:
String query = "select * from Postjob where jtit like(?)";
PreparedStatement PrepStmt = con.prepareStatement(query);
PrepStmt.setString(1, "%r%");