图书管理系统
1.增删改查
2.设计数据库
3.使用servlet+jdbc+el+jstl+jsp
1)设计数据库
id bname author price author date
2)创建web工程,导入相关jar包
3)编写servlet类 并且在web.xml文件中取配置
4)使用jdbc工具类 连接数据库 并且要查询数据库里面的数据 显示在列表界面上
5)完成添加功能 点击添加按钮 去往添加界面 提交数据 保存到数据库 并且显示出来
package com.bky.dto;
public class Book {
private Integer bid;
private String bname;
private double price;
private String author;
private String date;
public Book(Integer bid, String bname, double price, String author, String date) {
this.bid = bid;
this.bname = bname;
this.price = price;
this.author = author;
this.date = date;
}
public Book() {
}
public Integer getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "Book{" +
"bid=" + bid +
", bname='" + bname + '\'' +
", price=" + price +
", author='" + author + '\'' +
", date='" + date + '\'' +
'}';
}
}
User.java
package com.bky.dto;
public class User {
private Integer id;
private String userName;
private String passWord;
public User(Integer id, String userName, String passWord) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
'}';
}
}
BooksServlet .java
package com.bky.servlet;
import com.bky.dto.Book;
import com.bky.dto.User;
import com.bky.utils.JDBCUtils;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
public class BooksServlet extends HttpServlet {
public ArrayList<Book> getJDBC(){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
ArrayList<Book> books = new ArrayList<Book>();
try {
//获取数据库连接对象
connection = JDBCUtils.getConnection();
System.out.println(connection);
//获取数据库操作对象 ,预编译sql
String sql="select *from t_book";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet!=null&&resultSet.next()){
int bid = resultSet.getInt("id");
String bname = resultSet.getString("bname");
Double price = resultSet.getDouble("price");
String author = resultSet.getString("author");
String bdate = resultSet.getString("bdate");
Book book = new Book();
book.setBid(bid);
book.setBname(bname);
book.setPrice(price);
book.setAuthor(author);
book.setDate(bdate);
books.add(book);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.getClose(connection,preparedStatement,resultSet);
}
return books;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置字符集编码,防止乱码
req.setCharacterEncoding("utf-8"); //代码
resp.setContentType("text/html;charset=utf-8"); //网页
String method = req.getParameter("method");
if(method!=null&&method.equals("list")){
getList(req,resp);
}else if (method!=null && method.equals("add")){
getAdd(req,resp);
}else if (method!=null && method.equals("delete")){
getDelete(req,resp);
}else if (method!=null && method.equals("toupdate")){
toUpdate(req,resp);
}else if (method!=null && method.equals("update")) {
getUpdate(req, resp);
}else if (method!=null && method.equals("login")) {
getLogin(req, resp);
}else if (method!=null && method.equals("logout")) {
getLogout(req, resp);
}
}
private void getLogout(HttpServletRequest req, HttpServletResponse resp) {
HttpSession session = req.getSession();
User user = (User)session.getAttribute("user");
//方法一、session.setAttribute("user",null);
session.invalidate();//方法二、推荐
try {
resp.sendRedirect("http://localhost:8899/BooksServlet?method=list");
} catch (IOException e) {
e.printStackTrace();
}
}
private void getLogin(HttpServletRequest req, HttpServletResponse resp) {
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
String username = req.getParameter("username");
String passward = req.getParameter("passward");
try {
connection = JDBCUtils.getConnection();
String sql="select * from tb_user where username = ? and passward = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,username);
preparedStatement.setString(2,passward);
resultSet = preparedStatement.executeQuery();
if (resultSet != null && resultSet.next()){
User user = new User();
user.setUserName(username);
user.setPassWord(passward);
HttpSession session = req.getSession();
session.setAttribute("user",user);
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/jsp/toList.jsp");
requestDispatcher.forward(req,resp);
System.out.println("登录成功");
}else {
System.out.println("登录失败");
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/jsp/login.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.getClose(connection,preparedStatement,resultSet);
}
}
private void toUpdate(HttpServletRequest req, HttpServletResponse resp) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String bid = req.getParameter("bid");
try {
connection = JDBCUtils.getConnection();
String sql="select * from t_book where id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,Integer.parseInt(bid));
resultSet = preparedStatement.executeQuery();
if (resultSet!=null && resultSet.next()){
int id = resultSet.getInt("id");
String bname = resultSet.getString("bname");
Double price = resultSet.getDouble("price");
String author = resultSet.getString("author");
String bdate = resultSet.getString("bdate");
Book book = new Book();
book.setBname(bname);
book.setBid(id);
book.setDate(bdate);
book.setAuthor(author);
book.setPrice(price);
HttpSession session = req.getSession();
session.setAttribute("book",book);
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/jsp/update.jsp");
try {
requestDispatcher.forward(req,resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.getClose(connection,preparedStatement,resultSet);
}
}
private void getUpdate(HttpServletRequest req, HttpServletResponse resp) {
try {
req.setCharacterEncoding("utf-8"); //代码
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
resp.setContentType("text/html;charset=utf-8");
String bname = req.getParameter("bname");
String author = req.getParameter("author");
String price = req.getParameter("price");
String date = req.getParameter("date");
String id = req.getParameter("id");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getConnection();
String sql = "update t_book set bname = ?,price = ?,author = ?,bdate = ? where id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,bname);
preparedStatement.setDouble(2,Double.valueOf(price));
preparedStatement.setString(3,author);
preparedStatement.setString(4,date);
preparedStatement.setInt(5,Integer.parseInt(id));
int i = preparedStatement.executeUpdate();
if (i > 0){
System.out.println("修改成功");
resp.sendRedirect("http://localhost:8899/BooksServlet?method=list");
}else {
System.out.println("修改失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.getClose(connection,preparedStatement,resultSet);
}
}
private void getDelete(HttpServletRequest req, HttpServletResponse resp) {
Connection connection=null;
PreparedStatement preparedStatement=null;
String bid = req.getParameter("bid");
try {
connection = JDBCUtils.getConnection();
String sql="delete from t_book where id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, Integer.parseInt(bid));
int i = preparedStatement.executeUpdate();
if(i>0){
System.out.println("删除成功");
resp.sendRedirect("http://localhost:8899/BooksServlet?method=list");
}
else {
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.getClose(connection,preparedStatement,null);
}
}
private void getAdd(HttpServletRequest req, HttpServletResponse resp) {
Connection connection=null;
PreparedStatement preparedStatement=null;
String bname = req.getParameter("bname");
String author = req.getParameter("author");
String price = req.getParameter("price");
String date = req.getParameter("date");
try {
connection = JDBCUtils.getConnection();
String sql="insert into t_book(bname,price,author,bdate)values(?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,bname);
preparedStatement.setDouble(2,Double.valueOf(price));
preparedStatement.setString(3,author);
preparedStatement.setString(4,date);
int i = preparedStatement.executeUpdate();
if(i>0){
System.out.println("添加成功");
try {
resp.sendRedirect("http://localhost:8899/BooksServlet?method=list");
} catch (IOException e) {
e.printStackTrace();
}
}
else {
System.out.println("添加失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.getClose(connection,preparedStatement,null);
}
}
private void getList(HttpServletRequest req, HttpServletResponse resp) {
//1. 获取数据库对象
ArrayList<Book> books = getJDBC();
//2.获取域对象
HttpSession session = req.getSession();
//3.存值
session.setAttribute("books",books);
//4.转发
RequestDispatcher requestDispatcher = req.getRequestDispatcher("jsp/list.jsp");
//走向具体的类
try {
requestDispatcher.forward(req,resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
package com.bky.utils;
import java.sql.*;
import java.util.Arrays;
//工具类不能实例化对象,都是静态方法,直接调用
public class JDBCUtils {
//私有化构造方法,不能注册对象
private JDBCUtils(){
}
//注册驱动
static {
try {
Class.forName("com.mysql.jdbc.Driver");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/test01?characterEncoding=utf-8","root","root");
}
//关闭资源
public static void getClose(Connection connection,Statement statement,ResultSet resultSet){
try{
if(statement!=null)
{
statement.close();
}
if(resultSet!=null)
{
resultSet.close();
}
if(connection!=null)
{
connection.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加界面</title>
</head>
<body>
<h3>欢迎来到添加界面</h3>
<form action="http://localhost:8899/BooksServlet?method=add" method="post">
<table border="1px" bgcolor="#d8bfd8">
<tr>
<th>图书名称</th>
<td><input type="text" name="bname"></td>
</tr>
<tr>
<th>作者</th>
<td><input type="text" name="author"></td>
</tr>
<tr>
<th>价格</th>
<td><input type="text" name="price"></td>
</tr>
<tr>
<th>发行日期</th>
<td><input type="text" name="date"></td>
</tr>
<tr>
<td colspan="6" align="center">
<input type="submit" value="保存">
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>列表界面</title>
</head>
<body>
<h2>欢迎${
user.userName}来到列表界面</h2>
<c:if test="${empty user}">
<a href="http://localhost:8899/jsp/login.jsp">您还没有登录请前往登陆界面</a>
</c:if>
<c:if test="${not empty user}">
<table border="1px" bgcolor="#d8bfd8">
<tr>
<td colspan="6" align="center">
<a href="http://localhost:8899/jsp/add.jsp">添加</a>
<a href="http://localhost:8899/BooksServlet?method=logout">注销</a>
</td>
</tr>
<tr>
<th>图书名称</th>
<th>价格</th>
<th>作者</th>
<th>发行日期</th>
<th>操作</th>
</tr>
<c:forEach var="book" items="${books}">
<tr>
<td>${
book.bname}</td>
<td>${
book.price}</td>
<td>${
book.author}</td>
<td>${
book.date}</td>
<td>
<a href="http://localhost:8899/BooksServlet?method=delete&bid=${book.bid}">删除</a>,
<a href="http://localhost:8899/BooksServlet?method=toupdate">修改</a>
</td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>列表界面</title>
</head>
<body>
<h2>欢迎${
user.userName}来到列表界面</h2>
<c:if test="${empty user}">
<a href="http://localhost:8899/jsp/login.jsp">您还没有登录请前往登陆界面</a>
</c:if>
<c:if test="${not empty user}">
<table border="1px" bgcolor="#d8bfd8">
<tr>
<td colspan="6" align="center">
<a href="http://localhost:8899/jsp/add.jsp">添加</a>
<a href="http://localhost:8899/BooksServlet?method=logout">注销</a>
</td>
</tr>
<tr>
<th>图书名称</th>
<th>价格</th>
<th>作者</th>
<th>发行日期</th>
<th>操作</th>
</tr>
<c:forEach var="book" items="${books}">
<tr>
<td>${
book.bname}</td>
<td>${
book.price}</td>
<td>${
book.author}</td>
<td>${
book.date}</td>
<td>
<a href="http://localhost:8899/BooksServlet?method=delete&bid=${book.bid}">删除</a>,
<a href="http://localhost:8899/BooksServlet?method=toupdate">修改</a>
</td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>访问列表界面</title>
</head>
<body>
<span>欢迎${
user.userName}!!</span><br/>
<a href="http://localhost:8899/BooksServlet?method=list">访问列表界面</a>
</body>
</html>
update.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>修改界面</title>
</head>
<body>
<h3>欢迎来到修改界面</h3>
<form action="http://localhost:8899/BooksServlet?method=update" method="post">
<input type="hidden" name="id" value="${book.bid}"/>
<table border="1px" bgcolor="#d8bfd8">
<tr>
<th>图书名称</th>
<td><input type="text" name="bname" value="${book.bname}"></td>
</tr>
<tr>
<th>作者</th>
<td><input type="text" name="author" value="${book.author}"></td>
</tr>
<tr>
<th>价格</th>
<td><input type="text" name="price" value="${book.price}"></td>
</tr>
<tr>
<th>发行日期</th>
<td><input type="text" name="date" value="${book.date}"></td>
</tr>
<tr>
<td colspan="6" align="center">
<input type="submit" value="修改">
</td>
</tr>
</table>
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>BooksServlet</servlet-name>
<servlet-class>com.bky.servlet.BooksServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BooksServlet</servlet-name>
<url-pattern>/BooksServlet</url-pattern>
</servlet-mapping>
</web-app>