JSP Servlet MYSQL查询未执行

h5qlskok  于 2022-12-07  发布在  Mysql
关注(0)|答案(1)|浏览(116)

因此,我与这个问题斗争了2个小时,但我找不到解决方案。我有2个文件。Manage.jsp,我在其中打印出整个表的数据,还有2个提交按钮- 1个用于删除特定行,其他用于从该行中下载文件。
提交按钮传递一个关于特定行的字符串,该字符串具有唯一的名称,允许标识将在servlet文件中删除/下载的整行。
类似的方法可以用于插入或其他查询,但对于这个查询,它似乎不起作用。这很奇怪,特别是因为MySQL Workbench中的同一个命令实际上完成了这项工作。

@WebServlet("/ManageFiles")
public class ManageFiles extends HttpServlet {
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String alert = "";
        String remove = request.getParameter("remove");
        String download = request.getParameter("download");
        Connection con = null;
        if (remove != null) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String DB_NAME = "wap_dama";
                out.println("test");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + DB_NAME + "?serverTimezone=UTC","root","asd123");  
                PreparedStatement ps = con.prepareStatement("delete from wap_dama.files where FileName = ?");
                ps.setString(1, remove);
                int row = ps.executeUpdate();
                if (row > 0)
                {
                    out.println(row);
                    alert = "Your file was successfully deleted!";
                }
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }

至于jsp代码

<tbody>
                  <%
                  try
                   {
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        String DB_NAME = "wap_dama";
                        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + DB_NAME + "?serverTimezone=UTC","root","asd123");
                        PreparedStatement ps = con.prepareStatement("SELECT id_files, FileName, FileExtension, FileSize FROM files WHERE users_id_users=?");
                        int id = (int) session.getAttribute("id");
                        ps.setInt(1, id);
                        ResultSet rs =ps.executeQuery();
                        //Statement stmt=con.createStatement();
                        //ResultSet rs=stmt.executeQuery(ps);
                        while(rs.next())
                        {
                            
                    %>
                            <tr>
                            <td><%out.println(rs.getString("FileName")); %></td>
                            <td><%out.println(rs.getString("FileExtension")); %></td>
                            <td><%out.println(rs.getInt("FileSize")); %></td>
                            <td><button type="submit" class="btn btn-warning" name="download" value="<%out.println(rs.getString("FileName")); %>">Download</button></td>
                            <td><button type="submit" class="btn btn-danger" name="remove" value="<%out.println(rs.getString("FileName")); %>">Remove</button></td>
                            </tr>
                      <%
                      }
                      %>
                      </tbody>

有什么想法吗?

flvlnr44

flvlnr441#

好的,看起来这个问题已经被简单地使用.strip()函数在包含文件名的变量旁边解决了。由于某种原因,一定是添加了一些额外白色,因此导致了错误。

相关问题