0

I am trying to get wildcards to execute in my preparedstatements but on running it returns empty result in resultset even after using appropriate method, only if i search particular keyword the statement executes.

My html file:

          <form action="Searchjob" method="get">
          Enter desired job title <input type="text" name="find">
          <input type="submit" name="submit2" value="submit">
          </form>

Servlet file:

        @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 file:

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

   }
   }

On running on server, if i enter say 'manager' keyword in textbox ,the server returns a result, but on entering wildcards like 'themanager' or 'managerguru',it returns empty result. Is the problem with syntax for wildcards or something else?

Kaijju
  • 143
  • 10

2 Answers2

0

You may need to put the wildcard inside single quotes.

Kalyan Vedala
  • 1,049
  • 2
  • 17
  • 33
0

Got it solved guys. I used following syntax:

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