JavaWeb学生信息管理系统

提取码: 7sm3 

直接放上代码 ·

知识储备:java、mysql、Servlet、HTML、Jquery、Jscript、

        我的个人制作过程大概如下:

        数据库的创建——数据库的连接——Dao层——Service层——Servlet程序——页面设计。


        思路:首先我们要有被管理的对象——Student。那么我们首先要创建Student数据库表。JavaWeb学生信息管理系统_第1张图片

 与此同时创建Student类。

package com.atchensong.pojo;

public class Student {
    private Integer id;
    private String username;
    private int age;
    private String gender;
    private double score;

    public Student() {
    }

    public Student(Integer id, String username, int age, String gender, double score) {
        this.id = id;
        this.username = username;
        this.age = age;
        this.gender = gender;
        this.score = score;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", score=" + score +
                '}';
    }
}

 接下来通过JdbcUtils尝试与数据库连接,并查找数据库中的数据。

配置文件-jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/stu_info?characterEncoding=utf8
username=root
password=111111
initialSize=5
maxActive=10

JdbcUtils

package com.atchensong.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author Chensong
 * @description
 * @create 2022-02-17 8:56
 */
public class JdbcUtils {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    static {
        try {
            InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(inputStream);

            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

            //1.驱动需要加载一次
            Class.forName(driver);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //2.获取连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }

    //3.释放连接
    public static void release(Connection connection, Statement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null)
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        if (resultSet != null)
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }

}

确保连接好之后,我们接下来进入页面层面。首先应该实现登录功能。那么登录功能是管理员来登录的,所以应该用管理员的账号与密码。那么在数据库中创建manager表。只需要给上用户名密码就可以了。

JavaWeb学生信息管理系统_第2张图片

 与此同时创建Manager类。

package com.atchensong.pojo;

public class Manager {
    private String username;
    private String password;

    public Manager() {
    }

    public Manager(String username, String password) {
        this.username = username;
        this.password = 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;
    }

    @Override
    public String toString() {
        return "Manager{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

编写Dao层实现java程序与数据库的数据交互。

首先创建BaseDao。实现与数据库交互最底层的功能。这段儿代码也可以当作一个模板,适用于其他项目的编写。

package com.atchensong.dao.impl;

import com.atchensong.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public abstract class BaseDao {
    //使用DbUtils操作数据库
    private QueryRunner queryRunner = new QueryRunner();

    /*
     * update()方法用来执行:Insert\Update\Delete语句
     *
     * @return 如果返回-1,说明执行失败
返回其他表示影响的行数 * */ public int update(String sql, Object... args) { Connection connection = null; try { connection = JdbcUtils.getConnection(); return queryRunner.update(connection, sql, args); } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtils.release(connection, null, null); } return -1; } /* * * 查询返回一个javaBean的sql语句 * type 返回的对象类型 * sql 执行的sql语句 * args sql对应的参数值 * 返回的类型的泛型 * * */ public T queryForOne(Class type, String sql, Object... args) { Connection connection = null; try { connection = JdbcUtils.getConnection(); return queryRunner.query(connection, sql, new BeanHandler(type), args); } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtils.release(connection, null, null); } return null; } public List queryForList(Class type, String sql, Object... args) { Connection conn = null; try { conn = JdbcUtils.getConnection(); return queryRunner.query(conn, sql, new BeanListHandler(type), args); } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtils.release(conn, null, null); } return null; } public Object queryForSingleValue(String sql, Object... args) { Connection connection = null; try { connection = JdbcUtils.getConnection(); return queryRunner.query(connection, sql, new ScalarHandler(), args); } catch (SQLException e) { e.printStackTrace(); } return null; } }

 创建ManagerDao接口

package com.atchensong.dao;

import com.atchensong.pojo.Manager;

public interface ManagerDao {

    public Manager queryUserByUsernameAndPassword(String username, String password);

}

 创建Manager实现类。

package com.atchensong.dao.impl;

import com.atchensong.dao.ManagerDao;
import com.atchensong.pojo.Manager;

public class ManagerDaoImpl extends BaseDao implements ManagerDao {

    @Override
    public Manager queryUserByUsernameAndPassword(String username, String password) {
        String sql = "select * from `manager` where username=? and password = ?";
        return queryForOne(Manager.class, sql, username, password);
    }
}

创建Service层:

package com.atchensong.service;

import com.atchensong.pojo.Manager;

public interface ManagerService {
    /*
     * 登录业务
     * */
    public Manager login(Manager manager);
}
package com.atchensong.service.impl;

import com.atchensong.dao.ManagerDao;
import com.atchensong.dao.impl.ManagerDaoImpl;
import com.atchensong.pojo.Manager;
import com.atchensong.service.ManagerService;

public class ManagerServiceImpl implements ManagerService {
    ManagerDao managerDao = new ManagerDaoImpl();

    @Override
    public Manager login(Manager manager) {
        return managerDao.queryUserByUsernameAndPassword(manager.getUsername(), manager.getPassword());
    }
}

确保能够成功运行以后,我们开始实现页面与java程序的交互。这时要用到Servlet知识。

创建实现登录功能的Servlet代码

package com.atchensong.web;

import com.atchensong.pojo.Manager;
import com.atchensong.service.ManagerService;
import com.atchensong.service.impl.ManagerServiceImpl;

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

public class LoginServlet extends HttpServlet {
    ManagerService managerService = new ManagerServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Manager login = managerService.login(new Manager(username, password));
        if (login == null) {
            req.setAttribute("msg", "用户名或密码输入错误");
            req.setAttribute("username", username);
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
            System.out.println("登录失败");
        } else {
            System.out.println("登录成功");
            req.getRequestDispatcher("/login_success.jsp").forward(req, resp);
        }
    }
}

顺便附上登录页面

<%--
 Created by IntelliJ IDEA.
 User: 张宸菘
 Date: 2022/5/3
 Time: 15:59
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%----%>



    
    管理员登录
    
    
    


学 生 管 理 系 统

${ empty requestScope.msg ? "请输入用户名和密码:" :requestScope.msg}

还没有完。在web.xml中配置servlet程序地址。

JavaWeb学生信息管理系统_第3张图片

 完成上述工作以后,启动Tomcat,尝试登录操作。JavaWeb学生信息管理系统_第4张图片

 JavaWeb学生信息管理系统_第5张图片

 之后的:增删改查操作同登录操作一样:先在Dao层实现与数据库的连接,并编写对应 的方法,在通过Service层调用Dao层的方法。之后编写Servlet程序,配置Servlet程序。

你可能感兴趣的:(java)