JSP Servlet MYSQL 查询未执行
JSP Servlet MYSQL query does not execute
所以我一直在为这个问题奋斗 2 个小时,但我找不到解决方案。
我有 2 个文件。 Manage.jsp 我正在用数据打印出整个 table 并且我有 2 个提交按钮 - 1 个用于删除特定行,另一个用于从该行中下载文件。
提交按钮传递关于具有唯一名称的特定行的字符串,该名称允许识别将在 servlet 文件中 deleted/downloaded 的整行。
类似的方法适用于插入查询或其他查询,但对于这个查询似乎不起作用。这很奇怪,特别是因为 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>
有什么想法吗?
好吧,似乎问题已通过简单地使用包含文件名的变量旁边的 .strip()
函数得到解决。由于某种原因,必须添加一些额外的空格,因此导致错误。
所以我一直在为这个问题奋斗 2 个小时,但我找不到解决方案。 我有 2 个文件。 Manage.jsp 我正在用数据打印出整个 table 并且我有 2 个提交按钮 - 1 个用于删除特定行,另一个用于从该行中下载文件。
提交按钮传递关于具有唯一名称的特定行的字符串,该名称允许识别将在 servlet 文件中 deleted/downloaded 的整行。
类似的方法适用于插入查询或其他查询,但对于这个查询似乎不起作用。这很奇怪,特别是因为 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>
有什么想法吗?
好吧,似乎问题已通过简单地使用包含文件名的变量旁边的 .strip()
函数得到解决。由于某种原因,必须添加一些额外的空格,因此导致错误。