2011-05-13 70 views
-2

我想實現在郵件系統中搜索郵件。我用簡單的程序來搜索數據庫中的郵件。我想知道的查詢,這是下面給出的錯誤:在數據庫中搜索郵件

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 <code>GET</code> and <code>POST</code> 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(); 
    } 
} 

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
/** 
* Handles the HTTP <code>GET</code> 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 <code>POST</code> 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"; 
} 

} 
+0

你得到了什麼錯誤信息? – 2011-05-13 15:02:37

+0

你得到的錯誤/異常是什麼? – lunactic 2011-05-13 15:02:40

+2

壞的,壞的標題。 '與java相關的問題'。幾乎所有的東西都是一個問題,'Java'就是你的標籤。 – 2011-05-13 15:04:14

回答

1

不要寫

AND subject LIKE '%' ? '%' AND body LIKE '%?%' AND senderaddress LIKE '%?%' 

而是由

使用

AND subject LIKE ? AND body LIKE ? AND senderaddress LIKE ? 

和設置參數值

ps.setString(n, "%your_value%"); 

更新我已經檢查,並且'%' || ? || '%'?版本都在工作。

你的錯誤似乎在AND subject LIKE '%' ? '%' AND body LIKE '%?%' AND senderaddress LIKE '%?%'

0

被省略||你可以不喜歡所示的例子中使用。 而是嘗試使用這種方法:

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

,然後確保searchQuery是瀨戶到"%<query>%" - 例如

//search for messages with subject containing "Metting" 
String searchQuery = "%meeting%"; 
ptmt.setString(2, searchQuer);