Java课程设计(Jsp+Servlet+Mysql+Tomcat+Eclipse)
项目类型:JAVA WEB项目
用户类型:2个角色(管理员+学生)
主要技术:Jsp+MySQL+Servlet
开发工具:Eclipse
数据库表:5张
项目介绍:通过2种角色使用。
管理员进行管理图书馆内的所有书籍,以及书籍的相关信息。并能够管理学生用户的个人信息,之后管理学生的借书、还书情况。
学生则通过注册登录进行使用。主要进行在线借书和还书的工作。
另一个是自己开发的图书管理系统【原创开发】
Jsp+Servlet的新闻管理系统【原创开发】
Jsp+Servlet的外卖配送系统/在线点餐系统
以借阅书籍这个功能的实现为例子。
package com.cya.controller;
import java.io.IOException;
import java.io.PrintWriter;
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;
import com.cya.pojo.Admin;
import com.cya.dao.AdminDao;
import com.cya.dao.BookDao;
/**
* Servlet implementation class borrowServlet
*/
@WebServlet("/borrowServlet")
public class borrowServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public borrowServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
//设置编码类型
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
BookDao bookdao = new BookDao();
//为了区分借书和还书的功能,设置tip,tip为1,表示借书
int tip = Integer.parseInt(request.getParameter("tip"));
if (tip == 1) {
//获取图书id
int bid = Integer.parseInt(request.getParameter("bid"));
HttpSession session = request.getSession();
Admin admin = new Admin();
String status=request.getParameter("status");
String id="";
if(status.equals("user")) {
//获取到存入session的读者id
id = (String) session.getAttribute("uid");
}
else {
//获取到存入session的aid读者id
id = (String) session.getAttribute("aid");
}
AdminDao admindao = new AdminDao();
//通过aid获取到读者的信息
admin = admindao.get_AidInfo2(id);
//将借阅记录存入数据表
bookdao.borrowBook(bid, admin);
response.sendRedirect("/manage_books/books/user/select.jsp");
} else {
//还书功能,获取借阅记录的hid
int hid = Integer.parseInt(request.getParameter("hid"));
/**
* 还书在管理员和读者界面都有,为了区分,设置了show字段,show为1表示读者界面
*/
int show = Integer.parseInt(request.getParameter("show"));
//调用还书函数,改变status字段
bookdao.borrowBook2(hid);
if (show == 1) {
response.sendRedirect("/manage_books/books/user/borrow.jsp");
} else {
response.sendRedirect("/manage_books/books/admin/admin_borrows.jsp");
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.cya.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import com.cya.pojo.Admin;
import com.cya.pojo.Book;
import com.cya.pojo.History;
import com.cya.util.DBUtil;
/**
* 关于图书连接数据库的所有操作的类
*/
public class BookDao {
/**
* 添加图书信息,传入所有的信息
*
* @param card
* @param name
* @param type
* @param autho
* @param press
* @param num
*/
public void addBook(String card, String name, String type, String autho, String press, int num) {
// TODO Auto-generated method stub
Connection conn = DBUtil.getConnectDb();
String sql = "insert into book(card,name,type,autho,press,num) values(?,?,?,?,?,?)";
int rs = 0;
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1, card);
stm.setString(2, name);
stm.setString(3, type);
stm.setString(4, autho);
stm.setString(5, press);
stm.setInt(6, num);
rs = stm.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取所有的图书信息,返回的是ArrayList数组形式
*
* @return
*/
public ArrayList<Book> get_ListInfo() {
ArrayList<Book> tag_Array = new ArrayList<Book>();
Connection conn = DBUtil.getConnectDb();
String sql = "select * from book";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while (rs.next()) {
Book tag = new Book();
tag.setBid(rs.getInt("bid"));
tag.setName(rs.getString("name"));
tag.setCard(rs.getString("card"));
tag.setType(rs.getString("type"));
tag.setAutho(rs.getString("autho"));
tag.setPress(rs.getString("press"));
tag.setNum(rs.getInt("num"));
tag_Array.add(tag);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.CloseDB(rs, stm, conn);
}
return tag_Array;
}
/**
* 获取借阅记录的全部信息,传入的条件有status,aid,表示搜索正在借阅的,或者已经还书的信息,aid代表当前登录用户
*
* @param status
* @return
*/
public ArrayList<History> get_HistoryListInfo(int status, String aid) {
ArrayList<History> tag_Array = new ArrayList<History>();
Connection conn = DBUtil.getConnectDb();
String sql = "select * from history where aid='" + aid + "' and status='" + status + "'";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while (rs.next()) {
History tag = new History();
tag.setHid(rs.getInt("hid"));
tag.setAid(rs.getInt("aid"));
tag.setBid(rs.getInt("bid"));
tag.setBookname(rs.getString("bookname"));
tag.setCard(rs.getString("card"));
tag.setAdminname(rs.getString("adminname"));
tag.setUsername(rs.getString("username"));
tag.setBegintime(rs.getString("begintime"));
tag.setEndtime(rs.getString("endtime"));
tag.setStatus(rs.getInt("status"));
tag_Array.add(tag);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.CloseDB(rs, stm, conn);
}
return tag_Array;
}
/**
* 获取借阅记录的全部信息,传入的条件有status,表示搜索正在借阅的,或者已经还书的信息
*
* @param status
* @return
*/
public ArrayList<History> get_HistoryListInfo2(int status) {
ArrayList<History> tag_Array = new ArrayList<History>();
Connection conn = DBUtil.getConnectDb();
String sql = "select * from history where status='" + status + "'";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while (rs.next()) {
History tag = new History();
tag.setHid(rs.getInt("hid"));
tag.setAid(rs.getInt("aid"));
tag.setBid(rs.getInt("bid"));
tag.setBookname(rs.getString("bookname"));
tag.setCard(rs.getString("card"));
tag.setAdminname(rs.getString("adminname"));
tag.setUsername(rs.getString("username"));
tag.setBegintime(rs.getString("begintime"));
tag.setEndtime(rs.getString("endtime"));
tag.setStatus(rs.getInt("status"));
tag_Array.add(tag);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.CloseDB(rs, stm, conn);
}
return tag_Array;
}
/**
* 获取单个图书的信息,根据传入的bid来查找,返回一个Book数据类型
*
* @param bid
* @return
*/
public Book get_BookInfo(int bid) {
Book tag = new Book();
Connection conn = DBUtil.getConnectDb();
String sql = "select * from book where bid='" + bid + "'";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while (rs.next()) {
tag.setBid(rs.getInt("bid"));
tag.setName(rs.getString("name"));
tag.setCard(rs.getString("card"));
tag.setType(rs.getString("type"));
tag.setAutho(rs.getString("autho"));
tag.setPress(rs.getString("press"));
tag.setNum(rs.getInt("num"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.CloseDB(rs, stm, conn);
}
return tag;
}
/**
* 修改图书的信息,bid作为条件,
*/
public void updateBook(int bid, String card, String name, String type, String autho, String press, int num) {
// TODO Auto-generated method stub
Connection conn = DBUtil.getConnectDb();
String sql = "update book set name=?,card=?,type=?,autho=?,press=?,num=? where bid=?";
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1, name);
stm.setString(2, card);
stm.setString(3, type);
stm.setString(4, autho);
stm.setString(5, press);
stm.setInt(6, num);
stm.setInt(7, bid);
stm.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 删除图书信息,根据传入的bid作为条件
*
* @param bid
*/
public void deleteBook(int bid) {
// TODO Auto-generated method stub
Connection conn = DBUtil.getConnectDb();
String sql = "delete from book where bid=?";
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setInt(1, bid);
stm.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(uid);
}
/**
* 用户查找图书,根据输入的名称,使用like进行模糊查询,然后返回一个ArrayList数组类型
*
* @param name
* @return
*/
public ArrayList<Book> getLikeList(String name) {
// TODO Auto-generated method stub
ArrayList<Book> tag_Array = new ArrayList<Book>();
Connection conn = DBUtil.getConnectDb();
String sql = "select * from book where name like '%" + name + "%'";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while (rs.next()) {
Book tag = new Book();
tag.setBid(rs.getInt("bid"));
tag.setName(rs.getString("name"));
tag.setCard(rs.getString("card"));
tag.setType(rs.getString("type"));
tag.setAutho(rs.getString("autho"));
tag.setPress(rs.getString("press"));
tag.setNum(rs.getInt("num"));
tag_Array.add(tag);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.CloseDB(rs, stm, conn);
}
return tag_Array;
}
/**
* 图书借阅函数,根据传入bid图书id,adminbean当前登录用户的信息,在借阅记录数据表中新插入一条记录
*
* @param bid
* @param adminbean
*/
public void borrowBook(int bid, Admin adminbean) {
// TODO Auto-generated method stub
Book bookbean = new Book();
bookbean = this.get_BookInfo(bid);
//生成日期的功能
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DATE);
//生成借阅开始日期
String begintime = "" + year + "-" + month + "-" + day;
month = month + 1;
//生成截止还书日期
String endtime = "" + year + "-" + month + "-" + day;
Connection conn = DBUtil.getConnectDb();
String sql = "insert into history(aid,bid,card,bookname,adminname,username,begintime,endtime,status) values(?,?,?,?,?,?,?,?,?)";
int rs = 0;
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setInt(1, adminbean.getAid());
stm.setInt(2, bookbean.getBid());
stm.setString(3, bookbean.getCard());
stm.setString(4, bookbean.getName());
stm.setString(5, adminbean.getUsername());
stm.setString(6, adminbean.getName());
stm.setString(7, begintime);
stm.setString(8, endtime);
stm.setInt(9, 1);
rs = stm.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 还书功能的函数,根据传入的hid借阅记录id,讲status字段的值改为0,并将还书日期改变为当前日期
*
* @param hid
*/
public void borrowBook2(int hid) {
// TODO Auto-generated method stub
//生成日期
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DATE);
//生成还书日期
String endtime = "" + year + "-" + month + "-" + day;
Connection conn = DBUtil.getConnectDb();
String sql = "update history set endtime=?,status=? where hid=?";
PreparedStatement stm = null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1, endtime);
stm.setInt(2, 0);
stm.setInt(3, hid);
stm.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}