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

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

Java前后端交互实现班级管理(查询)_第1张图片

 2,后端:

连接数据库工具类DBUtil.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();
        }

    }
}

专业类信息:

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;
    }
}

 后端对数据库查询:

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 Listmajors() throws SQLException {
        Listlist=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

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);
    }
}

 重写代码:

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();
            Listmajorlist= 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

    
        Major
        com.webserver.servlet.MajorServlet
        
    
    
        Major
        /admin/MajorServlet
    

 前端:




效果展示:

Java前后端交互实现班级管理(查询)_第2张图片

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

你可能感兴趣的:(后端,Javascript,前端,java,交互,开发语言,前端,后端,服务器)