我正在使用以下代码来执行查询。我想通过使用 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>
}
提前致谢