Java前后端交互实现班级管理(查询)

1,数据库创建存储专业信息的表

2,后端:

连接数据库工具类DBUtil.java:

java 复制代码
package com.ffyc.webserver.util;

import java.sql.*;

public class DButils {
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {

        String url = "jdbc:mysql://127.0.0.1:3306/userdb?serverTimezone=Asia/Shanghai";
        Connection connection = DriverManager.getConnection(url, "root", "123456");
        return connection;
    }

    public static void close(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {

        if (resultSet != null) {
            resultSet.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }

    }
}

专业类信息:

java 复制代码
package com.ffyc.webserver.Models;

import java.util.Date;

public class Major {
    int id;
    String name;
    int adminid;
    Date oper_time;
    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 int getAdminid() {
        return adminid;
    }

    public void setAdminid(int adminid) {
        this.adminid = adminid;
    }

    public Date getOper_time() {
        return oper_time;
    }

    public void setOper_time(Date oper_time) {
        this.oper_time = oper_time;
    }
}

后端对数据库查询:

java 复制代码
package com.ffyc.webserver.dao;

import com.ffyc.webserver.Models.Major;
import com.ffyc.webserver.Models.Student;
import com.ffyc.webserver.util.DButils;

import javax.imageio.ImageReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MajorDao {
    public List<Major>majors() throws SQLException {
        List<Major>list=new ArrayList<>();
        Connection connection= DButils.getConnection();
        String sql="SELECT m.id,m.name,a.account,m.oper_time\n" +
                "FROM major m LEFT JOIN USER a \n" +
                "ON a.id=m.adminid;";
        PreparedStatement preparedStatement= connection.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();
        while(resultSet.next())
        {
            Major major=new Major();
            major.setId(resultSet.getInt("id"));
            major.setAccount(resultSet.getString("account"));
            major.setName(resultSet.getString("name"));
            major.setOper_time(resultSet.getDate("oper_time"));
            list.add(major);
        }
        DButils.close(resultSet,preparedStatement,connection);
        return list;
    }
}

创建专业servlet:MajorServlet.java

java 复制代码
package com.ffyc.webserver.servlet;

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 MajorServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

重写代码:

java 复制代码
package com.ffyc.webserver.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ffyc.webserver.Models.CommonData;
import com.ffyc.webserver.Models.Major;
import com.ffyc.webserver.dao.MajorDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MajorServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter printWriter= resp.getWriter();
        CommonData commonData=null;
        try{
            MajorDao majorDao=new MajorDao();
            List<Major>majorlist= majorDao.majors();
            commonData=new CommonData(200,majorlist,"查询成功!");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            commonData=new CommonData(401,"服务器忙...请稍后重试!");
        }
        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(commonData);
        printWriter.print(json);
    }
}

配置web.xml

java 复制代码
    <servlet>
        <servlet-name>Major</servlet-name>
        <servlet-class>com.webserver.servlet.MajorServlet</servlet-class>
        <!--        <load-on-startup>0</load-on-startup>-->
    </servlet>
    <servlet-mapping>
        <servlet-name>Major</servlet-name>
        <url-pattern>/admin/MajorServlet</url-pattern>
    </servlet-mapping>

前端:

html 复制代码
<template>
	<div>
		<el-button type="primary">添加专业信息</el-button>
		<el-table :data="tableData" height="400" border style="width: 100%">
			<el-table-column prop="id" label="序号" width="100">
			</el-table-column>
			<el-table-column prop="name" label="专业名" width="100">
			</el-table-column>
			<el-table-column prop="account" label="修改人">
			</el-table-column>
			<el-table-column prop="oper_time" label="修改时间" align="center">
			</el-table-column>
			<el-table-column width="200">
				<template slot-scope="scope">
					<el-button type="primary" icon="el-icon-edit"></el-button>
					<el-button type="primary" icon="el-icon-delete"></el-button>
				</template>
			</el-table-column>

		</el-table>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				tableData: []
			}
		},
		mounted() {
			this.$http.get("admin/MajorServlet").then(resp => {
				this.tableData = resp.data.data;
			})
		}
	}
</script>

<style>
</style>

效果展示:

以上仅完成了对专业信息的查询。

相关推荐
类人_猿1 小时前
ASP.NET Web(.Net Framework) Http服务器搭建以及IIS站点发布
前端·iis·asp.net·.net·http站点服务器
筱小虾米1 小时前
解决SSL VPN客户端一直提示无法连接服务器的问题
运维·服务器
小黄编程快乐屋1 小时前
各个排序算法基础速通万字介绍
java·算法·排序算法
kingwebo'sZone1 小时前
ASP.net WebAPI 上传图片实例(保存显示随机文件名)
后端·asp.net
桑榆肖物1 小时前
一个简单的ASP.NET 一致性返回工具库
后端·asp.net
材料苦逼不会梦到计算机白富美3 小时前
贪心算法-区间问题 C++
java·c++·贪心算法
羚羊角uou4 小时前
【C++】list模拟实现(详解)
开发语言·c++
组态软件4 小时前
web组态软件
前端·后端·物联网·编辑器·html
Peter_chq4 小时前
【计算机网络】多路转接之select
linux·c语言·开发语言·网络·c++·后端·select