diff --git "a/01 \351\231\210\346\266\233/20230606 \347\273\203\344\271\240.md" "b/01 \351\231\210\346\266\233/20230606 \347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..44926d52a27a8a2fd74651c9686c41ff0884a50c --- /dev/null +++ "b/01 \351\231\210\346\266\233/20230606 \347\273\203\344\271\240.md" @@ -0,0 +1,457 @@ +```mysql +CREATE DATABASE AttDB CHARSET utf8; -- 创建数据库 +use AttDB; +CREATE table Student( -- 创建学生表 + sid int PRIMARY key auto_increment, -- 主键,自动增长列 学号 + sname varchar(20) not null UNIQUE KEY -- 唯一,非空 学生姓名 +); +CREATE TABLE Attence( -- 创建考勤表 + aid int PRIMARY key auto_increment,-- 主键,自动增长列 考勤编号 + time varchar(20) not null, -- 非空 出勤时间 + type int , -- 1:已到;2:迟到;3旷课 出勤状况 + sid int , -- 外键 学号 + FOREIGN KEY (sid) REFERENCES Student(sid) +); + +insert into student VALUES -- 插入学生数据 +(1,"张三"), +(2,"李四"), +(3,"万五"); +insert into Attence VALUES -- 插入考勤数据 +(1,"2022-05-20 08:20:00",1,1), +(2,"2022-05-23 08:20:00",2,1), +(3,"2022-05-23 13:40:00",2,2), +(4,"2022-05-27 08:20:00",3,2), +(5,"2022-05-30 08:20:00",2,3); +``` + +```java +package bean; + +public class Student { + private int id; + private String name; + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } + + 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 Student() { + } + + public Student(int id, String name) { + this.id = id; + this.name = name; + } +} + +``` + +```java +package bean; + +import javax.xml.crypto.Data; + +public class Attence { + private int aid; + private int sid; + private String name; + private Data time; + private int type; + + @Override + public String toString() { + return "Attence{" + + "aid=" + aid + + ", sid=" + sid + + ", name='" + name + '\'' + + ", time=" + time + + ", type=" + type + + '}'; + } + + public int getAid() { + return aid; + } + + public void setAid(int aid) { + this.aid = aid; + } + + public int getSid() { + return sid; + } + + public void setSid(int sid) { + this.sid = sid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Data getTime() { + return time; + } + + public void setTime(Data time) { + this.time = time; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + public Attence() { + } + + public Attence(int aid, int sid, String name, Data time, int type) { + this.aid = aid; + this.sid = sid; + this.name = name; + this.time = time; + this.type = type; + } +} + +``` + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + + } + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection( + "jdbc:mysql:///?characterEncoding=utf8&useSSL=false&useUnicode=true", + "root", + "root" + ); + return conn; + } + public static ResultSet query(String sql,Object... x){ + ResultSet rs = null; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < x.length; i++) { + pst.setObject((i+1),x[i]); + } + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + + } + public static int update(String sql,Object... x){ + int num=0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < x.length; i++) { + pst.setObject((i+1),x[i]); + } + num = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; + } + +} + +``` + +```java +package server; + +import bean.Student; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.Date; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from student"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()) { + int sid = rs.getInt("sid"); + String sName = rs.getString("sname"); + Student student = new Student(sid, sName); + list.add(student); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```java +package server; + +import bean.Attence; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import javax.xml.crypto.Data; +import java.io.IOException; +import java.sql.Date; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/list") +public class Servlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from student s,Attence a where a.sid=s.sid"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + String num=null; + try { + while(rs.next()){ + int sid = rs.getInt("sid"); + String sName = rs.getString("sname"); + int aid = rs.getInt("aid"); + Date time = rs.getDate("time"); + int type = rs.getInt("type"); + switch (type){ + case 1: + num="已到"; + break; + case 2: + num="迟到"; + break; + case 3: + num="旷课"; + break; + } + Attence att = new Attence(aid,sid,sName, time,num); + list.add(att); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 18059506693 + Date: 2023/6/7 + Time: 13:09 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +

学生考勤系统

+
+ + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间
考勤情况已到 + 迟到 + 旷课
+ +
+
+ + + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 18059506693 + Date: 2023/6/7 + Time: 12:32 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生考勤 + + + +添加 + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤情况
${list.aid}${list.sid}${list.name}${list.time}${list.type}
+ + + +``` + +```java +package Servlet; + +import bean.Attence; +import bean.Student; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from student "; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int sid = rs.getInt("sid"); + String name = rs.getString("sname"); + Student stu = new Student(sid, name); + list.add(stu); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String sid = request.getParameter("sid"); + String time = request.getParameter("time"); + String type = request.getParameter("type"); + String sql="insert into attence values (?,?,?,?)"; + int i = DBUtil.update(sql, null, time, type, sid); + if (i>0){ + response.sendRedirect("/list"); + }else { + request.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} + +``` + diff --git "a/01 \351\231\210\346\266\233/20230609 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/01 \351\231\210\346\266\233/20230609 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..531c55949df52367253aba47a01c57d8e99fa0d7 --- /dev/null +++ "b/01 \351\231\210\346\266\233/20230609 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,450 @@ +mysql + +```mysql +# 序号 功能列表 功能描述 分数 +# 1 数据库设计 + +# 数据库名称:CompanyManager +create database if not exists CompanyManager charset utf8; +use CompanyManager; +# 表: Dept (部门信息表) +create table if not exists Dept +( + DeptID int primary key auto_increment,# 部门编号 主键,自动增长列 + DeptName varchar(20) not null # 部门名称 不允许为空 +); +# 添加部门信息 +insert into Dept +values (0, '开发部'), + (0, '测试部'), + (0, 'UI部'); +# 表:Emp (员工信息表) +create table if not exists Emp +( + EmpID int primary key auto_increment, # 员工编号 主键,自动增长列 + EmpName varchar(20) not null, # 员工姓名 不允许为空 + Sex char(2) not null, # 性别 不允许为空 + Age int not null, # 员工年龄 不允许为空 + Tel varchar(20) not null unique, # 联系电话 不允许为空(唯一约束) + PassWord varchar(12) not null, # 密码 不允许为空 + DeptID int, # 部门编号 外键,关联部门表DeptID字段 + foreign key (DeptID) references Dept (DeptID) +); +# 添加员工信息 +insert into Emp +values (0, '张三', '男', 25, '15078454454', 'root', 1), + (0, '李四', '女', 21, '15078454466', 'root', 2), + (0, '王五', '男', 24, '15078454477', 'root', 3), + (0, '111', '男', 11, '15078454499', 'root', 1); +``` + +bean.Dept + +```java +package bean; + +public class Dept { + private int deptId; + + public int getDeptId() { + return deptId; + } + + @Override + public String toString() { + return "Dept{" + + "deptId=" + deptId + + ", deptName='" + deptName + '\'' + + '}'; + } + + public Dept() { + } + + public Dept(int deptId, String deptName) { + this.deptId = deptId; + this.deptName = deptName; + } + + public void setDeptId(int deptId) { + this.deptId = deptId; + } + + public String getDeptName() { + return deptName; + } + + public void setDeptName(String deptName) { + this.deptName = deptName; + } + + private String deptName; +} +``` + +bean.Emp + +```java +package bean; + +public class Emp { + private int empID; + private String empName; + private String sex; + private int age; + private String tel; + private String pwd; + private int edptID; + private String edptName; + + @Override + public String toString() { + return "Emp{" + + "empID=" + empID + + ", empName='" + empName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", pwd='" + pwd + '\'' + + ", edptID=" + edptID + + ", edptName='" + edptName + '\'' + + '}'; + } + + public Emp() { + } + + public int getEmpID() { + return empID; + } + + public void setEmpID(int empID) { + this.empID = empID; + } + + public String getEmpName() { + return empName; + } + + public void setEmpName(String empName) { + this.empName = empName; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getTel() { + return tel; + } + + public void setTel(String tel) { + this.tel = tel; + } + + public String getPwd() { + return pwd; + } + + public void setPwd(String pwd) { + this.pwd = pwd; + } + + public int getEdptID() { + return edptID; + } + + public void setEdptID(int edptID) { + this.edptID = edptID; + } + + public String getEdptName() { + return edptName; + } + + public void setEdptName(String edptName) { + this.edptName = edptName; + } + + public Emp(int empID, String empName, String sex, int age, String tel, String pwd, int edptID, String edptName) { + this.empID = empID; + this.empName = empName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.pwd = pwd; + this.edptID = edptID; + this.edptName = edptName; + } +} +``` + +servlet.ListServlet + +```java +package servlet; + +import bean.Emp; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/ListServlet") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from emp e , dept d where d.deptID=e.deptID"; + ResultSet emp = DBUtil.select(sql); + ArrayList list = new ArrayList<>(); + try { + while (emp.next()){ + int empID = emp.getInt(1); + String empName = emp.getString(2); + String sex = emp.getString(3); + int age = emp.getInt(4); + String tel = emp.getString(5); + String pwd = emp.getString(6); + int deptID = emp.getInt(7); + String deptName = emp.getString(9); + Emp e = new Emp(empID,empName,sex,age,tel,pwd,deptID,deptName); + list.add(e); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +servlet.SelectServlet + +```java +package servlet; + +import bean.Emp; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/SelectServlet") +public class SelectServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 修改字符集 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=UTF-8"); + // 接收表单数据 + String name = request.getParameter("name"); + // 编写查询语句 + String sql = "select * from emp e , dept d where d.deptID=e.deptID and EmpName like ?"; + ResultSet emp = DBUtil.select(sql, "%" + name + "%"); + ArrayList list = new ArrayList<>(); + try { + while (emp.next()) { + int empID = emp.getInt(1); + String empName = emp.getString(2); + String sex = emp.getString(3); + int age = emp.getInt(4); + String tel = emp.getString(5); + String pwd = emp.getString(6); + int deptID = emp.getInt(7); + String deptName = emp.getString(9); + Emp e = new Emp(empID, empName, sex, age, tel, pwd, deptID, deptName); + list.add(e); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + System.out.println(list); + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/select.jsp").forward(request, response); + } +} +``` + +utils.DBUtil + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + // 1 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + // 2 获取连接数据库方法 + public static Connection conn() { + Connection conn = null; + try { + conn = DriverManager.getConnection("jdbc:mysql:///companymanager?characterEncoding=utf8", "root", "root"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + + // 3 update通用方法 + public static int update(String sql, Object... keys) { + int rs = 0; + + try { + PreparedStatement st = conn().prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + st.setObject((i + 1), keys[i]); + } + rs = st.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return rs; + } + // 4 select通用方法 + public static ResultSet select(String sql, Object... keys) { + ResultSet rs = null; + + try { + PreparedStatement st = conn().prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + st.setObject((i + 1), keys[i]); + } + rs = st.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return rs; + } +} +``` + +lib.list.jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 10:51 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ 姓名 +
+ + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${emp.empID}${emp.empName}${emp.sex}${emp.age}${emp.tel}${emp.edptName}
+ + +``` + +lib.select.jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 11:02 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ 姓名 +
+ + + + + + + + + + + + + + + + + + + + + +``` \ No newline at end of file diff --git "a/01 \351\231\210\346\266\233/20230610.md" "b/01 \351\231\210\346\266\233/20230610.md" new file mode 100644 index 0000000000000000000000000000000000000000..4211b7fee3c9a7a85e8f69fab9a50f96ca5f838b --- /dev/null +++ "b/01 \351\231\210\346\266\233/20230610.md" @@ -0,0 +1,357 @@ +```` +## Mysql + +```mysql +# 数据库名称:test +create database test charset utf8; +use test; +# 表:house_type (房屋类型表) +create table house_type +( + id int primary key auto_increment, # 编号主键,自动增长列 + type varchar(50) not null # 房屋类型 不允许为空 +); +insert into house_type values + (0,'3室1厅'), + (0,'3室2厅'), + (0,'2室1厅'), + (0,'2室2厅'); +# 表:house_info (房源信息表) +create table house_info +( + id int primary key auto_increment, # 编号主键,自动增长列 + lease_mode varchar(50), # 租赁方式可以为空 + rent double not null, # 租金不允许为空 + contacts varchar(20), # 联系人可以为空 + deposit_method varchar(20), # 押金方式可以为空 + house_type_id int, # 房屋类型外键 + address varchar(200) not null, # 详细地址不允许为空 + foreign key (house_type_id) references house_type (id) +); + +insert into house_info values + (0,'整租',2230,'王晓明','押一付三',1,'泉州柳城区1号'), + (0,'整租',2180,'王晓明','押一付三',4,'龙岩新罗区2号'), + (0,'合租',1120,'张小飞','押一付一',2,'泉州柳城区3号'), + (0,'整租',2280,'王晓明','押一付二',3,'泉州柳城区2号'), + (0,'合租',1200,'张小飞','押一付一',2,'泉州鲤城区1号'); +``` + + + +## bean + +```java +package bean; + +public class HouseInfo { + int id; + String mode; + double rent; + String contacts; + String method; + int typeID; + String address; + String typeName; + + public HouseInfo() { + } + + public HouseInfo(int id, String mode, double rent, String contacts, String method, int typeID, String address, String typeName) { + this.id = id; + this.mode = mode; + this.rent = rent; + this.contacts = contacts; + this.method = method; + this.typeID = typeID; + this.address = address; + this.typeName = typeName; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getMode() { + return mode; + } + + public void setMode(String mode) { + this.mode = mode; + } + + public double getRent() { + return rent; + } + + public void setRent(double rent) { + this.rent = rent; + } + + public String getContacts() { + return contacts; + } + + public void setContacts(String contacts) { + this.contacts = contacts; + } + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } + + public int getTypeID() { + return typeID; + } + + public void setTypeID(int typeID) { + this.typeID = typeID; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getTypeName() { + return typeName; + } + + public void setTypeName(String typeName) { + this.typeName = typeName; + } + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", mode='" + mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", method='" + method + '\'' + + ", typeID=" + typeID + + ", address='" + address + '\'' + + ", typeName='" + typeName + '\'' + + '}'; + } +} + +package bean; + +public class HouseType { + int id; + String type; + + public HouseType() { + } + + public HouseType(int id, String type) { + this.id = id; + this.type = type; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + @Override + public String toString() { + return "HouseType{" + + "id=" + id + + ", type='" + type + '\'' + + '}'; + } +} + +``` + +## servlet + +```java +package servlet; + +import bean.HouseInfo; +import bean.HouseType; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from house_type"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int id = rs.getInt("id"); + String type = rs.getString("type"); + HouseType houseType = new HouseType(id, type); + list.add(houseType); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response) + ; + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String mode = request.getParameter("mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String method = request.getParameter("method"); + String typeID = request.getParameter("typeID"); + String address = request.getParameter("address"); + String sql="insert into house_info values(?,?,?,?,?,?,?)"; + int i = DBUtil.update(sql,null,mode,rent,contacts,method,typeID,address); + if (i>0){ + response.sendRedirect("/list"); + }else { + request.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + + } +} +package servlet; + +import bean.HouseInfo; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/list") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from house_type t,house_info i where t.id=i.house_type_id"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int id = rs.getInt("i.id"); + String mode = rs.getString("lease_mode"); + double rent = rs.getDouble("rent"); + String contacts = rs.getString("contacts"); + String method = rs.getString("deposit_method"); + int typeId = rs.getInt("house_type_id"); + String address = rs.getString("address"); + String typeName = rs.getString("type"); + HouseInfo info = new HouseInfo(id, mode, rent, contacts, method, typeId, address, typeName); + list.add(info); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response) + ; + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +## util + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + private static final String url="jdbc:mysql:///test?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String user="root"; + private static final String pwd="root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, user, pwd); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + public static ResultSet query(String sql,Object ...keys){ + Connection conn = getConn(); + ResultSet rs=null; + try { + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + rs = pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return rs; + } + public static int update(String sql,Object ...keys){ + Connection conn = getConn(); + int num=0; + try { + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + num = pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return num; + } +} + +```xxxxxxxxxx9 1<%@ page contentType="text/html;charset=UTF-8" language="java" %>234   提示567${xinxi}89 +```` \ No newline at end of file
编号姓名性别年龄电话所属部门
${emp.empID}${emp.empName}${emp.sex}${emp.age}${emp.tel}${emp.edptName}