From c61afb7b05eabc4d7303a9d568b618e5828120d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E6=9D=8E=E6=8C=AF=E5=8D=8E=E2=80=9D?= <877611343@qq.com> Date: Fri, 9 Jun 2023 21:52:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\347\220\206\344\275\234\344\270\232.md" | 442 ++++++++++++++++++ 1 file changed, 442 insertions(+) create mode 100644 "02 \346\235\216\346\214\257\345\215\216/20230609 jsp\344\270\216servlet\347\254\254\344\270\211\346\254\241\346\250\241\346\213\237\345\244\247\350\200\203\345\221\230\345\267\245\347\256\241\347\220\206\344\275\234\344\270\232.md" diff --git "a/02 \346\235\216\346\214\257\345\215\216/20230609 jsp\344\270\216servlet\347\254\254\344\270\211\346\254\241\346\250\241\346\213\237\345\244\247\350\200\203\345\221\230\345\267\245\347\256\241\347\220\206\344\275\234\344\270\232.md" "b/02 \346\235\216\346\214\257\345\215\216/20230609 jsp\344\270\216servlet\347\254\254\344\270\211\346\254\241\346\250\241\346\213\237\345\244\247\350\200\203\345\221\230\345\267\245\347\256\241\347\220\206\344\275\234\344\270\232.md" new file mode 100644 index 0000000..9622ae8 --- /dev/null +++ "b/02 \346\235\216\346\214\257\345\215\216/20230609 jsp\344\270\216servlet\347\254\254\344\270\211\346\254\241\346\250\241\346\213\237\345\244\247\350\200\203\345\221\230\345\267\245\347\256\241\347\220\206\344\275\234\344\270\232.md" @@ -0,0 +1,442 @@ +# MySQL作业 + +```mysql +# 数据库名称:CompanyManager +create database if not exists CompanyManager charset utf8; +use CompanyManager; +# 表: Dept (部门信息表) +create table Dept +( + DeptID int primary key auto_increment, # 部门编号 主键,自动增长列 + DeptName varchar(20) not null # 部门名称 不允许为空 +); +# 表:Emp (员工信息表) + +create table 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 dept values + (0,'开发部'), + (0,'测试部'), + (0,'技术部'), + (0,'后勤部'); +insert into emp values + (0,'张三','男',18,'15078454454',123,1), + (0,'李四','女',25,'15078454544',123,2), + (0,'王五','男',22,'15065454454',123,4), + (0,'张三风','男',26,'15258454454',123,3), + (0,'皮五','男',23,'15066454454',123,2), + (0,'风姐','女',22,'15011454454',123,1); +``` + +# bean + + + +```java +package bean; + +public class Dept { + private int id; + private String name; + + public Dept() { + } + + public Dept(int id, String name) { + this.id = id; + this.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; + } + + @Override + public String toString() { + return "Dept{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} +------------------------------------------------------------------------------------------------------------------- +package bean; + +public class Emp { + private int id; + private String name; + private String sex; + private int age; + private String tel; + private String pwd; + private int deptID; + private String deptName; + + public Emp() { + } + + public Emp(int id, String name, String sex, int age, String tel, String pwd, int deptID, String deptName) { + this.id = id; + this.name = name; + this.sex = sex; + this.age = age; + this.tel = tel; + this.pwd = pwd; + this.deptID = deptID; + this.deptName = deptName; + } + + 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 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 getDeptID() { + return deptID; + } + + public void setDeptID(int deptID) { + this.deptID = deptID; + } + + public String getDeptName() { + return deptName; + } + + public void setDeptName(String deptName) { + this.deptName = deptName; + } + + @Override + public String toString() { + return "Emp{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", pwd='" + pwd + '\'' + + ", deptID=" + deptID + + ", deptName='" + deptName + '\'' + + '}'; + } +} + +``` + +# 工具类代码 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + private static final String url="jdbc:mysql:///companymanager?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username="root"; + private static final String password="root"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动注册失败"); + throw new RuntimeException(e); + } + } + public static Connection getConn() { + Connection conn = null; + try { + conn = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + public static void close(Connection conn, PreparedStatement pst, ResultSet rs){ + try { + if (rs!=null){ + rs.close(); + } + if (pst!=null){ + pst.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public static ResultSet qurey(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(); + return rs; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + 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(); + return num; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + +} + +``` + +# servlet + +```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("/list") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //1 编写查询所有员工的SQL语句,要连表 + String sql = "select * from emp e,dept d where e.DeptID=d.DeptID"; + //2 调用工具类,将SQL语句给通用的查询方法,得到结果集 + ResultSet rs = DBUtil.qurey(sql); + //3 创建一个集合,遍历结果集,将结果封装成对象,添加到集合 + ArrayList list = new ArrayList<>(); + try { + while (rs.next()) { + int id = rs.getInt(1); + String name = rs.getString(2); + String sex = rs.getString(3); + int age = rs.getInt(4); + String tel = rs.getString(5); + String password = rs.getString(6); + int deptID = rs.getInt(7); + String deptName = rs.getString(9); + Emp emp = new Emp(id, name, sex, age, tel, password, deptID, deptName); + list.add(emp); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + //4 将集合添加到request域中 + request.setAttribute("list", list); + //5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/empList.jsp").forward(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +------------------------------------------------------------------------------------------------------------------- +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("/select") +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 { + //1 处理乱码 + request.setCharacterEncoding("utf-8"); + //2 接收表单数据 + String user = request.getParameter("user"); + //3 编写SQL加上表单数据 + String sql="select * from emp e,dept d where e.DeptID=d.DeptID and EmpName like ? "; + //2 调用工具类,将SQL语句给通用的查询方法,得到结果集 + ResultSet rs = DBUtil.qurey(sql,"%"+user+"%"); + //3 创建一个集合,遍历结果集,将结果封装成对象,添加到集合 + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int id = rs.getInt(1); + String name = rs.getString(2); + String sex = rs.getString(3); + int age = rs.getInt(4); + String tel = rs.getString(5); + String password = rs.getString(6); + int deptID = rs.getInt(7); + String deptName = rs.getString(9); + Emp emp = new Emp(id, name, sex, age, tel, password, deptID, deptName); + list.add(emp); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + //4 将集合添加到request域中 + request.setAttribute("list",list); + //5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/empList.jsp").forward(request,response); + + } +} + +``` + +# 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:22 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 员工列表 + + + +
+ 姓名: + +
+ + + + + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${emp.id}${emp.name}${emp.sex}${emp.age}${emp.tel}${emp.deptName}
+ + + + +``` + -- Gitee From 75cf2df1dbd4fd7af9c397cc8557542ff11769f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E6=9D=8E=E6=8C=AF=E5=8D=8E=E2=80=9D?= <877611343@qq.com> Date: Sun, 11 Jun 2023 10:52:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\347\220\206\344\275\234\344\270\232.md" | 470 ++++++++++++++++++ 1 file changed, 470 insertions(+) create mode 100644 "02 \346\235\216\346\214\257\345\215\216/20230610 jsp\344\270\216servlet\347\254\254\345\233\233\346\254\241\346\250\241\346\213\237\345\244\247\350\200\203\346\210\277\345\261\213\347\256\241\347\220\206\344\275\234\344\270\232.md" diff --git "a/02 \346\235\216\346\214\257\345\215\216/20230610 jsp\344\270\216servlet\347\254\254\345\233\233\346\254\241\346\250\241\346\213\237\345\244\247\350\200\203\346\210\277\345\261\213\347\256\241\347\220\206\344\275\234\344\270\232.md" "b/02 \346\235\216\346\214\257\345\215\216/20230610 jsp\344\270\216servlet\347\254\254\345\233\233\346\254\241\346\250\241\346\213\237\345\244\247\350\200\203\346\210\277\345\261\213\347\256\241\347\220\206\344\275\234\344\270\232.md" new file mode 100644 index 0000000..9279958 --- /dev/null +++ "b/02 \346\235\216\346\214\257\345\215\216/20230610 jsp\344\270\216servlet\347\254\254\345\233\233\346\254\241\346\250\241\346\213\237\345\244\247\350\200\203\346\210\277\345\261\213\347\256\241\347\220\206\344\275\234\344\270\232.md" @@ -0,0 +1,470 @@ +# 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 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 + '\'' + + '}'; + } +} +------------------------------------------------------------------------------------------------------------------- +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 + '\'' + + '}'; + } +} + +``` + +# 工具类代码 + +```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; + } +} + +``` + +# servlet + +```java +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 { + + } +} +------------------------------------------------------------------------------------------------------------------ + +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); + } + + } +} + +``` + +# jsp + +```jsp +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示信息 + + +

${msg}

+
+返回列表 + + +------------------------------------------------------------------------------------------------------------------- +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 房屋列表 + + + + +
+ + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址
${info.id}${info.mode}${info.rent}${info.contacts}${info.method}${info.typeName}${info.address}
+ + +------------------------------------------------------------------------------------------------------------------- +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 11:32 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加房源 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型
详细地址
+
+ + + +``` + -- Gitee