From 0a19f12fcdfacb846b8780a461adaf3af740905c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=B4=8B?= Date: Sat, 10 Jun 2023 09:04:30 +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 --- "48 \346\235\250\346\264\213/20230609test.md" | 481 ++++++++++++++++++ 1 file changed, 481 insertions(+) create mode 100644 "48 \346\235\250\346\264\213/20230609test.md" diff --git "a/48 \346\235\250\346\264\213/20230609test.md" "b/48 \346\235\250\346\264\213/20230609test.md" new file mode 100644 index 0000000..8664a00 --- /dev/null +++ "b/48 \346\235\250\346\264\213/20230609test.md" @@ -0,0 +1,481 @@ +### mysql + +```mysql +create database test charset utf8; +use test; +create table housetype( -- 房屋类型表 + id int primary key auto_increment, -- 编号 + type varchar(50) not null -- 房屋类型 +); +insert into housetype values + (null,'两室二厅'), + (null,'三室一厅'), + (null,'四室一厅'); +create table houseinfo( -- 房源信息表 + id int primary key auto_increment, -- 编号 + mode varchar(50) not null , -- 租赁方式 + rent double not null , -- 租金 + contacts varchar(20), -- 联系人 + method varchar(20), -- 押金方式 + typeid int, -- 房屋类型 + address varchar(200) not null , -- 详细地址 + foreign key houseinfo(typeid) references housetype(id) +); +insert into houseinfo values + (null,'整租',340,'黄晓明','押一付四',1,'闽西曹溪路团结里09号'), + (null,'整租',34040,null,'押一付四',2,'闽西曹溪路团结里09号'), + (null,'合租',34100,'李晓明','押一付四',3,'闽西曹溪路团结里209号'), + (null,'合租',32400,'黄明','押一付一',3,'闽西曹溪路团结里309号'), + (null,'月租',3400,null,'押二付四',1,'闽西曹溪路团结里109号'); +``` + + + +### bean + +```java +package bean; + +public class HouseInfo { + private int id; + private String mode; + private double rent; + private String contacts; + private String method; + private int typeid; + private String address; + private String type; + + public HouseInfo(int id, String mode, double rent, String contacts, String method, int typeid, String address, String type) { + this.id = id; + this.mode = mode; + this.rent = rent; + this.contacts = contacts; + this.method = method; + this.typeid = typeid; + this.address = address; + this.type = type; + } + + public HouseInfo() { + } + + 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 typrid) { + this.typeid = typrid; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", mode='" + mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", method='" + method + '\'' + + ", typrid=" + typeid + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } +} + +``` + +```java +package bean; + +public class HouseType { + private int id; + private 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 + '\'' + + '}'; + } +} + +``` + +### ubtil + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + public static Connection getconn() throws SQLException { + Connection conn = DriverManager.getConnection("jdbc:mysql;//localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8","root","root"); + return conn; + } + public static ResultSet query(String sql, Object ...yes) throws SQLException { + Connection conn = getconn(); + PreparedStatement pr = conn.prepareStatement(sql); + for (int i = 0; i < yes.length ; i++) { + pr.setObject((i+1),yes[i]); + } + ResultSet re = pr.executeQuery(); + return re; + } + public static int update(String sql, Object ...yes) throws SQLException { + Connection conn = getconn(); + PreparedStatement pr = conn.prepareStatement(sql); + for (int i = 0; i < yes.length ; i++) { + pr.setObject((i+1),yes[i]); + } + int re = pr.executeUpdate(); + return re; + } + public static void close(Connection conn,PreparedStatement pr,ResultSet re) throws SQLException { + if(conn!=null){ + conn.close(); + } + if (pr!=null){ + pr.close(); + } + if(re!=null){ + re.close(); + } + } +} + +``` + +### servlet + +```java +package servlet; + +import bean.HouseType; +import utils.DBUtil; + +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.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +@WebServlet("/add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;character=utf-8"); + String sql="select * from HouseType"; + ArrayList list = null; + try { + ResultSet re = DBUtil.query(sql); + list = new ArrayList<>(); + while (re.next()){ + int id = re.getInt(1); + String type = re.getString(2); + + HouseType houseType = new HouseType(id,type); + list.add(houseType); + } + } catch (SQLException e) { + e.printStackTrace(); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/add.jsp").forward(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + req.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;character=utf-8"); + String mode = req.getParameter("mode"); + String rent = req.getParameter("rent"); + String contacts = req.getParameter("contacts"); + String method = req.getParameter("method"); + String typeid = req.getParameter("typeid"); + String address = req.getParameter("address"); + String sql="insert into houseinfo values(null,?,?,?,?,?,?)"; + int i = 0; + try { + i = DBUtil.update(sql,null,mode,rent,contacts,method,typeid,address); + } catch (SQLException e) { + e.printStackTrace(); + } + if (i>0){ + resp.sendRedirect("list"); + }else req.setAttribute("msg","添加失败"); + req.getRequestDispatcher("/WEB-INF/msg.jsp").forward(req,resp); + } +} + +``` + +```java +package servlet; + +import bean.HouseInfo; +import utils.DBUtil; + +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.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/list") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + String sql="select * from houseinfo h,housetype t where h.typeid=t.id"; + ArrayList list = null; + try { + ResultSet re = DBUtil.query(sql); + list = new ArrayList<>(); + while (re.next()){ + int id = re.getInt(1); + String mode = re.getString(2); + double rent = re.getDouble(3); + String contacts = re.getString(4); + String method = re.getString(5); + int typeid = re.getInt(6); + String type = re.getString(9); + String address = re.getString(7); + + HouseInfo info = new HouseInfo(id,mode,rent,contacts,method,typeid,address,type); + list.add(info); + } + } catch (SQLException e) { + e.printStackTrace(); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/list.jsp").forward(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + } +} + +``` + +### jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: asus + Date: 2023/6/7 + Time: 13:53 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金(元)
联系人
押金方式
房屋类型 + + +
详细地址
+
+ + + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + +<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${s.id}${s.mode}${s.rent}${s.contacts}${s.method}${s.type}${s.address}
+ + + +``` + +```jsp +<%-- + Created by IntelliJ IDEA. + User: asus + Date: 2023/6/7 + Time: 15:04 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示信息 + + +

${msg}

+ 返回列表 + + + +``` + + + -- Gitee From 9104306d39efa9bba5dffdf01892fe7abab4359a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=B4=8B?= Date: Sun, 11 Jun 2023 23:38:32 +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 --- .../20230610\345\221\230\345\267\245.md" | 330 ++++++++++++++++++ 1 file changed, 330 insertions(+) create mode 100644 "48 \346\235\250\346\264\213/20230610\345\221\230\345\267\245.md" diff --git "a/48 \346\235\250\346\264\213/20230610\345\221\230\345\267\245.md" "b/48 \346\235\250\346\264\213/20230610\345\221\230\345\267\245.md" new file mode 100644 index 0000000..c20fa44 --- /dev/null +++ "b/48 \346\235\250\346\264\213/20230610\345\221\230\345\267\245.md" @@ -0,0 +1,330 @@ +#### Servlet + +```java +package servlet; + +import bean.Emp; +import dbutils.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 dept d,emp e where d.deptID=e.deptID"; + ResultSet re = null; + try { + re = DBUtil.query(sql); + } catch (SQLException e) { + e.printStackTrace(); + } + ArrayList list = new ArrayList<>(); + try { + while (re.next()){ + int empid = re.getInt("empid"); + String empname = re.getString("empname"); + String sex = re.getString("sex"); + int deptid = re.getInt("deptid"); + String deptname = re.getString("deptname"); + int age = re.getInt("age"); + String tel = re.getString("tel"); + String password = re.getString("password"); + Emp emp = new Emp(empid,empname,sex,age,tel,password,deptid,deptname); + list.add(emp); + } + } 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 { + request.setCharacterEncoding("utf-8"); + //获取表单的信息 + String user = request.getParameter("user"); + //编写SQL语句 + String sql="select * from dept d,emp e where d.deptID=e.deptID and empname like ?"; + ResultSet re = null; + try { + re = DBUtil.query(sql,"%"+user+"%"); + } catch (SQLException e) { + e.printStackTrace(); + } + ArrayList list = new ArrayList<>(); + try { + while (re.next()){ + int empid = re.getInt("empid"); + String empname = re.getString("empname"); + String sex = re.getString("sex"); + int deptid = re.getInt("deptid"); + String deptname = re.getString("deptname"); + int age = re.getInt("age"); + String tel = re.getString("tel"); + String password = re.getString("password"); + Emp emp = new Emp(empid,empname,sex,age,tel,password,deptid,deptname); + list.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + +} + +``` + +#### 数据库 + +```sql +# 数据库名称:CompanyManager +create database CompanyManager charset utf8; +use CompanyManager; +create table Dept( + DeptID int primary key auto_increment, # 部门编号主键 自动增长列 + DeptName varchar(20) not null # 部门名称 不允许为空 +); +insert into Dept values + (null,'开发部'), + (null,'综合部'), + (null,'财务部'), + (null,'后勤部'), + (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 Emp(DeptID)references Dept(DeptID) +); +insert into Emp values + (null,'张三','男',34,'110','12345',4), + (null,'张小','男',24,'130','12345',1), + (null,'查会','女',34,'410','12345',2), + (null,'李三','女',54,'010','12345',5), + (null,'房三','男',24,'910','12345',3); + + +``` + +#### Dept表 + +```java +package bean; + +public class Dept { + private int deptiD; //部门编号 + private String deptname;//部门名称 + + @Override + public String toString() { + return "Dept{" + + "deptiD=" + deptiD + + ", deptname='" + deptname + '\'' + + '}'; + } + + 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; + } + + public Dept() { + } + + public Dept(int deptiD, String deptname) { + this.deptiD = deptiD; + this.deptname = deptname; + } +} + +``` + +#### Emp表 + +```java +package bean; + +public class Emp { + + private int empid; //编号 + private String empname; //姓名 + private String sex; //性别 + private int age; //年龄 + private String tel; //电话 + private String password; //密码 + private int deptid; //部门编号 + private String deptname;//所属部门 + + @Override + public String toString() { + return "Emp{" + + "empid=" + empid + + ", empname='" + empname + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", password='" + password + '\'' + + ", deptid=" + deptid + + ", deptname='" + deptname + '\'' + + '}'; + } + + 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 getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + 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; + } + + public Emp() { + } + + public Emp(int empid, String empname, String sex, int age, String tel, String password, int deptid, String deptname) { + this.empid = empid; + this.empname = empname; + this.sex = sex; + this.age = age; + this.tel = tel; + this.password = password; + this.deptid = deptid; + this.deptname = deptname; + } +} + +``` + +#### list + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 16:23 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 员工信息 + + +
+ 姓名 + + +
+ + + + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门操作
${t.empid}${t.empname}${t.sex}${t.age}${t.tel}${t.deptname}修改 + 删除 +
+ + + +``` + -- Gitee