0


IDEA+Java+JSP+Mysql+Tomcat实现Web图书管理系统-添加管理员增删改查功能

图书名称:

图书价格:

图书作者:

出版公司:

添加

重置

admin_delete.jsp

<%@ page import=“com.sjsq.po.Book” %>

<%@ page import=“com.sjsq.dao.impl.BookDaoImpl” %>

<%@ page import=“com.sjsq.dao.BookDao” %>

<%@ page import=“com.sjsq.po.User” %><%–

Created by IntelliJ IDEA.

User: shuijianshiqing

Date: 2020/6/10

Time: 22:45

To change this template use File | Settings | File Templates.

–%>

<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
删除图书
<%–头部信息–%>

<%

User user =(User)session.getAttribute(“user”);

if(user == null){

response.sendRedirect(“login.jsp”);

}else {

%>
欢迎您:<%=user.getName()%>

安全退出

<%

}

%>

<%

// 设置获取注册时的编码为UTF-8

request.setCharacterEncoding(“UTF-8”);

//获取admin.jsp页面的bookid

Integer bookid=Integer.parseInt(request.getParameter(“bookid”));

//引入数据交互层

BookDao dao=new BookDaoImpl();

Book book=new Book();

book = dao.getBook(bookid);

System.out.println(“删除的图书信息:”);

System.out.println(book);

boolean flag=dao.deleteBook(bookid);

if(flag){

response.sendRedirect(“admin.jsp”);

}else{

response.sendRedirect(“error.jsp”);

}

%>

admin_detail.jsp

<%–

Created by IntelliJ IDEA.

User: shuijianshiqing

Date: 2020/5/24

Time: 10:51

To change this template use File | Settings | File Templates.

–%>

<%@ page contentType=“text/html;charset=UTF-8” language=“java” pageEncoding=“UTF-8” %>

<%@ page import=“com.sjsq.po.Book” %>

<%@ page import=“com.sjsq.po.User” %>

<%

// 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错

String path = request.getContextPath();

String basePath = request.getScheme() + “😕/” + request.getServerName() + “:”

  • request.getServerPort() + path + “/”;

%>
图书详情
<%–头部信息–%>

<%

User user =(User)session.getAttribute(“user”);

if(user == null){

response.sendRedirect(“login.jsp”);

}else {

%>
欢迎您:<%=user.getName()%>

安全退出

<%

}

%>

图书详情

返回上一级

<%

Book book = (Book)session.getAttribute(“book”);

%>
图书编号 图书名称 图书价格 图书作者 图书封皮 图书出版社 <%=book.getBookid()%> <%=book.getBookname()%> <%=book.getPrice()%> <%=book.getAuthor()%> <%=book.getPicture()%> <%=book.getPublish()%>

图书列表

admin_do_addbook.jsp

<%–

Created by IntelliJ IDEA.

User: shuijianshiqing

Date: 2020/5/21

Time: 23:45

To change this template use File | Settings | File Templates.

–%>

<%@page import=“com.sjsq.dao.impl.UserDaoImpl”%>

<%@page import=“com.sjsq.dao.UserDao”%>

<%@page import=“com.sjsq.po.User”%>

<%@ page import=“com.sjsq.po.Book” %>

<%@ page import=“com.sjsq.dao.BookDao” %>

<%@ page import=“com.sjsq.dao.impl.BookDaoImpl” %>

<%@ page contentType=“text/html;charset=UTF-8” language=“java” pageEncoding=“UTF-8”%>
处理新增图书
<%–头部信息–%>

<%

User user =(User)session.getAttribute(“user”);

if(user == null){

response.sendRedirect(“login.jsp”);

}else {

%>
欢迎您:<%=user.getName()%>

安全退出

<%

}

%>

<%

// 设置获取注册时的编码为UTF-8

request.setCharacterEncoding(“UTF-8”);

Book book=new Book();

//获取admin_addbook.jsp页面提交的账号和密码

String bookname=request.getParameter(“bookname”);

Double price= Double.parseDouble(request.getParameter(“price”));

String author=request.getParameter(“author”);

String publish=request.getParameter(“publish”);

//获取admin_addbook.jsp页面提交的账号和密码设置到实体类User中

book.setBookname(bookname);

book.setPrice(price);

book.setAuthor(author);

book.setPublish(publish);

System.out.println(“添加的图书信息”);

System.out.println(book);

//引入数据交互层

BookDao dao=new BookDaoImpl();

boolean flag=dao.addBook(book);

if(flag){

response.sendRedirect(“admin.jsp”);

}else{

response.sendRedirect(“error.jsp”);

}

%>

admin_do_info.jsp

<%–

Created by IntelliJ IDEA.

User: shuijianshiqing

Date: 2020/5/24

Time: 10:51

To change this template use File | Settings | File Templates.

–%>

<%@ page contentType=“text/html;charset=UTF-8” language=“java” pageEncoding=“UTF-8” %>

<%@ page import=“com.sjsq.po.Book” %>

<%@ page import=“com.sjsq.service.BookService” %>

<%@ page import=“com.sjsq.service.impl.BookServiceImpl” %>

<%

// 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错

String path = request.getContextPath();

String basePath = request.getScheme() + “😕/” + request.getServerName() + “:”

  • request.getServerPort() + path + “/”;

%>
书籍信息
<%

Book book = new Book();

String sid = request.getParameter(“bookid”);

Integer id = Integer.parseInt(sid);

BookService service = new BookServiceImpl();

book.setBookid(id);

Book bookCur = service.getBook(book);

// 控制台打印出类的信息(日志的前身)

System.out.print(“admin_do_info.jsp的信息–>”);

System.out.println(bookCur);

session.setAttribute(“book”, bookCur);

response.sendRedirect(“admin_detail.jsp”);

%>

admin_do_updatebook.jsp

<%–

Created by IntelliJ IDEA.

User: shuijianshiqing

Date: 2020/5/21

Time: 23:45

To change this template use File | Settings | File Templates.

–%>

<%@page import=“com.sjsq.dao.impl.UserDaoImpl”%>

<%@page import=“com.sjsq.dao.UserDao”%>

<%@page import=“com.sjsq.po.User”%>

<%@ page import=“com.sjsq.po.Book” %>

<%@ page import=“com.sjsq.dao.BookDao” %>

<%@ page import=“com.sjsq.dao.impl.BookDaoImpl” %>

<%@ page contentType=“text/html;charset=UTF-8” language=“java” pageEncoding=“UTF-8”%>
处理修改图书
<%–头部信息–%>

<%

User user =(User)session.getAttribute(“user”);

if(user == null){

response.sendRedirect(“login.jsp”);

}else {

%>
欢迎您:<%=user.getName()%>

安全退出

<%

}

%>

<%

// 设置获取注册时的编码为UTF-8

request.setCharacterEncoding(“UTF-8”);

//获取admin_addbook.jsp页面提交的账号和密码

Integer bookid=Integer.parseInt(request.getParameter(“bookid”));

String bookname = request.getParameter(“bookname”);

Double price = Double.parseDouble(request.getParameter(“price”));

String author=request.getParameter(“author”);

String publish=request.getParameter(“publish”);

//引入数据交互层

BookDao dao=new BookDaoImpl();

Book book=new Book();

book = dao.getBook(bookid);

book.setBookname(bookname);

book.setPrice(price);

book.setAuthor(author);

book.setPublish(publish);

System.out.println(“修改的图书信息”);

System.out.println(book);

boolean flag=dao.updateBook(book);

if(flag){

response.sendRedirect(“admin.jsp”);

}else{

response.sendRedirect(“error.jsp”);

}

%>

admin_update.jsp

<%@ page import=“com.sjsq.po.Book” %>

<%@ page import=“com.sjsq.dao.impl.BookDaoImpl” %>

<%@ page import=“com.sjsq.dao.BookDao” %>

<%@ page import=“com.sjsq.po.User” %><%–

Created by IntelliJ IDEA.

User: shuijianshiqing

Date: 2020/5/21

Time: 23:14

To change this template use File | Settings | File Templates.

–%>

<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8” %>
修改图书
<%–头部信息–%>

<%

User user =(User)session.getAttribute(“user”);

if(user == null){

response.sendRedirect(“login.jsp”);

}

%>

修改图书


<%

//获取admin.jsp页面的bookid

Integer bookid=Integer.parseInt(request.getParameter(“bookid”));

BookDao dao=new BookDaoImpl();

Book book=new Book();

book = dao.getBook(bookid);

%>

图书名称:

图书价格:

图书作者:

出版公司:

修改

重置

error.jsp

<%–

Created by IntelliJ IDEA.

User: shuijianshiqing

Date: 2020/6/10

Time: 22:28

To change this template use File | Settings | File Templates.

–%>

<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
错误页面
操作出错,请回到上一个页面!

login.jsp

<%–

Created by IntelliJ IDEA.

User: shuijianshiqing

Date: 2020/5/19

Time: 22:44

To change this template use File | Settings | File Templates.

–%>

<%@ page contentType=“text/html;charset=UTF-8” language=“java” pageEncoding=“UTF-8” %>

<%

// 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错

String path = request.getContextPath();

String basePath = request.getScheme() + “😕/” + request.getServerName() + “:”

  • request.getServerPort() + path + “/”;

%>
用户登录

用户登录

---正在美化中---

账号: 密码:
找回密码

注册

2.后端


User

package com.sjsq.po;

import java.io.Serializable;

/**

  • @author shuijianshiqing
  • @date 2020/5/19 22:19
  • 用户的实体类

*/

public class User implements Serializable {

// 增加序列号,作用是反序列化的时候不会报错,切能进行IO的持久化

public static final long serialVersionUID = 1L;

private Integer id;

private String name;

private String password;

private String email;

private String phone;

private Integer isadmin;

public static long getSerialVersionUID() {

return serialVersionUID;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

public Integer getIsadmin() {

return isadmin;

}

public void setIsadmin(Integer isadmin) {

this.isadmin = isadmin;

}

@Override

public String toString() {

return “User{” +

“id=” + id +

“, name='” + name + ‘’’ +

“, password='” + password + ‘’’ +

“, email='” + email + ‘’’ +

“, phone='” + phone + ‘’’ +

“, isadmin=” + isadmin +

‘}’;

}

}

UserDaoImpl

package com.sjsq.dao.impl;

import com.sjsq.dao.UserDao;

import com.sjsq.po.User;

import com.sjsq.utils.BaseDao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

/**

  • @author shuijianshiqing
  • @date 2020/5/20 22:15
  • 实现UserDao接口

*/

public class UserDaoImpl implements UserDao {

/**

  • 用户登录
  • @param user
  • @return

*/

@Override

public User login(User user) {

Connection con = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

// 1.获取数据库连接

con = BaseDao.getConnection();

// 2.写sql

String sql = “select * from user where name = ? and password = ?”;

// 3.预编译

ps = con.prepareStatement(sql);

// 4.设置值

ps.setObject(1,user.getName());

ps.setObject(2,user.getPassword());

rs = ps.executeQuery();

User users = null;

if(rs.next()){

users = new User();

// 从数据库中获取值到实体类的setter方法中

users.setId(rs.getInt(“id”));

users.setName(rs.getString(“name”));

users.setPassword(rs.getString(“password”));

users.setEmail(rs.getString(“email”));

users.setPhone(rs.getString(“phone”));

users.setIsadmin(rs.getInt(“isadmin”));

// 返回的是你查询出来的完整的对象

return users;

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}finally {

// 关闭资源,避免出现异常

BaseDao.close(con,ps,rs);

}

return null;

}

/**

  • 用户注册
  • @param user
  • @return

*/

@Override

public boolean register(User user) {

String sql = “insert into user values (0,?,?,?,?,0)”;

List list = new ArrayList();

list.add(user.getName());

list.add(user.getPassword());

list.add(user.getEmail());

list.add(user.getPhone());

boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());

if(flag){

return true;

}else {

return false;

}

}

/**

  • 查询用户信息
  • @param sql
  • @param arr
  • @return

*/

@Override

public List selectUser(String sql, Object[] arr) {

Connection con = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

// 1.连接数据库

con = BaseDao.getConnection();

// 2.预编译

ps = con.prepareStatement(sql);

if(arr != null){

for (int i = 0; i < arr.length; i++) {

ps.setObject(i+1,arr[i]);

}

}

// 3.执行sql

rs = ps.executeQuery();

// 4.保存查询出来的数据到list

List list = new ArrayList();

while(rs.next()){

User user = new User();

// 从数据库中获取值到实体类的setter方法中

user.setId(rs.getInt(“id”));

user.setName(rs.getString(“name”));

user.setPassword(rs.getString(“password”));

user.setEmail(rs.getString(“email”));

user.setPhone(rs.getString(“phone”));

user.setIsadmin(rs.getInt(“isadmin”));

// 测试数据

System.out.println(“Dao层测试日志,显示用户信息:”);

System.out.println(user);

// 将对象保存到list中

list.add(user);

}

return list;

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}finally {

// 关闭资源,避免出现异常

BaseDao.close(con,ps,rs);

}

return null;

}

}

BookDao

package com.sjsq.dao;

import com.sjsq.po.Book;

import java.util.List;

/**

  • @author shuijianshiqing
  • @date 2020/5/20 23:13
  • 图书信息接口

*/

public interface BookDao {

/**

  • 查询图书信息
  • @param sql
  • @param arr
  • @return

*/

public List select(String sql,Object[] arr);

/**

  • 根据图书编号进行查询
  • @param id
  • @return

*/

public Book getBook(Integer id);

/**

  • 图书新增
  • @param book
  • @return

*/

public boolean addBook(Book book);

/**

  • 图书修改
  • @param book
  • @return

*/

public boolean updateBook(Book book);

/**

  • 删除图书
  • @param bookid
  • @return

*/

public boolean deleteBook(Integer bookid);

}

BookDaoImpl

package com.sjsq.dao.impl;

import com.sjsq.dao.BookDao;

import com.sjsq.po.Book;

import com.sjsq.utils.BaseDao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

/**

  • @author shuijianshiqing
  • @date 2020/5/20 23:15

*/

public class BookDaoImpl implements BookDao {

@Override

public List select(String sql, Object[] arr) {

Connection con = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

// 1.连接数据库

con = BaseDao.getConnection();

// 2.预编译

ps = con.prepareStatement(sql);

if(arr != null){

for (int i = 0; i < arr.length; i++) {

// 传入sql的参数

ps.setObject(i+1,arr[i]);

}

}

// 3.执行sql

rs = ps.executeQuery();

// 4.保存查询出来的数据到list

List list = new ArrayList();

while (rs.next()){

Book book = new Book();

book.setBookid(rs.getInt(“bookid”));

book.setBookname(rs.getString(“bookname”));

book.setPrice(rs.getDouble(“price”));

book.setAuthor(rs.getString(“author”));

book.setPicture(rs.getString(“picture”));

book.setPublish(rs.getString(“publish”));

list.add(book);

}

return list;

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}finally {

// 关闭资源,避免出现异常

BaseDao.close(con,ps,rs);

}

return null;

}

@Override

public Book getBook(Integer id) {

Connection con = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

// 1.连接数据库

con = BaseDao.getConnection();

// 2.预编译

String sql = “select * from book where bookid = ?”;

ps = con.prepareStatement(sql);

ps.setInt(1,id);

// 3.执行sql

rs = ps.executeQuery();

while (rs.next()){

Book book = new Book();

book.setBookid(rs.getInt(“bookid”));

book.setBookname(rs.getString(“bookname”));

book.setPrice(rs.getDouble(“price”));

book.setAuthor(rs.getString(“author”));

book.setPicture(rs.getString(“picture”));

book.setPublish(rs.getString(“publish”));

return book;

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}finally {

// 关闭资源,避免出现异常

BaseDao.close(con,ps,rs);

}

return null;

}

/**

  • 新增图书
  • @param book
  • @return

*/

@Override

public boolean addBook(Book book) {

String sql = “insert into book values (0,?,?,?,null,?)”;

List list = new ArrayList();

list.add(book.getBookname());

list.add(book.getPrice());

list.add(book.getAuthor());

list.add(book.getPublish());

boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());

if(flag){

return true;

}else {

return false;

}

}

/**

  • 更新图书
  • @param book
  • @return

*/

@Override

public boolean updateBook(Book book) {

String sql = “update book set bookname=?,price=?,author=?,publish=? where bookid=?”;

List list = new ArrayList();

list.add(book.getBookname());

list.add(book.getPrice());

list.add(book.getAuthor());

list.add(book.getPublish());

list.add(book.getBookid());

boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());

if(flag){

return true;

}else {

return false;

}

}

/**

  • 删除图书
  • @param bookid
  • @return

*/

@Override

public boolean deleteBook(Integer bookid) {

String sql = “delete from book where bookid=?”;

List list = new ArrayList();

list.add(bookid);

boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());

if(flag){

return true;

}else {

return false;

}

}

}

BookServiceImpl

package com.sjsq.service.impl;

import com.sjsq.dao.BookDao;

import com.sjsq.dao.impl.BookDaoImpl;

import com.sjsq.po.Book;

import com.sjsq.service.BookService;

import java.util.ArrayList;

import java.util.List;

/**

  • @author shuijianshiqing
  • @date 2020/5/20 23:42
  • Book的Service实现层

*/

public class BookServiceImpl implements BookService {

private BookDao dao = new BookDaoImpl();

/**

  • 查询所有图书信息
  • @param book
  • @return

*/

@Override

public List select(Book book) {

StringBuffer sql = new StringBuffer("select * from book where 1 = 1 ");

List list = new ArrayList();

if(book != null){

// 根据bookid来查找对应的书籍

if(book.getBookid() != null && book.getBookid() != 0){

sql.append(" and bookid = ?");

list.add(book.getBookid());

}

// 根据bookname来查找对应的书籍

if(book.getBookname() != null){

System.out.println(“图书名称:”+book.getBookname());

sql.append(" and bookname = ?");

list.add(book.getBookname());

}

}

return dao.select(sql.toString(),list.toArray());

}

/**

  • 根据图书编号查询信息
  • @param book
  • @return

*/

@Override

public Book getBook(Book book) {

if(book.getBookid() != null && book.getBookid() != 0){

return dao.getBook(book.getBookid());

}

return null;

}

}

四、感悟及商务合作

========================================================================

实现管理增删改查用了一晚上时间,感觉代码越写越顺了。

1.项目相关


备注1:这个项目是基于上个项目写的,上个项目地址:

IDEA+Java+JSP+Mysql+Tomcat实现Web图书管理系统

备注2:要是不知道IDEA如何搭建WEB项目,请移步:

IDEA创建Web项目详细说明

备注3:要是您需要学生成绩管理系统(界面较友好),请移步:

学生成绩管理系统

备注4:要是侵犯到您的权益,请通知我删除此文章。

五、其他

===================================================================

1.其他系统实现


JavaWeb系统系列实现

Java+JSP实现学生图书管理系统

Java+JSP实现学生信息管理系统

Java+JSP实现用户信息管理系统

Java+Servlet+JSP实现航空订票系统

Java+Servlet+JSP实现房屋租赁管理系统


本文转载自: https://blog.csdn.net/omofun5541/article/details/144108098
版权归原作者 omofun5541 所有, 如有侵权,请联系我们删除。

“IDEA+Java+JSP+Mysql+Tomcat实现Web图书管理系统-添加管理员增删改查功能”的评论:

还没有评论