bean.Student
package yhp.bean;
//实体类包-主要存放和数据库对应的实体类
//类名=表名
//列名=属性名
//实体类需要包含:属性,构造(无参,全参),setter/getter
//属于Model(M)
public class Student {
private Integer stuid;
private String stuname;
private Integer age;
private Integer sex;
public Student() {
}
public Student(Integer stuid, String stuname, Integer age, Integer sex) {
this.stuid = stuid;
this.stuname = stuname;
this.age = age;
this.sex = sex;
}
public Integer getStuid() {
return stuid;
}
public void setStuid(Integer stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
}
dao.impl.StudentDapImpl
package yhp.dao.impl;
import yhp.bean.Student;
import yhp.dao.StudentDao;
import yhp.util.DruidUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//接口名+Impl=当前类名
public class StudentDaoImpl extends DruidUtil implements StudentDao {
@Override
public List<Student> getall() {
List list = new ArrayList();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement("select * from student");
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
Student student = new Student();
student.setStuid(resultSet.getInt("stuid"));
student.setStuname(resultSet.getString("stuname"));
student.setAge(resultSet.getInt("age"));
student.setSex(resultSet.getInt("sex"));
list.add(student);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
close(connection,preparedStatement,resultSet);
}
return list;
}
}
dao.StudentDao
package yhp.dao;
import yhp.bean.Student;
import java.util.List;
//实体类名+Dao=当前类名
public interface StudentDao {
//定义操作数据库的方法
public List<Student> getall();
}
service.impl
package yhp.service.impl;
import yhp.bean.Student;
import yhp.dao.StudentDao;
import yhp.dao.impl.StudentDaoImpl;
import yhp.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = new StudentDaoImpl();
@Override
public List<Student> getall() {
return studentDao.getall();
}
}
service.StudentService
package yhp.service;
import yhp.bean.Student;
import java.util.List;
//bean类名+Service=当前类名
//service层主要定义业务逻辑,现阶段主要实现调取dao层
public interface StudentService {
//查询全部
public List<Student> getall();
}
servlet.StudentServlet
package yhp.servlet;
import yhp.bean.Student;
import yhp.service.impl.StudentServiceImpl;
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 java.io.IOException;
import java.util.List;
//c -controller 控制层
@WebServlet(urlPatterns = "/getstus")
public class StudentServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.接收请求参数
//2.调取service层方法
StudentServiceImpl studentService = new StudentServiceImpl();
List<Student> getall = studentService.getall();
//3.跳转页面
//后台传递数据给前台
req.setAttribute("stulist",getall);
req.getRequestDispatcher("/show.jsp").forward(req,resp);
}
}
util.DruidUtil
package yhp.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import javax.xml.transform.Result;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DruidUtil {
private static DataSource ds;
static{
try {
Properties ppt = new Properties();
ppt.load(DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
ds = DruidDataSourceFactory.createDataSource(ppt);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 从连接池中取出一个连接给用户
* @return
*/
public static Connection getConnection(){
try {
return ds.getConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
public static void close(Connection conn, Statement state, ResultSet rs){
try {
rs.close();
} catch (Exception throwables) {
throwables.printStackTrace();
}
try {
state.close();
} catch (Exception throwables) {
throwables.printStackTrace();
}
try {
conn.close();
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
}
druid.properties
url=jdbc:mysql://localhost:3306/yhp?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username=root
password=123456
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=10
minIdle=5
maxWait=3000
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="getstus">查询学生列表</a>
</body>
</html>
show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>show.jsp</h1>
<table border="1" width="500px" bgcolor="aqua">
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
<td>sex</td>
</tr>
<c:forEach items="${stulist}" var="stu">
<tr>
<td>${stu.stuid}</td>
<td>${stu.stuname}</td>
<td>${stu.age}</td>
<td>${stu.sex==1?"男":"女"}</td>
</tr>
</c:forEach>
</table>
</body>
</html>