使用Java Servlet在MySQL中创建存储过程?

时间:2018-11-27 04:39:09

标签: java mysql

尝试创建存储过程。当它通过可怕的MySOL命令运行时它正在工作,但是通过Java它正在创建没有数据的空白表吗?我正在创建具有动态表名和固定日期范围的存储过程。

DELIMITER $$
            CREATE PROCEDURE `create_tb_tw`(IN tableName varchar(255),IN x varchar(255),IN y varchar(255))
            BEGIN
            SET @sql = CONCAT('CREATE TABLE ',tableName,' SELECT * FROM emp
             WHERE hiredate >= ',x,'
             AND hiredate <= ',y);
                    PREPARE s FROM @sql;
                    EXECUTE s;
                    DEALLOCATE PREPARE s;
            END $$
            DELIMITER;


            call create_tb_tw("x","'1980-12-17'","'1981-02-22'");//working







    public class emdetails extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            try (PrintWriter out = response.getWriter()) {

                //db connection

                Connection con = DbConnection.getcon();
                String Sdate = request.getParameter("Sdate");
                String Edate = request.getParameter("Edate");
                String TName = request.getParameter("TName");
                try {
                    CallableStatement cStmt = con.prepareCall("{call create_tb_tw(?, ?,?)}");
                    cStmt.setString(1, TName);
                    cStmt.setString(2, Sdate);  
                    cStmt.setString(3, Edate);
                    cStmt.executeQuery();




                    }
                } catch (SQLException ex) {
    //                System.out.println(emdetails.class.getName()).log(Level.SEVERE, null, ex);
                    Logger.getLogger(emdetails.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }

        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /**
         * Handles the HTTP <code>GET</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doGet(HttpServletReq`enter code heenter code herere`uest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }

        /**
         * Handles the HTTP <code>POST</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }

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

    }

试图创建存储过程。当它通过不可靠的sql​​命令运行时起作用,但是通过java它正在创建无数据的空白表?我创建了具有动态表名和固定日期范围的协调过程。

0 个答案:

没有答案