1

我正在使用以下代码来执行查询。它显示错误为 ORA-00905:缺少关键字。当我在 sql*plus 中执行此操作时,它显示正确的结果。通过使用 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>
}

谢谢。请帮忙

4

1 回答 1

2

表达式在SELECT INTO这里不起作用。该SELECT INTO表达式只能在PL/SQL运行时中使用,例如在存储过程中。您应该使用常规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);
于 2013-01-18T12:37:49.507 回答