朋友我有一个jsp页面,它从mysql表中检索数据并以表格的形式显示它们。我还将显示另一列,其中包含一个按钮,将状态设置为1。我点击状态按钮时需要更新数据库表。
以下是jsp的代码
<form action="Edit" method="post">
<table cellspacing="8">
<tr>
<td><b>Name</b>
</td>
<td><b>Place</b>
</td>
<td><b>Gender</b>
</td>
<td><b>UserName</b>
</td>
<td><b>Password</b>
</td>
<td><b>Status</b>
</td>
<td></td>
</tr>
<%
ViewService vs = new ViewService();
ResultSet rs = vs.getRecords();
while(rs.next())
{
%>
<tr>
<td><%=rs.getString(2) %></td>
<td><%=rs.getString(3) %></td>
<td><%=rs.getString(4) %></td>
<td><%=rs.getString(5) %></td>
<td><%=rs.getString(6) %></td>
<td><%=rs.getString(7) %></td>
<td></td>
<td><input type="text" name="userid" value="<%=rs.getString(2) %>"
style="visibility: hidden;" > <input type="submit" value="Active"></td>
</tr>
<% }
%>
</table>
</form>
我的编辑servlet是
package Admin;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
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 DBcon.Dbcon;
@WebServlet("/Edit")
public class Edit extends HttpServlet {
private static final long serialVersionUID = 1L;
public Edit() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con;
PreparedStatement ps;
Dbcon db = new Dbcon();
con = db.getCon();
int status=1;
int userid =Integer.parseInt(request.getParameter("userid"));
try {
ps=con.prepareStatement("update tbluser set status = ? where userid
= ?");
ps.setInt(7,status);
ps.setInt(1, userid);
ps.executeUpdate();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect("ViewUser.jsp");
}
}
答案 0 :(得分:0)
将表单标记放在while循环中(&lt; td&gt;)而不是&lt;表&gt;
....
<td><%=rs.getString(7) %></td>
<td></td>
<td>
<form action="Edit" method="post">
<input type="text" name="userid" value="<%=rs.getString(2) %>" style="visibility: hidden;" > <input type="submit" value="Active">
</form>
</td>
</tr>
<% }
%>
</table>
使用超链接代替表单和按钮
<td>
<a href="Edit?userd=<%=rs.getString(2) %>"> Activate </a>
<td>
答案 1 :(得分:0)
试试这个兄弟我修改了你的代码。
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con;
PreparedStatement ps;
Dbcon db = new Dbcon();
con = db.getCon();
int status=1;
int userid =Integer.parseInt(request.getParameter("userid"));
try {
ps=con.prepareStatement("update tbluser set status = '"+status+"' where userid = '"+userid+"'");
ps.executeUpdate();
int updateQuery = ps.executeUpdate();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect("ViewUser.jsp");
}
}