日期:2014-05-17  浏览次数:20786 次

新手JSP问题,如何实现“删除”按钮功能
我想实现这样的页面功能:在显示页面中显示所有的注册用户,并在每一个用户后面添加删除功能(就是每条用户记录后面加个删除按钮),删除该条用户后跳转回用户显示页面。
  下面是我写好的显示所有用户的页面,删除语句倒是知道,问题是不知道怎么在每个用户后面添加这个删除按钮来执行删除语句。
HTML code
 
  <head>   
    <title>添加用户信息</title>
  </head>
  <body>
    <%
       request.setCharacterEncoding("gb2312");
       String reqUsername=request.getParameter("username");
       String reqPassword=request.getParameter("userpassword");
     %>
    <%
       String url="jdbc:mysql://localhost:3306/javaweb";
       String user="root";
       String password="admin";
       Connection conn=null;
       PreparedStatement pstmt=null;  
       ResultSet rs=null;
       try{
          Class.forName("com.mysql.jdbc.Driver");
          conn=DriverManager.getConnection(url,user,password);
          }catch(ClassNotFoundException e){
              out.println("找不到驱动类");
              }catch (SQLException e){
              out.println("连接数据库失败");
              }
       try{//添加注册用户
          String addUser="INSERT INTO user(userid,username,password)VALUES(null,?,?)";
          pstmt=conn.prepareStatement(addUser);
          pstmt.setString(1,reqUsername);
          pstmt.setString(2,reqPassword);
          pstmt.executeUpdate();
          }catch(SQLException e){
              out.println("添加用户信息失败");
              e.printStackTrace();
              }
       try{  //显示所有用户,
             //我猜想既然每条记录后面都要有个删除按钮,应该是要加在这个语句块里的吧
          String queryALL="SELECT *FROM user;";  
          pstmt=conn.prepareStatement(queryALL);
          rs=pstmt.executeQuery();
          while(rs.next()) {
              int userid=rs.getInt(1);
              String username=rs.getString(2);
              String userpassword=rs.getString(3);
              out.println("用户id:"+userid+",");
              out.println("用户名:"+username+",");
              out.println("用户密码:" + userpassword +"<br>");//按钮添加在这里?如何添加?
              }
          }catch (SQLException e) {
              out.println("查询所有用户失败");
              } 
       try{//关闭数据库
          if(pstmt!=null){
               pstmt.close();
               pstmt=null;
               }
               if(conn!=null){
                     conn.close();
                     conn=null;
                     }
           }catch(Exception e){
               out.println("数据库关闭异常");
               }       
    %>
  </body>
</html>




------解决方案--------------------
参考一下:
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
 
function chk(id)
{

if(confirm("你确定要删除吗?"))
{
location.href="manager.jsp?pid="+id;
}