极简的图书管理系统,无任何样式修饰,适合新手练手(图文并释)
1、实现了用户注册、登录,图书的添加、修改、删除和修改操作
2、工具需要:eclipse、mysql、Tomcat
3、做系统之前在eclipse需要配置Tomcat服务器和导入mysql连接数据库的jar包
4、数据库中建一个user表存用户信息,一个book表存图书信息
一、各种页面
index.html文件,系统运行的首页,有登录、注册选项
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书系统首页</title>
</head>
<body>
<a href="login.html"><button>登录</button></a>
<a href="register.html"><button>注册</button></a>
</body>
</html>
register.html文件,用户注册页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="registerServlet">
用户名:<input type="text" name="username"/><br>
密 码:<input type="password" name="password"/><br>
生 日:<input type="date" name="birthday"/><br>
性 别:<input type="radio" name="gender" value="男"/>男<input type="radio" name="gender" value="女"/>女<br>
<input type="submit" value="注册"/>
<input type="reset" value="清空"/>
</form>
<a href="index.html"><button>回首页</button></a>
</body>
</html>
login.html文件,用户登录页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="loginServlet">
用户名:<input type="text" name="username"/><br>
密 码:<input type="password" name="password"/><br>
<input type="submit" value="登录"/>
<input type="reset" value="清空"/>
</form>
<a href="index.html"><button>回首页</button></a>
</body>
</html>
book.jsp文件,登录成功后的页面,显示用户名,有查询图书信息功能
<%@page import="(包名).User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图书系统</title>
</head>
<body>
<%
User user=(User)request.getAttribute("user");//得到servlet传来的属性“user”
String username=user.getUsername();
%>
<h3>图书系统</h3> 用户名:<%=username %> <a href="index.html"><button>退出</button></a><br><br>
<form action="bookServlet">
<button>查询图书信息</button>
</form>
</body>
</html>
bookquery.jsp文件,显示所有图书信息,可删除对应图书、修改图书信息、以及添加图书
<%@ page import="java.util.ArrayList" %>
<%@ page import="(包名).Book" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图书查询</title>
</head>
<body>
<%
ArrayList<Book> booklist=(ArrayList<Book>)session.getAttribute("booklist");
%>
<h3>图书管理:</h3><a href="addbook.html"><button>添加图书</button></a> <a href="index.html"><button>退出</button></a><br><br>
<table border="1px" cellspacing="0px">
<tr><td>编号</td><td>书名</td><td>作者</td><td>出版社</td><td>价格</td></tr>
<%
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();
%>
<tr><td><%=id %></td>
<td><%=name %></td>
<td><%=author %></td>
<td><%=press %></td>
<td><%=price %></td>
<td><a href="updateServlet?id=<%=id%>&name=<%=name%>&author=<%=author%>&press=<%=press%>&price=<%=price%>"><button>修改</button></a></td>
<td><a href="deleteServlet?id=<%=id%>"><button>删除</button></a></td></tr>
<%}
%>
</table>
</body>
</html>
update.jsp文件,更新对应图书信息页面,有限制不能更新为已有图书名以及不为null或者不为空字符串等功能
<%@page import="(包名).Book"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
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();
%>
<h3>更改信息:</h3><a href="bookServlet"><button>返回上一层</button></a><br>
<form action="confirmUpdateServlet">
<input type="hidden" value=<%=id %> name="id">
编 号:<span><%=id %></span><br>
<input type="hidden" name="hiddenname" value=<%=name %>>
书 名:<input type="text" name="name" value=<%=name %>><br>
作 者:<input type="text" name="author" value=<%=author %>><br>
出版社:<select name="press">
<option value="人民教育出版社" <%="人民教育出版社".equals(press)? "selected":"" %>>人民教育出版社</option>
<option value="清华出版社" <%="清华出版社".equals(press)? "selected":"" %>>清华出版社</option>
<option value="北大出版社" <%="北大出版社".equals(press)? "selected":"" %>>北大出版社</option>
<option value="智游出版社" <%="智游出版社".equals(press)? "selected":"" %>>智游出版社</option>
</select><br>
价 格:<input type="number" name="price" value=<%=price %>><br>
<input type="submit" value="确认更改">
</form>
<a href="bookServlet"><button>取消修改</button></a>
</body>
</html>
addbook.html文件,添加图书页面,在对应的servlet中有限制了不能添加已有图书、添加信息不为null以及不为空字符串等功能
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加图书页面</title>
</head>
<body>
<h3>添加图书:</h3><a href="bookServlet"><button>返回上一层</button></a><br>
<form action="addbookServlet"><!-- action是把数据上传到servlet的行为 -->
书名:<input type="text" name="name"/><br>
作者:<input type="text" name="author"/><br>
出版社:<select name="press">
<option value="人民教育出版社">人民教育出版社</option>
<option value="清华出版社">清华出版社</option>
<option value="北大出版社">北大出版社</option>
<option value="智游出版社">智游出版社</option>
</select><br>
价格:<input type="number" name="price"/><br>
<input type="submit" value="确认添加"/>
</form>
</body>
</html>
二、封装的数据库连接,以及数据库操作的工具类
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<object.length;i++){
pstmt.setObject(i+1, object[i]);
}
num=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return num;
}
ResultSet rs=null;
public ResultSet select(String sql,Object[] object){//查询用的方法,返回一个查询到的结果集
try {
pstmt=conn.prepareStatement(sql);
for(int i=0;i<object.length;i++){
pstmt.setObject(i+1,object[i]);
}
rs=pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public void close(){
if(pstmt!=null){
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
三、用于存储、传递信息的间接类
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<Book> 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();
}
}
}