jsp+servlet+jdbc实现对数据库的增删改查(2)

package example.servlet.book; import java.io.IOException; import java.sql.Connection; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import example.dao.book.BookJdbcDao; import example.dao.book.ConnectionFactory; /** * Servlet implementation class DeleteServlet */ public class DeleteServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.valueOf(request.getParameter("id")); try { // // 加载数据库驱动,注册到驱动管理器 // Class.forName("com.mysql.jdbc.Driver"); // // 数据库连接字符串 // String url = "jdbc:mysql://localhost:3306/db_book"; // // 数据库用户名 // String username = "root"; // // 数据库密码 // String password = ""; // // 创建Connection连接 // Connection conn = DriverManager.getConnection(url, username, // password); // // 删除图书信息的SQL语句 // String sql = "delete from tb_books where id=?"; // // 获取PreparedStatement // PreparedStatement ps = conn.prepareStatement(sql); // // 对SQL语句中的第一个占位符赋值 // ps.setInt(1, id); // // 执行更新操作 // ps.executeUpdate(); // // 关闭PreparedStatement // ps.close(); // // 关闭Connection // conn.close(); BookJdbcDao bookDao=new BookJdbcDao(); Connection conn=ConnectionFactory.getInstance().getConnection(); bookDao.delete(conn,id); } catch (Exception e) { e.printStackTrace(); } // 重定向到FindServlet response.sendRedirect("https://www.jb51.net/FindServlet"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

4、servlet访问url映射配置:

由于客户端是通过URL地址访问web服务器中的资源,所以Servlet程序若想被外界访问,必须把servlet程序映射到一个URL地址上,这个工作在web.xml文件中使用<servlet>元素和<servlet-mapping>元素完成,<servlet>元素用于注册Servlet,它包含有两个主要的子元素:<servlet-name>和<servlet-class>,分别用于设置Servlet的注册名称和Servlet的完整类名。

一个<servlet-mapping>元素用于映射一个已注册的Servlet的一个对外访问路径,它包含有两个子元素:<servlet-name>和<url-pattern>,分别用于指定Servlet的注册名称和Servlet的对外访问路径.另外:同一个Servlet可以被映射到多个URL上,即多个<servlet-mapping>元素的<servlet-name>子元素的设置值可以是同一个Servlet的注册名.

eg:上例中的web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee " version="2.5"> <display-name>JdbcConnection</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>FindServlet</display-name> <servlet-name>FindServlet</servlet-name> <servlet-class>example.servlet.book.FindServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FindServlet</servlet-name> <url-pattern>/FindServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>UpdateServlet</display-name> <servlet-name>UpdateServlet</servlet-name> <servlet-class>example.servlet.book.UpdateServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UpdateServlet</servlet-name> <url-pattern>/UpdateServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>DeleteServlet</display-name> <servlet-name>DeleteServlet</servlet-name> <servlet-class>example.servlet.book.DeleteServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DeleteServlet</servlet-name> <url-pattern>/DeleteServlet</url-pattern> </servlet-mapping> </web-app>

5、jsp页面

1)、index.jsp  web页面主界面

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjxwp.html