朋友我有一个jsp页面,它从sql表中检索数据并以表格的形式显示它们。我还将显示另一列,其中包含一个用于从sql表中删除该行/条目的按钮。 以下是我的jsp页面的代码:
<%@ page import="java.sql.*" %>
<%
Class.forName ("com.mysql.jdbc.Driver");
Connection conn=null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","pass");
%>
<body>
<center>
<h1>USERS TABLE</h1>
<table>
<tr>
<th>UserId</th>
<th>Username</th>
</tr>
<%
Statement stm = conn.createStatement();
String s = "SELECT * FROM login";
ResultSet r = stm.executeQuery(s);
while ( r.next() )
{
out.print("<tr>");
out.print("<td>" + r.getString ("userid") + "</td>" );
out.print("<td>" + r.getString ("username") + "</td>");
out.print("<td>" + "<input type=button value='Delete'/>"+ "</td>");
out.print("</tr>");
}
%>
</table>
</center>
</body>
现在我希望我的删除按钮提取用户ID并传递查询以从数据库中删除所选用户。我对如何实现此用户标识提取和处理查询感到困惑。
答案 0 :(得分:1)
我建议你这样做
1}首先创建一个servlet(比如Display.java)并将数据库内容存储在响应属性中。将响应属性发送到jsp(比如home.jsp)
2}在jsp文件中检索响应属性并创建一个删除按钮,当调用OnClick时,运行一个脚本,将脚本指向执行删除操作的servlet(比如deleteData1.java)。
我附上以下相应的代码。
(Display.java) 它将数据库内容存储在arrayList对象中。此外,arrayList对象将存储在response属性中并发送到jsp(home.jsp)。
public Display() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection conn = null;
Statement st;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","pass");
ArrayList al=null;
ArrayList userList =new ArrayList();
String query = "select userid,username from login";
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next())
{
al = new ArrayList();
al.add(rs.getInt(1));
al.add(rs.getString(2));
userList.add(al);
}
request.setAttribute("userList",userList);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/home.jsp");
dispatcher.forward(request,response);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
(针对home.jsp)
它将检索从Display.java发送的响应属性。单击删除按钮时会调用一个脚本,该脚本会将其转发到一个servlet,该servlet将执行与该id相对应的删除操作。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*;" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script language ="javascript">
function deleteData(id){
window.location.href="http://localhost:8080/First/deleteData1?id="+id;
}
</script>
</head>
<body>
<br>
<table align="center">
</table>
<br>
<table border='1' width='300' cellpadding='1' cellspacing='0'>
<tr><td colspan=3 align="center"></td></tr>
<tr>
<td>UserId</td><td>userName</td>
</tr>
<%
List Li = new ArrayList();
Iterator lr;
if(request.getAttribute("userList")!=null && request.getAttribute("userList")!="")
{
List userList = (ArrayList)request.getAttribute("userList");
Iterator itr = userList.iterator();
while(itr.hasNext())
{
Li = (ArrayList)itr.next();
lr = Li.iterator();
Integer id = (Integer)lr.next();
%>
<tr>
<td><%=id%></td>
<%
while(lr.hasNext())
{
%>
<td> <%=lr.next()%></td>
<%
}
%>
<td><input type="button" name="delete" value="Delete" onclick="deleteData(<%=id%>);"></td>
</tr>
<%
}
}
%>
}
</table>
</body>
</html>
最后,当单击删除按钮时,脚本将运行,这将指向将执行删除操作的servlet(deleteData1.java)
(deleteData1.java)
@WebServlet("/deleteData1")
public class deleteData1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public deleteData1() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection conn = null;
int id = Integer.parseInt(request.getParameter("id"));
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=Tpg@1234");
String query = "delete from login where id="+id;
Statement stmt = conn.createStatement();
int i = stmt.executeUpdate(query);
if(i>0){
response.sendRedirect("http://localhost:8080/First/Display");
}
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
答案 1 :(得分:0)
您可以这样做:
out.print("<td>"+"<input type='button' value='Delete' id='"+r.getString("userid")+"'/>"+</td>");
并检查要删除的用户ID,如下所示:
$(document).ready(function(){
$(":button").click(function(){
alert("Deleting user id: "+this.id);
});
});
答案 2 :(得分:0)
使用超链接代替按钮
<td>
<a href="Edit?userd=<%=rs.getString(1) %>"> Activate </a>
<td>
然后你必须创建Edit servlet或jsp页面来编写你的Query