SSM框架实战详细教程(二)Java Web前置项目

        在Java Web学习过程中,我们使用的是Eclipse开发工具搭建的Java Web项目,因为Eclipse对初学者方便易上手。而在SSM阶段,我们要符合行业大多数的使用习惯,使用IDEA+Maven的形式。
        为了便于承上启下,我们先使用Eclipse搭建传统的项目开发一个我们贯穿项目。
        做一个教务管理系统学生模块,从数据库中查询数据,利用Bootstrap修饰网页,项目效果如下:
SSM框架实战详细教程(二)Java Web前置项目_第1张图片
        数据库表结构如下:
SSM框架实战详细教程(二)Java Web前置项目_第2张图片

        项目结构如下:
SSM框架实战详细教程(二)Java Web前置项目_第3张图片
代码如下:

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>

你可能感兴趣的:(SSM,SSM,JavaEE)