通过Servlet从数据库中删除行

时间:2015-09-08 10:29:50

标签: html servlets jdbc

我的servlet中有一个关于从数据库中删除记录的问题。 请查看我的servlet代码,请纠正我。先感谢您 当手动提供单个ID时,DeleteRow servlet正常工作。 场景是:

ManageSinger.java servlet显示数据库中的记录以及"删除"每一行都有超链接。但问题是每当我尝试按下删除按钮时..它在deleterow.java servlet中收到ID的空值。 请从这里引导我如何才能将相应的ID传递给另一个servlet。

ManageSinger.java

package com.ea.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class ManageSinger extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    PrintWriter out = res.getWriter();
    res.setContentType("text/html");
    out.println("<html><body>");
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/EATWO","root","");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from singerdetails");
        out.println("<form method = \"post\">");
        out.println("<table border=1 width=50% height=50%>");
        out.println("<tr><th align=\"center\">Singer Name</th><th align=\"center\">StageName</th><th align=\"center\">Language</th><th></th><tr>");
        while (rs.next()) {
            String singername = rs.getString("singername");
            String stagename = rs.getString("stagename");
            String language = rs.getString("language"); 
            String id = rs.getString("userID");
            HttpSession session = req.getSession(true); 
            session.setAttribute("userID",id);
            out.println("<tr><td align=\"center\">" + singername + "</td><td align=\"center\">" + stagename + "</td><td align=\"center\">" + language + "</td><td align=\"center\"><a href = \"./deleterow\">Delete</a></td></tr>");
        }
        out.println("</table>");
        out.println("</form");
        out.println("</body></html>");
        con.close();
       }
        catch (Exception e) {
        e.printStackTrace();
    }finally{

    }
}
} 

DeleteRow.java

package com.ea.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.jws.Oneway;
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 javax.servlet.http.HttpSession;


public class DeleteRow extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
Connection con;
PreparedStatement st;
ResultSet rs;
try
{ 
HttpSession session = req.getSession(true);
Class.forName("com.mysql.jdbc.Driver");
String id = (String) session.getAttribute("id");
System.out.println(id);
con =        DriverManager.getConnection("jdbc:mysql://localhost:3306/EATWO","root","");
st= con.prepareStatement("delete from singerdetails where userID = ?");
st.setString(1, id);
st.executeUpdate();
int i = st.executeUpdate();
if(i!=0)
pw.println("Deleting row...");
else if (i==0)
{
pw.println("<br>Row has been deleted successfully.");
}
}
catch(SQLException sx)
{
pw.println(sx);
}
catch(ClassNotFoundException cx)
{
pw.println(cx);
}   
}
}

2 个答案:

答案 0 :(得分:2)

在DeleteRow.java中说你应该String id = (String) session.getAttribute("userID");而不是(String) session.getAttribute("id")。在ManageSinger.java中,您要设置属性如下session.setAttribute("userID",id);

答案 1 :(得分:1)

根据您当前的ManageSinger.java,只会在会话中设置最后userID,这就是最后userID被删除的原因。在ManageSinger.java而不是将userID设置为会话属性,您可以将其设置为每条记录的url参数。

以下列出了更改:

//HttpSession session = req.getSession(true); 
//session.setAttribute("userID",id);
out.println("<tr><td align=\"center\">" + singername + "</td><td align=\"center\">" + stagename + "</td><td align=\"center\">" + language + "</td><td align=\"center\"><a href = \"./deleterow?userID="+id+"\">Delete</a></td></tr>");

DeleteRow.java而不是从会话中获取userID,您可以从网址获取该内容,如下所示:

//HttpSession session = req.getSession(true);
//String id = (String) session.getAttribute("id");
String id = req.getParameter("userID").toString();