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

一、JSP和Servlet的简单介绍

1、Servlet和JSP简介:

Java开发Web应用程序时用到的技术主要有两种,即Servlet和JSP,Servlet是在服务器端执行的Java程序,一个被称为Servlet容器的程序(其实就是服务器) 负责执行Java程序,而JSP(Java Server Page)则是一个页面, 由JSP容器负责执行.

2、Servlet和JSP的区别:

Servlet以Java程序为主, 输出HTML代码时需要使用out.println函数,也就是说Java中内嵌HTML; 而JSP则以HTML页面为主,需要写Java代码时则在页面中直接插入Java代码, 即HTML中内嵌Java.

3、MVC模型

MVC模型就是将数据、逻辑处理、用户界面分离的一种方法

1)、M(Model, 模型):用于数据处理、逻辑处理

2)、V(View,视图):用于显示用户界面

3)、C(Controller,控制器):根据客户端的请求控制逻辑走向和画面

而在Java中,MVC这三个部分则分别对应于 JavaBeans、JSP和Servlet

1)、M = JavaBeans:用于传递数据,拥有与数据相关的逻辑处理

2)、V = JSP:从Model接收数据并生成HTML

3)、C = Servlet:接收HTTP请求并控制Model和View

4、jdbc连接,可参考文章:点击打开链接

二、代码演示,实现了book的添加删除和修改功能

1、环境的配置

myeclipse+tomcat+MySQL

2、Book中bean类

package example.bean.book; public class Book { // 编号 private int id; // 图书名称 private String name; // 价格 private double price; // 数量 private int bookCount; // 作者 private String author; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getBookCount() { return bookCount; } public void setBookCount(int bookCount) { this.bookCount = bookCount; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }

3、Servlet类

1)、FindServlet.java

package example.servlet.book; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; 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 example.bean.book.Book; /** * Servlet implementation class FindServlet */ public class FindServlet 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 { try { // 加载数据库驱动,注册到驱动管理器 Class.forName("com.mysql.jdbc.Driver"); // 数据库连接字符串 String url = "jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=utf-8"; // 数据库用户名 String username = "root"; // 数据库密码 String password = ""; // 创建Connection连接 Connection conn = DriverManager.getConnection(url, username, password); // 添加图书信息的SQL语句 String sql = "select * from tb_books"; // 获取Statement Statement statement = conn.createStatement(); ResultSet resultSet = statement.executeQuery(sql); List<Book> list = new ArrayList<Book>(); while (resultSet.next()) { Book book = new Book(); book.setId(resultSet.getInt("id")); book.setName(resultSet.getString("name")); book.setPrice(resultSet.getDouble("price")); book.setBookCount(resultSet.getInt("bookCount")); book.setAuthor(resultSet.getString("author")); list.add(book); } request.setAttribute("list", list); resultSet.close(); statement.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } request.getRequestDispatcher("book_list.jsp") .forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }

2)、UpdateServlet.java类

package example.servlet.book; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class UpdateServlet */ public class UpdateServlet 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")); int bookCount = Integer.valueOf(request.getParameter("bookCount")); 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 = "update tb_books set bookcount=? where id=?"; // 获取PreparedStatement PreparedStatement ps = conn.prepareStatement(sql); // 对SQL语句中的第一个参数赋值 ps.setInt(1, bookCount); // 对SQL语句中的第二个参数赋值 ps.setInt(2, id); // 执行更新操作 ps.executeUpdate(); // 关闭PreparedStatement ps.close(); // 关闭Connection conn.close(); } 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 { // TODO Auto-generated method stub doGet(request, response); } }

3)、DeleteServlet类

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

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