IDEA+Java+JSP+Mysql+Tomcat实现Web图书管理系统-添加管理员增删改查功能(1)
2401_84102720 2024-06-23 14:33:01 阅读 74
更多:Java进阶核心知识集
包含:JVM,JAVA集合,网络,JAVA多线程并发,JAVA基础,Spring原理,微服务,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存等等
高效学习视频
本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录
需要这份系统化的资料的朋友,可以点击这里获取
#before{
text-align: center;
}
#head{
background: #eeeeee;height: 80px;
}
#headLink{
font-size: 20px;
}
#headWelLink{
font-size: 20px;
}
<%–头部信息–%>
<%
User user =(User)session.getAttribute(“user”);
if(user == null){
response.sendRedirect(“login.jsp”);
}else {
%>
安全退出
<%
}
%>
<%
// 设置获取注册时的编码为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 {
%>
安全退出
<%
}
%>
<%
// 设置获取注册时的编码为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());
}
/**
总结
就写到这了,也算是给这段时间的面试做一个总结,查漏补缺,祝自己好运吧,也希望正在求职或者打算跳槽的 程序员看到这个文章能有一点点帮助或收获,我就心满意足了。多思考,多问为什么。希望小伙伴们早点收到满意的offer! 越努力越幸运!
金九银十已经过了,就目前国内的面试模式来讲,在面试前积极的准备面试,复习整个 Java 知识体系将变得非常重要,可以很负责任的说一句,复习准备的是否充分,将直接影响你入职的成功率。但很多小伙伴却苦于没有合适的资料来回顾整个 Java 知识体系,或者有的小伙伴可能都不知道该从哪里开始复习。我偶然得到一份整理的资料,不论是从整个 Java 知识体系,还是从面试的角度来看,都是一份含技术量很高的资料。
本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录
需要这份系统化的资料的朋友,可以点击这里获取
Object>();
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());
}
/**
总结
就写到这了,也算是给这段时间的面试做一个总结,查漏补缺,祝自己好运吧,也希望正在求职或者打算跳槽的 程序员看到这个文章能有一点点帮助或收获,我就心满意足了。多思考,多问为什么。希望小伙伴们早点收到满意的offer! 越努力越幸运!
金九银十已经过了,就目前国内的面试模式来讲,在面试前积极的准备面试,复习整个 Java 知识体系将变得非常重要,可以很负责任的说一句,复习准备的是否充分,将直接影响你入职的成功率。但很多小伙伴却苦于没有合适的资料来回顾整个 Java 知识体系,或者有的小伙伴可能都不知道该从哪里开始复习。我偶然得到一份整理的资料,不论是从整个 Java 知识体系,还是从面试的角度来看,都是一份含技术量很高的资料。
[外链图片转存中…(img-0a6aTBkj-1715284869159)]
本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录
需要这份系统化的资料的朋友,可以点击这里获取
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。