学习日志——2019/08/09

学生信息管理系统(一)

  • 整体架构
    在这里插入图片描述

数据库准备

	CREATE DATABASE stus;
	USE stus;
	CREATE TABLE stu (
		sid INT PRIMARY KEY  AUTO_INCREMENT,
		sname VARCHAR (20),
		gender VARCHAR (5),
		phone VARCHAR (20),
		birthday DATE,
		hobby VARCHAR(50),
		info VARCHAR(200)
	);

查询

  1. 先写一个JSP 页面, 里面放一个超链接 。
     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h3><a href="StudentServlet">显示所有学生列表</a></h3>
</body>
</html>
  1. 写Servlet, 接收请求, 去调用 Service , 由service去调用dao
package e.servlet;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import e.dao.StudentDao;
import e.dao.impl.StudentDaoImpl;
import e.domain.Student;
import e.service.StudentService;
import e.service.impl.StudentServiceImpl;

/**
 * Servlet implementation class StudentServlet
 */
public class StudentServlet extends HttpServlet {
	
	/**
	 * 负责查询所有的学生信息,然后呈现到list.jsp页面上。
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		try {
			//1.查询出来所有的学生
			StudentService service=new StudentServiceImpl();
			List<Student> list =service.findAll();
			
			//2.先把数据存储到作用域中
			request.setAttribute("list", list);
			
			//3.跳转页面
			request.getRequestDispatcher("list.jsp").forward(request, response);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}



  1. 先写Dao , 做Dao实现。
package e.dao;

import java.sql.SQLException;
import java.util.List;

import e.domain.Student;

/**
 * 这是针对学生的数据访问
 * @author Administrator
 *
 */
public interface StudentDao {
	/**
	 * 查询所有学生
	 * @return List<Student>
	 */
	List<Student> findAll()throws SQLException;
}
----------------------------------------------------------------------------
package e.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import e.dao.StudentDao;
import e.domain.Student;
import e.util.JDBCUtil;
/** 
 * 这是针对StudentDao的实现,针对前面的规范,做出具体的实现
 * @author Administrator
 *
 */
public class StudentDaoImpl implements StudentDao {
	/**
	 * 查询所有学生
	 * @throws SQLException 
	 */
	@Override
	public List<Student> findAll() throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner runner=new QueryRunner(JDBCUtil.getDataSource());
		String sql="select * from stu";
		List<Student> list = runner.query(sql, new BeanListHandler<Student>(Student.class));
		return list;
	}

}

  1. 再Service , 做Service的实现。
package e.service;

import java.sql.SQLException;
import java.util.List;

import e.domain.Student;

public interface StudentService {

	/**
	 * 查询所有学生
	 * @return List<Student>
	 */
	List<Student> findAll()throws SQLException;
}
--------------------------------------------
package e.service.impl;

import java.sql.SQLException;
import java.util.List;

import e.dao.StudentDao;
import e.dao.impl.StudentDaoImpl;
import e.domain.Student;
import e.service.StudentService;
/**
 * 这是学生业务实现
 * @author Administrator
 *
 */
public class StudentServiceImpl implements StudentService {

	@Override
	public List<Student> findAll() throws SQLException {
		// TODO Auto-generated method stub
		StudentDao dao=new StudentDaoImpl();
		return dao.findAll();
	}

}

  1. 在servlet 存储数据,并且做出页面响应。
package e.servlet;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import e.dao.StudentDao;
import e.dao.impl.StudentDaoImpl;
import e.domain.Student;
import e.service.StudentService;
import e.service.impl.StudentServiceImpl;

/**
 * Servlet implementation class StudentServlet
 */
public class StudentServlet extends HttpServlet {
	
	/**
	 * 负责查询所有的学生信息,然后呈现到list.jsp页面上。
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		try {
			//1.查询出来所有的学生
			StudentService service=new StudentServiceImpl();
			List<Student> list =service.findAll();
			
			//2.先把数据存储到作用域中
			request.setAttribute("list", list);
			
			//3.跳转页面
			request.getRequestDispatcher("list.jsp").forward(request, response);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  1. 在list.jsp上显示数据

    EL + JSTL + 表格

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生列表</title>
</head>
<body>
	<table border="1px" width="700">
	<tr align="center">
		<td>编号</td>
		<td>姓名</td>
		<td>性别</td>
		<td>电话</td>
		<td>生日</td>
		<td>爱好</td>
		<td>简介</td>
		<td>操作</td>
	</tr>
	
	<c:forEach items="${list}"var="stu">
		<tr align="center">
			<td>${stu.sid}</td>
			<td>${stu.sname}</td>
			<td>${stu.gender}</td>
			<td>${stu.phone}</td>
			<td>${stu.birthday}</td>
			<td>${stu.hobby}</td>
			<td>${stu.info}</td>
			<td>${stu.sid}<a herf="#">更新</a> <a herf="#">删除</a></td>
		</tr>
	</c:forEach>
	</table>
</body>
</html>
  • 运行:
    出问题了
    在这里插入图片描述
  • 解决方法

一:打开eclipse→window→preference。
在这里插入图片描述

  • 在这里插入图片描述
  • 在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值