使用jsp和servlets在mysql数据库中插入数据的问题?

时间:2016-10-13 19:03:25

标签: java mysql jsp servlets

我正在学习如何使用java中的servlet和JSP。我试图创建一个简单的表单,并使用action方法将数据发布到servlet中来处理它,但由于某种原因,数据没有插入到mysql数据库中。我试图检查数据是否从JSP表单中正确发布,并且它完美地运行。然后没有sql错误或任何东西,但当我点击提交按钮时,数据没有插入mysql数据库。任何人都可以帮我找出错误。这是我的servlet。

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       // processRequest(request, response);
        String projectname = request.getParameter("projectname");
        String description = request.getParameter("description");
        String initiator = request.getParameter("initiator");
        String email = request.getParameter("email");
        String repository = request.getParameter("repository");
        String projecttags = request.getParameter("tags");


        projecttags = projecttags.toLowerCase();
        String[] t = projecttags.split(",");
        int i;


         try {
                 Class.forName("com.mysql.jdbc.Driver");

                 Connection con = DriverManager.getConnection("jdbc:mysql://localhost/hactivism",
                         "root", "");
                 Statement st = con.createStatement();
                 //ResultSet rs;
                 for(int j=0;j<t.length;j++)
                 {  i = st.executeUpdate("insert into projectdetails(projectname, description, initiator,email,repository,projecttags) values ('" + projectname + "','" + description + "','" + initiator + "', '" + email + "', '" + repository + "', '" + t[j] + "'");

                     if (i > 0 && j==t.length-1) {
                         //session.setAttribute("userid", user);

                         response.sendRedirect("ProjectDescription.jsp");
                         // out.print("Registration Successfull!"+"<a href='index.jsp'>Go to Login</a>");
                     } else {
                         out.println("Registration Unsuccesful");
                     }

                 }       } catch (ClassNotFoundException | SQLException ex) {
                 Logger.getLogger(HomePage.class.getName()).log(Level.SEVERE, null, ex);
             }

  }

0 个答案:

没有答案