准备状态中的通配符未执行

时间:2015-05-30 18:41:59

标签: java mysql prepared-statement jdbc-odbc

我正在尝试在我的preparedstatements中执行通配符,但是在运行它时,即使在使用适当的方法之后,返回结果集中的空结果,只有当我搜索特定关键字时,语句才会执行。

我的html文件:

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

}}

Jdbc-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”等通配符时,它会返回空结果。 是否存在通配符或其他语法的问题?

2 个答案:

答案 0 :(得分:0)

您可能需要将通配符放在单引号内。

答案 1 :(得分:0)

让它解决了。 我使用以下语法:

        String query = "select * from Postjob where jtit like(?)"; 
        PreparedStatement PrepStmt = con.prepareStatement(query);
        PrepStmt.setString(1, "%r%");