使用JSP将值插入数据库

时间:2013-06-24 04:41:48

标签: ajax database jsp

<%
      if (MultipartFormDataRequest.isMultipartFormData(request))
      {
         // Uses MultipartFormDataRequest to parse the HTTP request.
         MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);
         String todo = null;
         if (mrequest != null) todo = mrequest.getParameter("todo");
         if ( (todo != null) && (todo.equalsIgnoreCase("upload")) )
         {
            Hashtable files = mrequest.getFiles();
            if ( (files != null) && (!files.isEmpty()) )
            {
                UploadFile file = (UploadFile) files.get("uploadfile");
                if (file != null) 

                out.println("");

                 //out.println(report1);  
                 String sever = mrequest.getParameter("sever");
                 String ease = mrequest.getParameter("ease");
                 String logo = "C:/uploads/"+file.getFileName();  // Uses the bean now to store specified by jsp:setProperty at the top.
                 String dana = mrequest.getParameter("danalysis");
                 String loc = mrequest.getParameter("loc");
                 String state = mrequest.getParameter("state");
                 String Sr = mrequest.getParameter("Sr");
                 String Doc_ID = mrequest.getParameter("doc");
                 String impact = mrequest.getParameter("impact");
                 String desc = mrequest.getParameter("desc");
                 String ref = mrequest.getParameter("ref");
                 String recom = mrequest.getParameter("recom"); 
                try
                        {
                        String connectionURL = "jdbc:mysql://localhost:3306/mssg";
                        Class.forName("com.mysql.jdbc.Driver").newInstance();
                        Connection con=DriverManager.getConnection(connectionURL, "root","");
                        out.println("OK!\n");
                    PreparedStatement ps1=con.prepareStatement("insert into report values(?,?,?,?,?,?,?,?,?,?,?,?)");
                        ps1.setString(1,Doc_ID);
                        ps1.setString(2,Sr);
                        ps1.setString(3,sever);
                        ps1.setString(4,ease);
                        ps1.setString(5,state);
                        ps1.setString(6,loc);
                        ps1.setString(7,desc);
                        ps1.setString(8,impact);
                        ps1.setString(9,dana);
                        ps1.setString(10,logo);
                        ps1.setString(11,recom);
                        ps1.setString(12,ref);

                        int count=ps1.executeUpdate();
                        if(count > 0)
            {
              out.println("successfully inserted");
                          //response.sendRedirect("/index.jsp");

            }
            else
                         {
            out.println("error occured");
                     }
                   }
            catch (Exception e)
            {
            System.out.println("error in program:-"+e);
            }

                upBean.store(mrequest, "uploadfile");
            }
            else
            {
               out.println("<li>No uploaded files");
            }

   } 
 }

%>

在上面的代码中,我得到了所有值,文件也在上传,但无法将这些值插入数据库。我想我犯了一个很小的错误,请告诉我上面代码中的问题是什么。这段代码一直有效,直到连接,但在准备声明之后它才能正常工作。

1 个答案:

答案 0 :(得分:0)

这是因为你使用了try和catch阻止。

在jsp页面生命周期中,jsp页面在servlet中翻译。所以数据库连接的代码将自动放在try catch块中。

只需删除try catch块。