九、Table删除信息

在每一个按要求查询的JSP中都有一个删除操作,具体是:

			<a href="Detail?num=<%=times.getNum() %>">查看</a>
			</td>
			<td>
			<a href="Delete?num=<%=times.getNum() %>">删除</a>
			</td>

具体解释一下href中的内容,以删除为例

Delete是删除操作的servlet名字,如果存在这个servlet,那么将times列表的num用getNum函数赋值给num

---------------------------------------------------------------

web.xml配置

                <servlet>
  	<servlet-name>Delete</servlet-name>
  	<servlet-class>servlet.Delete</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>Delete</servlet-name>
  	<url-pattern>/Delete</url-pattern>
  </servlet-mapping>

和之前一样

url-pattern的/后面和JSP的a标签中的Delete一样,然后根据一样的servlet-name找到servlet-class,也就是servlet包中的Delete.java

-----------------------------------------------------------

Delete.java

package servlet;
import java.io.IOException;  
import java.sql.Connection;  
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
import java.util.ArrayList;  
import java.util.List;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import bean.AddMessageBean;

public class Delete extends HttpServlet { 
	
	 private static final long serialVersionUID = 1L;  
	 
	 public void doPost(HttpServletRequest request, HttpServletResponse response)  
	            throws ServletException, IOException {  
	 request.setCharacterEncoding("UTF-8");  
	 response.setCharacterEncoding("UTF-8"); 
	            
	  try {
	        Connection conn=null;  
	        Statement stat=null;  
	        conn=connect();  
	        stat=conn.createStatement();  
	        String num=request.getParameter("num");  //将jsp的a标签里的num的值赋给String类型的num
	        //System.out.println(id2);
	        String sql="delete from stumessage where num=?";//删除语句
	        PreparedStatement ps=conn.prepareStatement(sql);
	        ps.setString(1, num);//给SQL语句中第一个问号赋值给num
	        ps.executeUpdate();  //执行删除操作
	        ps.close();//关闭
	        
	        request.getRequestDispatcher("TimeManage.jsp").forward(request, response);   //完成后,页面返回TimeManage.jsp
		  
	  } catch (ClassNotFoundException e) {  
          // TODO Auto-generated catch block  
          e.printStackTrace();  
      } catch (SQLException e) {  
          // TODO Auto-generated catch block  
          e.printStackTrace();  
      } 
}
	    public void doGet(HttpServletRequest request, HttpServletResponse response)  
	    	    throws ServletException, IOException {  
	    	       doPost(request,response);  
	    	    }  
	    	      
	    	      
	    	    //数据库连接方法  
	    	    public Connection connect() throws ClassNotFoundException, SQLException{  
	    	        Connection conn=null;   
	    	        Class.forName("com.mysql.jdbc.Driver");  
	    	        String url = "jdbc:mysql://localhost:3306/project?"  
	    	                + "user=root&password=root&useUnicode=true&characterEncoding=UTF8";  
	    	        conn=DriverManager.getConnection(url);   
	    	        return conn;  
	    	    }  
	    	    //关闭数据库资源  
	    	    public void close(Statement stat,Connection conn) throws SQLException{  
	    	        if(stat!=null){  
	    	               stat.close();  
	    	        }  
	    	        if(conn!=null){  
	    	               conn.close();  
	    	        }  
	    	    }  
	

}

这样删除操作就完成了

----------------------------------------------------------------------

因为所有的按要求查询页面调用同一个servlet,所以只需要更改一下后面的

num=<%=times.getNum() %>

按照不同的要求传入值就可以了。

这个部分,具体的之前所有的JSP页面中已经都写好了

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页