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

        在Java Web学习过程中,我们使用的是Eclipse开发工具搭建的Java Web项目,因为Eclipse对初学者方便易上手。而在SSM阶段,我们要符合行业大多数的使用习惯,使用IDEA+Maven的形式。
        为了便于承上启下,我们先使用Eclipse搭建传统的项目开发一个我们贯穿项目。
        做一个教务管理系统学生模块,实现两个功能,从数据库中查询数据,展示学生,并可以新增学生。利用Bootstrap修饰网页,项目效果如下:
在这里插入图片描述
在这里插入图片描述

        数据库表结构如下:
在这里插入图片描述

        项目结构如下:
在这里插入图片描述

代码如下:

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

	public boolean add(Student stu) {
		int rs=0;
		try {
			// 2.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			// 3.建立连接
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school?characterEncoding=utf-8", "root", "123456");
			// 4.生成SQL执行器
			Statement stat = conn.createStatement();
			// 5.执行SQL
			String sql = "insert into student (name,gender,age) values('"+stu.getName()+"','"+stu.getGender()+"',"+stu.getAge()+ ")";
			 rs = stat.executeUpdate(sql);

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

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) {
        String type = request.getParameter("type");
        if (type == null) {
            search(request, response);
        } else if (type.equals("showAdd")) {
            showAdd(request, response);

        } else if (type.equals("add")) {
            add(request, response);

        }

    }

    private void search(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();
        }
    }

    private void showAdd(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher("WEB-INF/showAdd.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void add(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        int age = Integer.parseInt(request.getParameter("age"));
        Student stu=new Student();
        stu.setName(name);
        stu.setGender(gender);
        stu.setAge(age);
        StudentDao stuDao=new StudentDao();
        boolean flag=stuDao.add(stu);
        if(flag){
            try {
                response.sendRedirect("stu");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{

        }
    }

    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>
<script>
	function showAdd(){

		location.href="stu?type=showAdd"

	}


</script>
</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" onclick="showAdd()">新增</button>
		<button type="button" class="btn btn-primary">修改</button>
		<button type="button" class="btn btn-primary">删除</button>

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

showAdd.jso

<%@ 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>

	<div id="container">
		<form class="form-horizontal" action="stu">
			<input type="hidden" name="type" value="add"/>
			<div class="form-group">
				<label class="col-sm-2 control-label">名字</label>
				<div class="col-sm-10">
					<input type="text" class="form-control"  name="name" placeholder="请输入名字">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">性别</label>
				<div class="col-sm-10">
					<input type="text" class="form-control" name="gender" placeholder="请输入性别">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">年龄</label>
				<div class="col-sm-10">
					<input type="text" class="form-control" name="age" placeholder="请输入年龄">
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="submit" class="btn btn-primary">保存</button>
				</div>
			</div>
		</form>

	</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值