如何使用jsp更新数据库中的文件?

时间:2019-05-09 05:55:38

标签: java mysql database jsp sql-update

我在数据库data_table中有一个文件名为'a.txt'的文件:

doc_id    filename

 321        a.txt

Table('data_table')定义:

doc_id   varchar(100) NOT NULL

filename varchar(100) NOT NULL

当我单击“上传”按钮时,我希望将数据库中的当前文件更新为“ b.txt”,否则,我不想更新文件,文件名应为“ a.txt”

当前,当我单击上载按钮时,它正在擦除上载的文件名('a.txt'),但没有任何显示。

数据库显示为:

doc_id   filename

321       

我的预期输出是

doc_id  filename

321     b.txt

updatedetails.jsp

<%

    String connectionURL = "jdbc:mysql://localhost:3306/doc_search";
    Statement stmt = null;
    Connection con = null;
    String docId=null;                        
    if(request.getParameter("doc_id")!=null && request.getParameter("doc_id")!="")
    {
        docId = request.getParameter("doc_id");

    }
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection(connectionURL, "root", "root");
    stmt = con.createStatement();                                           

    String qry = "select doc_id,doc_title,filename  from data_table where doc_id='"+docId+"'";
    ResultSet rst= stmt.executeQuery(qry);
    while(rst.next())
        {     
     %>
     <center>  <h1 class="header-w3ls" id="h1"> Update Document Details</h1></center>
     <form  id="form1" class="form-control" name="form1" action="update" enctype="multipart/form-data"    method="post"  >
        <div class="col-md-4 mb-3 form-group">
             <input type="hidden" name="doc_id" value="<%=rst.getString(1)%>">
        </div>
             <div class="col-md-4 mb-3 form-group">
                 <label for="validationDefault02">Document Title</label>       
                 <input type="text" class="form-control"  name="document_title" value="<%=rst.getString(2)%>" id="validationDefaultUsername" placeholder="Document Title " aria-describedby="inputGroupPrepend2" >
            </div>
            <div class="col-md-4 mb-3 form-group">
                <label for="validationDefaultUsername">Upload File </label>       
                <input type="file" class="form-control"  name="my_file_element"  aria-describedby="inputGroupPrepend2" >  
            </div>
        <div class="col-md-4 mb-3 form-group">

            <div class="col-md-4 mb-3">
                <center> <button class="btn btn-primary form-control" type="submit" name="Submit">Submit</button></center>
            </div>
        </div>
     </form>
            <%
               }
               con.close();                       
              %>

update.java

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

    String msg=null;
try{    


 String tYear=null;

   HttpSession session=request.getSession();
 Class.forName("com.mysql.jdbc.Driver").newInstance();


 String staffname = (String) session.getAttribute("username");     
 MultipartRequest multi =new MultipartRequest(request,"F:\\binsha\\JavaProject\\DOC_Register\\uploaded_files\\",100*1024* 1024);
 String docId=multi.getParameter("document_id");
 String documentTitle=multi.getParameter("document_title");
 String author=multi.getParameter("prepared_by");
String tReviewedby=multi.getParameter("reviewed_by");
String documentType=multi.getParameter("doc_type");
String tRemarks=multi.getParameter("remarks");
 String tDate=multi.getParameter("theDate2");    
 tYear = tDate.substring(6);

    PreparedStatement ps20= null;   

 Statement stmt = null;
 Connection con = null;
 ResultSet rst=null;   
  con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/doc_search", "root", "root");
 stmt = con.createStatement();

           Enumeration files = multi.getFileNames();
            String name=(String)files.nextElement();

            String filename=multi.getFilesystemName(name);
            System.out.println(filename);
            if(filename.equals(""))
            {

               String sql2="update data_table set doc_title=?,prepared_by=?,reviewed_by=?,doc_type=?,remarks=?,date=?,year=? where doc_id='"+docId+"'";

     ps20=con.prepareStatement(sql2);           
           ps20.setString(1, documentTitle);
            ps20.setString(2, author);
             ps20.setString(3,tReviewedby );
              ps20.setString(4,documentType );
               ps20.setString(5,tRemarks );
               ps20.setString(6,tDate );
               ps20.setString(7,tYear );                      
               ps20.executeUpdate(); 
}
            else{

               String sql1="update data_table set doc_title=?,prepared_by=?,reviewed_by=?,doc_type=?,remarks=?,date=?,year=?,filename=? where doc_id='"+docId+"'";

     ps20=con.prepareStatement(sql1);           
           ps20.setString(1, documentTitle);
            ps20.setString(2, author);
             ps20.setString(3,tReviewedby );
              ps20.setString(4,documentType );
               ps20.setString(5,tRemarks );
               ps20.setString(6,tDate );
               ps20.setString(7,tYear );
                    ps20.setString(8,filename );
               ps20.executeUpdate(); 
            }
        con.close(); 

}

catch(Exception e)
{
     out.println("<pre>");
    System.out.println(e);
    e.printStackTrace(out);
    out.println("</pre>");
}
request.setAttribute("Message", msg);

            // forwards to the message page
            getServletContext().getRequestDispatcher("/updatedetails.jsp").forward(request, response);
           out.println("</body> </html>");
}

如何更新数据库中的当前数据?

1 个答案:

答案 0 :(得分:0)

您需要检查用户selected中是否有update-process.jsp个其他文件,如下所示:

 PreparedStatement ps20=null;
String filename=request.getParameter("my_file_element");
//if filename doesn't have anything in it just update doc_title if required
if(filename.equals("")){
  String sql1="update data_table set doc_title=? where doc_id='"+docId+"'";    
  ps20=con.prepareStatement(sql1);
  ps20.setString(1, documentTitle);
  int row1=ps20.executeUpdate();  
  if (row1 > 0) {
      msg= "Updated Successfully";
      System.out.println(msg);
    }      

}else{
   //if there exist filename
  String sql2="update data_table set doc_title=?,filename=? where doc_id='"+docId+"'";
  ps20=con.prepareStatement(sql2);
  ps20.setString(1, documentTitle);
  ps20.setString(2, filename);
  int row2=ps20.executeUpdate();
   if (row2 > 0) {
     msg= "Updated Successfully";
     System.out.println(msg);
      }    
 }