码迷,mamicode.com
首页 > 其他好文 > 详细

图书管理系统

时间:2021-01-29 12:17:52      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:find   request   int   manage   ret   代码   timezone   asi   gets   

数据库的连接(JDBCUTil)
具体代码:

public class JDBCUtil {

	private static final String DRIVERNAME = "com.mysql.cj.jdbc.Driver";// jdbc 4.0以后不需要
	private static final String URL = "jdbc:mysql://localhost:3306/bookmanage?serverTimezone=Asia/Chongqing";
	private static final String USERNAME = "root";
	private static final String PASSWORD = "1551963";

	public static void registerDriver() {
		try {
			Class.forName(DRIVERNAME);// jdbc4.0以前需要这句进行驱动注册
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("找不到驱动");
		}
	}

	public static Connection getConnection() throws SQLException {
		Connection con = null;
		//System.out.println("正在连接数据库...");
		con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		//System.out.println("数据库已连接!");
		return con;
	}

学生登陆:
具体代码:

@WebServlet("/studentLoginServlet")
public class StudentLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        StudentService studentService = new StudentServiceImpl();
        Student student = studentService.login(username, password);
        if (student == null) {
            request.setAttribute("login_Error", "用户名或密码错误,请重新输入");
            //转发到登录页面
            request.getRequestDispatcher("/stuLogin.jsp").forward(request, response);
        } else {
            //登陆成功 设置Session域参数
            session.setAttribute("student", student);
            //重定向到success.jsp
            response.sendRedirect(request.getContextPath()+"/stuFindBookServlet");
        }
    }

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

展示主界面:
具体代码:

@WebServlet("/stuFindBookServlet")
public class StuFindBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StudentService studentService = new StudentServiceImpl();
        List<Book> books = studentService.findBook();
        request.setAttribute("books", books);
        request.getRequestDispatcher("/student_main.jsp").forward(request,response);
    }

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

退出到登陆界面:
具体代码:

@WebServlet("/stuLogoutServlet")
public class StuLogoutServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("student");
        request.getRequestDispatcher("/stuLogin.jsp").forward(request, response);
    }

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

完整运行截图:

管理员登陆界面
技术图片

学生登陆界面
技术图片

登陆失败界面
技术图片

管理员登陆成功界面
技术图片

增加图书
技术图片

技术图片

删除
技术图片

学生端
技术图片

图书管理系统

标签:find   request   int   manage   ret   代码   timezone   asi   gets   

原文地址:https://www.cnblogs.com/254916-cn/p/14342653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!