基于javaEE的图书管理系统

极简的图书管理系统,无任何样式修饰,适合新手练手(图文并释)
1、实现了用户注册、登录,图书的添加、修改、删除和修改操作
2、工具需要:eclipse、mysql、Tomcat
3、做系统之前在eclipse需要配置Tomcat服务器和导入mysql连接数据库的jar包
4、数据库中建一个user表存用户信息,一个book表存图书信息

一、各种页面
index.html文件,系统运行的首页,有登录、注册选项





图书系统首页






register.html文件,用户注册页面





注册


用户名:
密   码:
生   日:
性   别:

login.html文件,用户登录页面





登录


用户名:
密   码:

book.jsp文件,登录成功后的页面,显示用户名,有查询图书信息功能

<%@page import="(包名).User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




图书系统


<%
	User user=(User)request.getAttribute("user");//得到servlet传来的属性“user”
	String username=user.getUsername();
%>

图书系统

    用户名:<%=username %>  

bookquery.jsp文件,显示所有图书信息,可删除对应图书、修改图书信息、以及添加图书

<%@ page import="java.util.ArrayList" %>
<%@ page import="(包名).Book" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




图书查询


<%
	ArrayList booklist=(ArrayList)session.getAttribute("booklist");
%>

图书管理:

  

<% for(Book book:booklist){//遍历“booklist”属性,获取里面的值 int id=book.getId(); String name=book.getName(); String author=book.getAuthor(); String press=book.getPress(); double price=book.getPrice(); %> <%} %>
编号书名作者出版社价格
<%=id %> <%=name %> <%=author %> <%=press %> <%=price %>

update.jsp文件,更新对应图书信息页面,有限制不能更新为已有图书名以及不为null或者不为空字符串等功能

<%@page import="(包名).Book"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


<%
	Book book=(Book)session.getAttribute("book");
	int id=book.getId();
	String name=book.getName();
	String author=book.getAuthor();
	String press=book.getPress();
	double price=book.getPrice();
%>

更改信息:


name="id"> 编   号:<%=id %>
> 书   名:>
作   者:>
出版社:
价   格:>

addbook.html文件,添加图书页面,在对应的servlet中有限制了不能添加已有图书、添加信息不为null以及不为空字符串等功能





添加图书页面


添加图书:


书名:
作者:
出版社:
价格:

二、封装的数据库连接,以及数据库操作的工具类
Conn.java文件

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Conn {
	private static String url="jdbc:mysql://localhost:3306/abc?serverTimezone=Asia/Shanghai";
	private static String user="root";
	private static String password="root";
	static Connection conn=null;
	static{//静态代码块,加载类的同时实现数据库的连接
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn=DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	PreparedStatement pstmt=null;
	int num=0;
	public int update(String sql,Object[] object){//增、删、改用的方法,返回一个整型数
		try {
			pstmt=conn.prepareStatement(sql);
			for(int i=0;i

三、用于存储、传递信息的间接类
User.java文件

public class User {
	private String username;
	private String password;
	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;
	}
}

Book.java文件

public class Book {
	private int id;
	private String name;
	private String author;
	private String press;
	private double price;
	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 String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getPress() {
		return press;
	}
	public void setPress(String press) {
		this.press = press;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
}

四、用于实现功能的各种servlet
RegisterServlet.java文件,实现把用户注册信息存入数据库的功能

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html,charset=utf-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String birthday=request.getParameter("birthday");
		String gender=request.getParameter("gender");
		if(username.equals("")||username.equals(null)||password.equals("")||password.equals(null)){
			JOptionPane.showMessageDialog(null, "用户名或密码不能为空","标题【警告】" , JOptionPane.ERROR_MESSAGE);
			response.sendRedirect("register.html");
			return;
		}
		
		Conn db=new Conn();
		String sql="select * from user where username=?";
		Object[] o={username};
		ResultSet rs=db.select(sql, o);
		try {
			if(rs.next()){
				JOptionPane.showMessageDialog(null, "该用户已存在","标题【警告】" , JOptionPane.ERROR_MESSAGE);
				response.sendRedirect("register.html");
			}else{
				String sql1="insert into user(USERNAME,PASSWORD,BIRTHDAY,GENDER) values(?,?,?,?)";
				Object[] o2={username,password,birthday,gender};
				db.update(sql1, o2);
				JOptionPane.showMessageDialog(null, "注册成功,即将跳转到登录页面","标题【成功】" , JOptionPane.ERROR_MESSAGE);
				response.sendRedirect("login.html");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	}
}

LoginServlet.java文件,实现匹配数据库信息,进行登录信息的判断等功能

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html,charset=utf-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		if(username==null||username.equals("")||password==null||password.equals("")){
			JOptionPane.showMessageDialog(null, "账号或密码有误","标题【出错啦】" , JOptionPane.ERROR_MESSAGE);
			response.sendRedirect("login.html");
			return;
		}
		
		Conn db=new Conn();
		String sql="select * from user where USERNAME=? and PASSWORD=?";
		Object[] o={username,password};
		ResultSet rs=db.select(sql, o);
		try {
			if(rs.next()){
				User user=new User();
				user.setUsername(username);
				user.setPassword(password);
				request.setAttribute("user", user);
				request.getRequestDispatcher("book.jsp").forward(request, response);
			}else{
				JOptionPane.showMessageDialog(null, "账号或密码错误","标题【出错啦】" , JOptionPane.ERROR_MESSAGE);
				response.sendRedirect("login.html");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

BookServlet.java文件,实现查询出所有图书信息的功能

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/bookServlet")
public class BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html,charset=utf-8");
		Conn db=new Conn();
		String sql="select * from book";
		Object[] o={};
		ResultSet rs=db.select(sql, o);
		ArrayList booklist=new ArrayList<>();
		try {
			while(rs.next()){
				Book book=new Book();
				book.setId(rs.getInt("ID"));
				book.setName(rs.getString("NAME"));
				book.setAuthor(rs.getString("AUTHOR"));
				book.setPress(rs.getString("PRESS"));
				book.setPrice(rs.getDouble("PRICE"));
				booklist.add(book);
			}
			
			HttpSession session=request.getSession();
			session.setAttribute("booklist", booklist);
			request.getRequestDispatcher("bookquery.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

AddbookServlet.java文件,实现添加图书功能

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

@WebServlet("/addbookServlet")
public class AddbookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html,charset=utf-8");
		String name=request.getParameter("name");
		String author=request.getParameter("author");
		String press=request.getParameter("press");
		String price1=request.getParameter("price");
		if(name.equals("")||name.equals(null)||author.equals("")||author.equals(null)||press.equals("")||press.equals(null)||price1.equals("")||price1.equals(null)){
			JOptionPane.showMessageDialog(null, "输入的信息不能为空","标题【警告】" , JOptionPane.ERROR_MESSAGE);
			response.sendRedirect("addbook.html");
			return;
		}
		double price=Double.parseDouble(price1);//要通过上面的if语句确定price1不为空才能对其转型
		
		Conn db=new Conn();
		String sql="select NAME from book where NAME=?";
		Object[] o={name};
		ResultSet rs=db.select(sql, o);
		try {
			if(rs.next()){
				JOptionPane.showMessageDialog(null, "该书本已存在","标题【警告】" , JOptionPane.ERROR_MESSAGE);
				response.sendRedirect("addbook.html");
			}else{
				String sql1="insert into book(NAME,AUTHOR,PRESS,PRICE) values(?,?,?,?)";
				Object[] o1={name,author,press,price};
				int i=db.update(sql1, o1);
				if(i>0){
					JOptionPane.showMessageDialog(null, "添加成功","标题【成功】" , JOptionPane.ERROR_MESSAGE);
					response.sendRedirect("addbook.html");
				}else{
					JOptionPane.showMessageDialog(null, "添加失败","标题【失败】" , JOptionPane.ERROR_MESSAGE);
					response.sendRedirect("addbook.html");
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

DeleteServlet.java文件,实现删除指定图书的功能

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

@WebServlet("/deleteServlet")
public class DeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html,charset=utf-8");
		int id=Integer.parseInt(request.getParameter("id"));
		Conn db=new Conn();
		String sql="delete from book where ID=?";
		Object[] o={id};
		int i=db.update(sql, o);
		if(i>0){
			JOptionPane.showMessageDialog(null, "删除成功","标题【成功】" , JOptionPane.ERROR_MESSAGE);
			//request.getRequestDispatcher("bookquery.jsp").forward(request, response);
			response.sendRedirect("bookServlet");
		}else{
			JOptionPane.showMessageDialog(null, "删除失败","标题【失败】" , JOptionPane.ERROR_MESSAGE);
			response.sendRedirect("bookServlet");
		}
	}
}

UpdateServlet.java文件,实现要修改的图书信息传给update.jsp,是为了在原来的基础上修改

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/updateServlet")
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html,charset=utf-8");
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		String author=request.getParameter("author");
		String press=request.getParameter("press");
		double price=Double.parseDouble(request.getParameter("price"));

		Book book=new Book();
		book.setId(id);
		book.setName(name);
		book.setAuthor(author);
		book.setPress(press);
		book.setPrice(price);
		HttpSession session=request.getSession();
		session.setAttribute("book", book);
		request.getRequestDispatcher("update.jsp").forward(request, response);
	}
}

ConfirmUpdateServlet.java文件,实现图书的信息修改的功能

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

@WebServlet("/confirmUpdateServlet")
public class ConfirmUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html,charset=utf-8");
		int id=Integer.parseInt(request.getParameter("id"));
		String hiddenname=request.getParameter("hiddenname");
		String name=request.getParameter("name");
		String author=request.getParameter("author");
		String press=request.getParameter("press");
		double price=Double.parseDouble(request.getParameter("price"));
		if(name==""||name==null){
			JOptionPane.showMessageDialog(null, "修改后的书名不能为空,请重新修改","标题【警告】" , JOptionPane.ERROR_MESSAGE);
			request.getRequestDispatcher("bookServlet").forward(request, response);
			return;
		}
		
		Conn db=new Conn();
		String sql="select NAME from book where NAME!=?";
		Object[] o={hiddenname};
		ResultSet rs=db.select(sql, o);
		try {
			int num=0;
			while(rs.next()){
				if(name.equals(rs.getString("NAME"))){
					num=1;
				}
			}
			if(num==1){
				JOptionPane.showMessageDialog(null, "您修改后的书名已存在,请重新修改","标题【警告】" , JOptionPane.ERROR_MESSAGE);
				request.getRequestDispatcher("bookServlet").forward(request, response);
			}else{
				String sql1="update book set NAME=?,AUTHOR=?,PRESS=?,PRICE=? where ID=?";
				Object[] o1={name,author,press,price,id};
				int i=db.update(sql1, o1);
				if(i>0){
					JOptionPane.showMessageDialog(null, "修改成功,即将跳转到图书查询页面","标题【成功】" , JOptionPane.ERROR_MESSAGE);
					request.getRequestDispatcher("bookServlet").forward(request, response);
				}else{
					JOptionPane.showMessageDialog(null, "修改失败,请确认信息后重新修改","标题【失败】" , JOptionPane.ERROR_MESSAGE);
					request.getRequestDispatcher("bookServlet").forward(request, response);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}

你可能感兴趣的:(课程设计,编程基础,javaEE课程设计,图书管理系统)