Search mails in database 
<p>I want to implement searching of mails in my mail system. I have used simple procedure to search the mails in the database. I want to know the error in the query, which is given below:</p>

<pre><code>import MyPackage.Message;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Blob;
import java.sql.Timestamp;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ROHIT
 */
@WebServlet(name="SearchServlet", urlPatterns={"/SearchServlet"})
public class SearchServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
         HttpSession session = request.getSession();
         String receiverAddress = (String) session.getAttribute("username");
         String searchQuery = request.getParameter("SearchMails");

         Connection con=null;
     PreparedStatement ptmt=null;
         PreparedStatement stmt = null;
         ResultSet rs= null;
         ResultSet rs1 = null;

         String driverclass = "oracle.jdbc.driver.OracleDriver";
         String dburl="jdbc:oracle:thin:@ROHIT-PC:1521:XE";
         String dbuser="system";
         String dbpassword="jaihanuman";
         ArrayList messageList = new ArrayList();
         Message message;
         String fileName = "", fileSize = "";

         System.out.print("Inside   ");
         out.print("Inside   ");
         try
         {
            Class.forName(driverclass);
            con=DriverManager.getConnection(dburl,dbuser,dbpassword);

            String sql="SELECT * FROM mail where receiveraddress = ? AND subject LIKE '%' ? '%' AND body LIKE '%?%' AND senderaddress LIKE '%?%' ";

                ptmt=con.prepareStatement(sql);

                ptmt.setString(1, receiverAddress);
                ptmt.setString(2, searchQuery);
                ptmt.setString(3,searchQuery);
                ptmt.setString(4,searchQuery);

                rs=ptmt.executeQuery();

                while(rs.next()){

                    message = new Message();

                    message.setMailId(rs.getString(1));
                    message.setSubject(rs.getString(2));
                    message.setBody(rs.getString(3));
                    message.setSenderAdress(rs.getString(4));
                    message.setReceiverAddress(rs.getString(5));
                    message.setDate(rs.getDate(6));
                    message.setTimestamp(rs.getTimestamp(7));
                    message.setAttachmentFlag(rs.getString(8));
                    message.setLabel(rs.getString(11));
                    if(rs.getString(8).equals("1")) {

                        String sql1 = "Select * From attachfile where mailid = ? ";
                        stmt=con.prepareStatement(sql1);
                        stmt.setString(1,rs.getString(1));
                        rs1= stmt.executeQuery();
                        rs1.next();
                        fileName = rs1.getString("filename")  ;
                        fileSize = rs1.getString("filesize");
                    }
                    message.setPriorityFlag(rs.getString(9));
                    message.setReadFlag(rs.getString(10));
                    message.setFileName(fileName);
                    message.setFileSize(fileSize);
                    System.out.print("Mesage" + message.getReceiverAddress());
                    out.print("Mesage" + message.getReceiverAddress());
                    messageList.add(message);

                 }
                session.setAttribute("messageList", messageList);
                response.sendRedirect("Search.jsp");
                ptmt.close();
            con.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
               System.out.println( e.getMessage());
        }
    }
    catch(Exception e){e.printStackTrace();}
    finally {
        out.close();
    }
}

// &lt;editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."&gt;
/**
 * Handles the HTTP &lt;code&gt;GET&lt;/code&gt; method.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP &lt;code&gt;POST&lt;/code&gt; method.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}

}
</code></pre>
