简体   繁体   中英

Wrong result in executing oracle query from servlet

I'm using the following code to execute the query. I want to display the result as "B010025" by using lpad command and concatenation.(25 and B01 is given as input value). But it shows only B0125. When I'm executing this from SQL*Plus it shows the correct result. But in servlet it doesn't. I can't understand this. Can anyone help me to get the expected result.

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>
}

Thanks in advance

Only:

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

and:

ofrn=cd+no;

contribute to the output result:

out.println(ofrn);

So, nothing unexpected is happening!

It is because you haven't selected the value return by the database. executing

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

doesn't change the value of "no" or "cd".

You should get the result set and process it to access the first column in the first row return by the result set.

Following code should do the assignment. (Not tested.)

ResultSet rs1=stmt.executeQuery("select lpad(ltrim(rtrim('"+no+"')),4,'0') from dual");
if(rs1.next()){
no = rs1.getString(1);
}

You have never assign values to cd and 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM