0

我正在使用以下代码来执行查询。我想通过使用 lpad 命令和连接将结果显示为“B010025”。(25 和 B01 作为输入值给出)。但它只显示 B0125。当我从 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;
    String cd="B01";
    String no="25";
    JSONObject obj = new JSONObject();
    //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");
                 //while(rs1.next())
                 ofrn=cd+no;

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


@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>
}

提前致谢

4

3 回答 3

1

仅有的:

String cd="B01";
String no="25";

和:

ofrn=cd+no;

有助于输出结果:

out.println(ofrn);

所以,没有什么意外的发生!

于 2013-01-18T11:59:05.823 回答
1

这是因为您没有选择数据库返回的值。执行

"select lpad(ltrim(rtrim('"+no+"')),4,'0') from dual"

不会改变“no”或“cd”的值。

您应该获取结果集并对其进行处理以访问结果集返回的第一行中的第一列。

以下代码应该完成分配。(未测试。)

ResultSet rs1=stmt.executeQuery("select lpad(ltrim(rtrim('"+no+"')),4,'0') from dual");
if(rs1.next()){
no = rs1.getString(1);
}
于 2013-01-18T11:59:10.807 回答
0

cd您从未为和赋值no

    ResultSet rs1=stmt.executeQuery("select lpad(ltrim(rtrim('"+no+"')),4,'0') from dual");
    //while(rs1.next())
    ofrn=cd+no;

    //obj.put("offrn", ofrn);
    out.println(ofrn);
于 2013-01-18T11:56:39.063 回答