Java WEB管理系统

2401_85358655 2024-07-31 08:33:10 阅读 73

@author 北落燕门

*/

public class LiangZai {

private int id;

private String name;

private String sex;

private String banJi;

private String stuId;

@Override

public String toString() {

return “Liang{” +

“id=” + id +

“, name='” + name + ‘’’ +

“, sex='” + sex + ‘’’ +

“, banJi='” + banJi + ‘’’ +

“, stuId='” + stuId + ‘’’ +

‘}’;

}

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 getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getBanJi() {

return banJi;

}

public void setBanJi(String banJi) {

this.banJi = banJi;

}

public String getStuId() {

return stuId;

}

public void setStuId(String stuId) {

this.stuId = stuId;

}

public LiangZai(int id, String name, String sex, String banJi, String stuId) {

this.id = id;

this.name = name;

this.sex = sex;

this.banJi = banJi;

this.stuId = stuId;

}

public LiangZai() {

}

}

package pojo;

/**

@author 北落燕门

*/

public class ResultInfo {

private boolean flag;

private Object data;

private String msg;

public boolean isFlag() {

return flag;

}

public void setFlag(boolean flag) {

this.flag = flag;

}

public Object getData() {

return data;

}

public void setData(Object data) {

this.data = data;

}

public String getMsg() {

return msg;

}

public void setMsg(String errorMsg) {

this.msg = errorMsg;

}

public ResultInfo(boolean flag, Object data, String errorMsg) {

this.flag = flag;

this.data = data;

this.msg = errorMsg;

}

public ResultInfo() {

}

}

第五步:连接数据库

编写配置文件

在resources文件夹下创建db.propertice文件然后输入以下内容

driver = com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/[数据库名称]?UseUnicode=true&characterEncoding=UTF-8

username = [数据库用户名]

password = [数据库密码]

实现JDBC建立连接

在java文件夹下新建utils文件夹并创建DbUtil类,内容如下:

package utils;

import com.mysql.jdbc.Connection;

import com.mysql.jdbc.Driver;

import java.io.FileReader;

import java.net.URL;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.Properties;

/**

@author 北落燕门

*/

public class DbUtil {

private static String url;

private static String username;

private static String password;

//使用静态方法自动执行

static {

try {

//实用反射将db.propertice文件加载进内存

Properties pro = new Properties();

ClassLoader classLoader = Driver.class.getClassLoader();

URL resource = classLoader.getResource(“db.propertice”);

assert resource != null;

String path = resource.getPath();

FileReader fileReader = new FileReader(path);

pro.load(fileReader);

//获取db.propertice文件的内容

String driver = pro.getProperty(“driver”);

url = pro.getProperty(“url”);

username = pro.getProperty(“username”);

password = pro.getProperty(“password”);

//加载驱动

Class.forName(driver);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

建立连接

@return 返回Connecting连接对象

@throws Exception 抛出异常

*/

public static Connection getConnection()throws Exception{

return (Connection) DriverManager.getConnection(url, username, password);

}

/**

关闭连接

@param ps

@param conn

*/

public static void close(PreparedStatement ps, Connection conn){

if (ps!=null){

try {

ps.close();

}catch (Exception e){

e.printStackTrace();

}

}else if (conn!=null){

try {

conn.close();

}catch (Exception e){

e.printStackTrace();

}

}

}

/**

方法重载 关闭连接

@param rs

@param ps

@param conn

*/

public static void close(ResultSet rs, PreparedStatement ps, Connection conn){

if (rs!=null){

try {

rs.close();

}catch (Exception e){

e.printStackTrace();

}

}else if (ps!=null){

try {

ps.close();

}catch (Exception e){

e.printStackTrace();

}

}else if (conn!=null){

try {

conn.close();

}catch (Exception e){

e.printStackTrace();

}

}

}

}

第六步:实现Dao层

之后在java文件夹下创建dao文件夹,并创建LiangZaiDao接口

根据任务需求我们需要实现添加实体、查询实体的功能,因此创建以下三个抽象方法

/**

通过id和password查找

@param name 姓名

@param stuId 班级

@return flag

*/

boolean findByNameAndStuId(String name, String stuId);

/**

通过用户名和密码查找

@param id id

@return flag

*/

boolean findById(int id);

/**

添加

@param lz 传入靓仔对象

@return flag

*/

boolean add(LiangZai lz);

之后在dao文件夹下新建impl文件夹并创建LiangZaiDaoImpl类继承LiangZaiDao接口并复写其中所有方法。返回值boolean类型用于判断是否执行成功。代码如下:

public class LiangZaiDaoImpl implements LiangZaiDao {

Connection conn = null;

PreparedStatement pstm = null ;

ResultSet rs = null;

/**

用于登录界面在数据库中查询姓名与学号

@param name 姓名

@param stuId 学号

@return flag

*/

@Override

public boolean findByNameAndStuId(String name, String stuId) {

//编写sql

String sql = “SELECT * FROM liangzai WHERE name = ? AND stu_id = ?”;

try {

//调用DbUtil建立连接

conn = DbUtil.getConnection();

//开启事务

conn.setAutoCommit(false);

//绑定sql

pstm = conn.prepareStatement(sql);

//补全sql语句

pstm.setString(1,name);

pstm.setString(2,stuId);

//执行sql

rs = pstm.executeQuery();

if (rs.next()){

//提交

conn.commit();

return true;

}

} catch (Exception e) {

try {

//回滚

if (conn!=null){

conn.rollback();

}

}catch (Exception e1){

e1.printStackTrace();

}

e.printStackTrace();

}finally {

//关闭数据库连接

DbUtil.close(rs,pstm,conn);

}

return false;

}

/**

用于查询用户是否存在(不用于登录)

@param id id

@return flag

*/

@Override

public boolean findById(int id) {

//编写sql

String sql = “SELECT * FROM liangzai WHERE id = ?”;

try {

//建立连接

conn = DbUtil.getConnection();

//开启事务

conn.setAutoCommit(false);

//绑定sql语句

pstm = conn.prepareStatement(sql);

pstm.setInt(1,id);

//执行sql

rs = pstm.executeQuery();

if (rs.next()){

conn.commit();

return true;

}

} catch (Exception e) {

try{

if (conn!=null){

//回滚

conn.rollback();

}

}catch (Exception e1){

e1.printStackTrace();

}

e.printStackTrace();

}finally {

//关闭数据库连接

DbUtil.close(rs,pstm,conn);

}

return false;

}

/**

用于注册等对数据库进行添加的操作

@param lz 传入靓仔对象

@return flag

*/

@Override

public boolean add(LiangZai lz) {

//编写sql

String sql = “INSERT INTO liangzai(name,sex,class,stu_id) VALUES(?,?,?,?)”;

try {

//建立连接

conn = DbUtil.getConnection();

//开启事务

conn.setAutoCommit(false);

pstm = conn.prepareStatement(sql);

pstm.setString(1,lz.getName());

pstm.setString(2,lz.getSex());

pstm.setString(3,lz.getBanJi());

pstm.setString(4,lz.getStuId());

int i = pstm.executeUpdate();

if (i!=0){

//提交事务

conn.commit();

return true;

}

} catch (Exception e) {

try{

if (conn!=null){

//回滚

conn.rollback();

}

}catch (Exception e1){

e1.printStackTrace();

}

e.printStackTrace();

}finally {

//关闭连接

DbUtil.close(pstm,conn);

}

return false;

}

}

第七步:实现service层

在java文件夹下新建一个包命名为service其中新建一个接口命名为LiangZaiService。

package service;

import pojo.LiangZai;

/**

@author 北落燕门

*/

public interface LiangZaiService {

/**

登录服务

@param lz 靓仔实体

@return 返回实体对象

*/

boolean liangZaiLogin(LiangZai lz);

/**

注册服务

@param lz 靓仔实体

@return 返回flag

*/

boolean liangZaiRegister(LiangZai lz);

}

之后在service文件夹中新建文件夹并创建类LiangZaiServiceImpl实现LiangZaiService接口

package service.impl;

import dao.LiangZaiDao;

import dao.imlp.LiangZaiDaoImpl;

import pojo.LiangZai;

import service.LiangZaiService;

/**

@author 北落燕门

*/

public class LiangZaiServiceImpl implements LiangZaiService {

LiangZaiDao lzDao = new LiangZaiDaoImpl();

@Override

public boolean liangZaiLogin(LiangZai lz) {

return lzDao.findByNameAndStuId(lz.getName(), lz.getStuId());

}

@Override

public boolean liangZaiRegister(LiangZai lz) {

boolean byNameAndStuId = lzDao.findByNameAndStuId(lz.getName(), lz.getStuId());

if (byNameAndStuId){

return false;

}

return lzDao.add(lz);

}

}

第八步:实现servlet

注册servlet

package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.beanutils.BeanUtils;

import pojo.LiangZai;

import pojo.ResultInfo;

import service.impl.LiangZaiServiceImpl;

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 java.io.IOException;

import java.lang.reflect.InvocationTargetException;

import java.util.Map;

/**

@author 北落燕门

*/

@WebServlet(“/register”)

public class RegisterServlet extends HttpServlet {

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//封装获取到的数据为map

Map<String, String[]> map = req.getParameterMap();

LiangZai lz = new LiangZai();

try {

//使用工具类将map元素封装为对象

BeanUtils.populate(lz,map);

} catch (IllegalAccessException | InvocationTargetException e) {

e.printStackTrace();

}

System.out.println(lz);

LiangZaiServiceImpl liangZaiService = new LiangZaiServiceImpl();

boolean flag = liangZaiService.liangZaiRegister(lz);

ResultInfo resultInfo = new ResultInfo();

if (flag){

resultInfo.setFlag(true);

resultInfo.setMsg(“注册成功”);

}else {

resultInfo.setFlag(false);

resultInfo.setMsg(“注册失败”);

}

//响应数据

ObjectMapper mapper = new ObjectMapper();

resp.setContentType(“application/json;charset=UTF-8”);

mapper.writeValue(resp.getOutputStream(),resultInfo);

}

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

this.doPost(req,resp);

}

}

登录servlet

package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;

import pojo.LiangZai;

import pojo.ResultInfo;

import service.LiangZaiService;

import service.impl.LiangZaiServiceImpl;

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 java.io.IOException;

/**

@author 北落燕门

*/

@WebServlet(“/login”)

public class LoginServlet extends HttpServlet {

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

LiangZai lz = new LiangZai();

ResultInfo resultInfo = new ResultInfo();

lz.setName(req.getParameter(“name”));

lz.setStuId(req.getParameter(“stuId”));

LiangZaiService liangZaiService = new LiangZaiServiceImpl();

boolean flag = liangZaiService.liangZaiLogin(lz);

if (!flag){

resultInfo.setMsg(“用户名或密码错误”);

resultInfo.setFlag(false);

}else{

resultInfo.setMsg(“登录成功”);

resultInfo.setFlag(true);

req.getSession().setAttribute(“liangzai”,lz);

//跳转首页

//req.getRequestDispatcher(“index.html”).forward(req,resp);

}

//响应数据

ObjectMapper mapper = new ObjectMapper();

resp.setContentType(“application/json;charset=UTF-8”);

mapper.writeValue(resp.getOutputStream(),resultInfo);

}

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

this.doPost(req,resp);

最后

小编利用空余时间整理了一份《MySQL性能调优手册》,初衷也很简单,就是希望能够帮助到大家,减轻大家的负担和节省时间。

关于这个,给大家看一份学习大纲(PDF)文件,每一个分支里面会有详细的介绍。

image

这里都是以图片形式展示介绍,如要下载原文件以及更多的性能调优笔记(MySQL+Tomcat+JVM)!

ttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

LiangZai lz = new LiangZai();

ResultInfo resultInfo = new ResultInfo();

lz.setName(req.getParameter(“name”));

lz.setStuId(req.getParameter(“stuId”));

LiangZaiService liangZaiService = new LiangZaiServiceImpl();

boolean flag = liangZaiService.liangZaiLogin(lz);

if (!flag){

resultInfo.setMsg(“用户名或密码错误”);

resultInfo.setFlag(false);

}else{

resultInfo.setMsg(“登录成功”);

resultInfo.setFlag(true);

req.getSession().setAttribute(“liangzai”,lz);

//跳转首页

//req.getRequestDispatcher(“index.html”).forward(req,resp);

}

//响应数据

ObjectMapper mapper = new ObjectMapper();

resp.setContentType(“application/json;charset=UTF-8”);

mapper.writeValue(resp.getOutputStream(),resultInfo);

}

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

this.doPost(req,resp);

最后

小编利用空余时间整理了一份《MySQL性能调优手册》,初衷也很简单,就是希望能够帮助到大家,减轻大家的负担和节省时间。

关于这个,给大家看一份学习大纲(PDF)文件,每一个分支里面会有详细的介绍。

[外链图片转存中…(img-PSTW5GXG-1721833260295)]

这里都是以图片形式展示介绍,如要下载原文件以及更多的性能调优笔记(MySQL+Tomcat+JVM)!



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。