SSM framework actual combat detailed tutorial (2) Java Web pre-project

        In the process of Java Web learning, we use the Java Web project built by the Eclipse development tool, because Eclipse is convenient and easy for beginners to use. In the SSM stage, we have to conform to most of the industry's usage habits and use the form of IDEA+Maven.
        In order to facilitate the link between the previous and the next, we first use Eclipse to build a traditional project to develop a project that we run through.
        Make a student module of an educational administration system, query data from the database, and use Bootstrap to modify the web page. The project effect is as follows:
insert image description here
        The database table structure is as follows:
insert image description here

        The project structure is as follows: The
insert image description here
code is as follows:

Student.java

package entity;

public class Student {
    
    
	private int id;
	private String name;
	private String gender;
	private int age;

	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 getGender() {
    
    
		return gender;
	}

	public void setGender(String gender) {
    
    
		this.gender = gender;
	}
	public int getAge() {
    
    
		return age;
	}
	public void setAge(int age) {
    
    
		this.age = age;
	}
}

StudentDao.java

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import entity.Student;

//JDBC
//1.添加Jar包
public class StudentDao {
    
    

	public List<Student> search() {
    
    
		List<Student> list = new ArrayList<Student>();
		try {
    
    
			// 2.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			// 3.建立连接
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "123456");
			// 4.生成SQL执行器
			Statement stat = conn.createStatement();
			// 5.执行SQL
			String sql = "select * from student";
			ResultSet rs = stat.executeQuery(sql);
			// 6.处理结果
			while (rs.next()) {
    
    
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setGender(rs.getString("gender"));
				stu.setAge(rs.getInt("age"));
				list.add(stu);
			}
			// 7.关闭资源链接
			rs.close();
			stat.close();
			conn.close();
		} catch (ClassNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}

StudentController.java

package controller;

import java.io.IOException;
import java.util.List;

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 dao.StudentDao;
import entity.Student;
@WebServlet("/stu")
public class StudentController extends HttpServlet{
    
    

	public void doGet(HttpServletRequest request,HttpServletResponse response) {
    
    
		StudentDao stuDao=new StudentDao();
		List<Student> list=stuDao.search();
		try {
    
    
			request.setAttribute("stus", list);
			request.getRequestDispatcher("WEB-INF/show.jsp").forward(request, response);
		} catch (ServletException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) {
    
    
		doGet(request,response);
	}
}

show.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.List,entity.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet"
	href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">

<style>
#container {
    
    
	width: 800px;
	margin: 20px auto;
}
</style>

</head>
<body>
	<%
		List<Student> list = (List<Student>) request.getAttribute("stus");
	%>
	<div id="container">
		<table class="table table-striped table-bordered table-hover">
			<tr>
				<th>ID</th>
				<th>姓名</th>
				<th>性别</th>
				<th>年龄</th>
			</tr>
			<%
				for (int i = 0; i < list.size(); i++) {
    
    
			%>
			<tr>
				<td><%=list.get(i).getId()%></td>
				<td><%=list.get(i).getName()%></td>
				<td><%=list.get(i).getGender()%></td>
				<td><%=list.get(i).getAge()%></td>
			</tr>

			<%
				}
			%>
		</table>
		<button type="button" class="btn btn-primary">新增</button>
		<button type="button" class="btn btn-primary">修改</button>
		<button type="button" class="btn btn-primary">删除</button>

	</div>
</body>
</html>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324230045&siteId=291194637