2013-01-18 62 views
1

我正在使用以下代碼執行查詢。它顯示錯誤爲ORA-00905:缺少關鍵字。當我在sql * plus中執行這個時,它顯示正確的結果。通過使用servlet它不會。任何人都可以幫我解決問題ORA-00905:從servlet執行時缺少關鍵字錯誤

public class PickOfferNo extends HttpServlet { 


protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    PrintWriter out = response.getWriter(); 
    try { 
     //TODO output your page here 
     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Servlet PickOfferNo</title>"); 
     out.println("</head>"); 
     out.println("<body>"); 
     out.println("<h1>Servlet PickOfferNo at " + request.getContextPath() + "</h1>"); 
     out.println("</body>"); 
     out.println("</html>"); 

    } finally {    
     out.close(); 
    } 
} 

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 

@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    //processRequest(request, response); 

    response.setContentType("text/html;charset=UTF-8"); 
    PrintWriter out = response.getWriter(); 
    try 
    { 
     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Servlet PickOfferNo</title>"); 
     out.println("</head>"); 
     out.println("<body>"); 
     out.println("<h1>Servlet PickOfferNo at " + request.getContextPath() + "</h1>"); 
     doPost(request,response); 
     out.println("</body>"); 
     out.println("</html>"); 
    } 
    finally 
    { 
     out.close(); 
    } 
} 


@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    //processRequest(request, response); 
    String ofrn=null; 
    String cd=null; 
    String no=null; 


    cd=request.getParameter("sec_code"); 
    PrintWriter out=response.getWriter(); 
    try 
    { 
     Class.forName("oracle.jdbc.driver.OracleDriver"); 
     java.sql.Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.200:1521:pdsm", "test", "test"); 
      Statement stmt=conn.createStatement(); 
       conn.setAutoCommit(true); 
       ResultSet rs=stmt.executeQuery("select to_char(nvl(max(substr(offerno,4,4)),0)+1) into '"+no+"' from offer1 where div_sec='"+cd+"'"); 
       ResultSet rs1=stmt.executeQuery("select lpad(ltrim(rtrim("+no+")),4,'0') from dual"); 
       if(rs1.next()) 
        no=rs1.getString(1); 
       ofrn=cd+no; 

       //obj.put("offrn", ofrn); 
       out.println(ofrn); 
    } 
    catch(Exception e) 
    { 
     out.println(e.getMessage()); 
    } 
} 

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

謝謝。請幫忙

回答

2

SELECT INTO表達式在這裏不起作用。 SELECT INTO表達式只能在運行時使用,例如,在存儲過程中。您應該使用常規的SELECT語法,並像往常一樣從ResultSet對象中取得結果。下面的代碼應該工作:

   ResultSet rs=stmt.executeQuery("select to_char(nvl(max(substr(offerno,4,4)),0)+1) from offer1 where div_sec='"+cd+"'"); 
      if(rs.next()) 
       no=rs.getString(1); 
      ResultSet rs1=stmt.executeQuery("select lpad(ltrim(rtrim("+no+")),4,'0') from dual"); 
      if(rs1.next()) 
       no=rs1.getString(1);