使用jsp从db中删除行

时间:2015-07-03 23:02:51

标签: java mysql jsp jdbc web

问题在于,当我点击deleteData中的userNotes.jsp测试函数中的按钮 DELETE - out.println(value)时,工作 - 它显示了点击的内容的当前值按钮但没有别的。它不会从数据库中删除数据,也不会在响应时发送重定向,我需要做什么?
userNotes.jsp

                        <table id="notes" border="2">
                            <tr>

                                <td>note name</td>
                                <td>note</td>

                            </tr>
                            <%
                                PrintWriter writer = response.getWriter();
                                int id = Integer.parseInt(request.getParameter("r"));
                                try {
                                    Class.forName("com.mysql.jdbc.Driver");
                                    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/evernoteDB",
                                            "evernoteDB", "0633739768z");

                                    Statement st = con.createStatement();
                                    ResultSet rs;
                                    rs = st.executeQuery("SELECT * FROM note");

                                    while (rs.next())
                                    {
                            %>
                            <tr>
                                <td><%=rs.getString(1) %></td>
                                <td><%=rs.getString(2) %></td>
                                <td>

                                            <a href=deleteData?id=<%=rs.getString(1) %>> Delete </a>

                                </td>
                       </tr>
                            <%

                                    }
                                    rs.close();
                                    st.close();
                                    con.close();
                                }
                                catch(ClassNotFoundException e)
                                {
                                    writer.println("Couldn't load database driver: " + e.getMessage());
                                }
                                catch(SQLException e)
                                {
                                    writer.println("SQLException caught: " + e.getMessage());
                                }
                                catch (Exception e)
                                {
                                    writer.println(e);
                                }

                            %>
                        </table>

deleteData - servlet

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.*;
import java.sql.*;


@WebServlet("/deleteData")
public class deleteData extends HttpServlet {

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


    String value = request.getParameter("id");
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/evernoteDB",
                "evernoteDB", "0633739768z");


        Statement st = conn.createStatement();

        st.executeUpdate("DELETE FROM note WHERE noteName=" + value);
        out.print(value);
            response.sendRedirect("http://localhost:8080/userNotes.jsp");

        conn.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
  }
}

1 个答案:

答案 0 :(得分:0)

String的类型为st.executeUpdate("DELETE FROM note WHERE noteName='" + value + "'"); ,因此执行查询应引用该值。

PreparedStatement

请考虑使用response.setContentType("text/html"); PrintWriter out = response.getWriter(); 。它也很安全,也可以处理这些错误。

由于您正在重定向,因此您不应该尝试设置任何内容标头。删除以下内容,因为servlet不应该写任何响应,而是重定向。

sendRedirect()

此外,请勿对主机和端口号进行硬编码,否则您将无法在其他服务器上部署生成的 war 。使用conn.close(); response.sendRedirect("/userNotes.jsp"); return; 的相对网址

id <- paste("sample", c(1:9))

A <- seq(10, 90, 10)
B <- seq(100, 180, 10)

Example <- data.frame(id, A, B)

Example$A2 <- with(Example, A/sum(A))
Example$B2 <- with(Example, B/sum(B))