diff --git "a/33 \347\206\212\346\231\257\345\263\260/20230610\344\275\234\344\270\232.md" "b/33 \347\206\212\346\231\257\345\263\260/20230610\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..458fa123f7bdd17f88c3a0bc498cf064433e963b --- /dev/null +++ "b/33 \347\206\212\346\231\257\345\263\260/20230610\344\275\234\344\270\232.md" @@ -0,0 +1,404 @@ +```java +package util; + +import java.sql.*; + +public class DBUtil { + private static final String url ="jdbc:mysql:///test?characterEncoding=utf8"; + private static final String user ="root"; + private static final String pow ="200311"; + static { + try { + Class.forName("com.mysql.cj.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, pow); + return conn; + } + public static ResultSet getQuery(String sql,Object ...flop) throws SQLException { + Connection conn = DBUtil.getConn(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setObject((i+1),flop[i]); + } + ResultSet rs = prs.executeQuery(); + return rs; + } + public static int getUpdate(String sql,Object ...flop) throws SQLException { + Connection conn = DBUtil.getConn(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setObject((i+1),flop[i]); + } + int i = prs.executeUpdate(); + return i; + } + public static void close(Connection conn,PreparedStatement prs,ResultSet rs){ + try { + if (rs!=null){ + rs.close(); + } + if (prs!=null){ + prs.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` + +```java +package bean; + +public class House_Info { + private int id; + private String lease_Mode; + private double rent; + private String contacts; + private String deposit_Method; + private int house_Type_Id; + private String address; + + public House_Info() { + } + + public House_Info(int id, String lease_Mode, double rent, String contacts, String deposit_Method, int house_Type_Id, String address) { + this.id = id; + this.lease_Mode = lease_Mode; + this.rent = rent; + this.contacts = contacts; + this.deposit_Method = deposit_Method; + this.house_Type_Id = house_Type_Id; + this.address = address; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLease_Mode() { + return lease_Mode; + } + + public void setLease_Mode(String lease_Mode) { + this.lease_Mode = lease_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 getDeposit_Method() { + return deposit_Method; + } + + public void setDeposit_Method(String deposit_Method) { + this.deposit_Method = deposit_Method; + } + + public int getHouse_Type_Id() { + return house_Type_Id; + } + + public void setHouse_Type_Id(int house_Type_Id) { + this.house_Type_Id = house_Type_Id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} + +package bean; + +public class House_Type { + private int id ; + private String type; + + public House_Type() { + } + + public House_Type(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; + } +} +``` + +```java +package Servlet; + +import bean.House_Info; +import util.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("/list") +public class list extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + ArrayList list = new ArrayList<>(); + String sql = "select * from house_info"; + ResultSet rs = null; + try { + rs = DBUtil.getQuery(sql); + while (rs.next()) { + int anInt = rs.getInt(1); + String mode = rs.getString(2); + double aDouble = rs.getDouble(3); + String name = rs.getString(4); + String method = rs.getString(5); + int typeId = rs.getInt(6); + String ress = rs.getString(7); + list.add(new House_Info(anInt, mode, aDouble, name, method, typeId, ress)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null, null, rs); + } + 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.House_Type; +import util.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; +import java.util.List; + +@WebServlet("/add") +public class add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + ArrayList list = new ArrayList<>(); + String sql = "select * from house_type"; + ResultSet rs = null; + try { + rs = DBUtil.getQuery(sql); + while (rs.next()) { + int anInt = rs.getInt(1); + String type = rs.getString(2); + list.add(new House_Type(anInt, type)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null, null, rs); + } + 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 sql ="insert into house_info values (?,?,?,?,?,?,?)"; + String mode = request.getParameter("mode"); + double rent = Double.parseDouble(request.getParameter("rent")); + String contacts = request.getParameter("contacts"); + String method = request.getParameter("method"); + int type = Integer.parseInt(request.getParameter("type")); + String address = request.getParameter("address"); + int update = 0; + try { + update = DBUtil.getUpdate(sql, 0, mode, rent, contacts, method, type, address); + } catch (SQLException e) { + throw new RuntimeException(e); + } + if (update>0){ + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + }else { + request.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + + } +} +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: lch + Date: 2023/6/8 + Time: 12:58 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +添加房原 + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址
${bar.id}${bar.lease_Mode}${bar.rent}${bar.contacts}${bar.deposit_Method} + + 1室1厅1卫 + + + 2室1厅1卫 + + + 3室2厅1卫 + + ${bar.address}
+ + + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: lch + Date: 2023/6/8 + Time: 14:13 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金(元)
联系人
押金方式
房屋类型 + +
详细地址
+
+ + + + +<%-- + Created by IntelliJ IDEA. + User: lch + Date: 2023/6/8 + Time: 14:31 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} +
+返回 + + +``` + diff --git "a/33 \347\206\212\346\231\257\345\263\260/20230611\344\275\234\344\270\232.md" "b/33 \347\206\212\346\231\257\345\263\260/20230611\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..ea0c9c832bccdbe2ee9313ecf54fe740b92438b1 --- /dev/null +++ "b/33 \347\206\212\346\231\257\345\263\260/20230611\344\275\234\344\270\232.md" @@ -0,0 +1,469 @@ +```java +package bean; + +public class Dept { + private int dId; + private String dName; + + public Dept() { + } + + public Dept(int dId, String dName) { + this.dId = dId; + this.dName = dName; + } + + public int getdId() { + return dId; + } + + public void setdId(int dId) { + this.dId = dId; + } + + public String getdName() { + return dName; + } + + public void setdName(String dName) { + this.dName = dName; + } + + @Override + public String toString() { + return "Dept{" + + "dId=" + dId + + ", dName='" + dName + '\'' + + '}'; + } +} + +package bean; + +public class Emp { + private int eId; + private String eName; + private String sex; + private int age; + private String tel; + private String pass; + private int dId; + + public Emp() { + } + + public Emp(int eId, String eName, String sex, int age, String tel, String pass, int dId) { + this.eId = eId; + this.eName = eName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.pass = pass; + this.dId = dId; + } + + public int geteId() { + return eId; + } + + public void seteId(int eId) { + this.eId = eId; + } + + public String geteName() { + return eName; + } + + public void seteName(String eName) { + this.eName = eName; + } + + 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 getPass() { + return pass; + } + + public void setPass(String pass) { + this.pass = pass; + } + + public int getdId() { + return dId; + } + + public void setdId(int dId) { + this.dId = dId; + } + + @Override + public String toString() { + return "Emp{" + + "eId=" + eId + + ", eName='" + eName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", pass='" + pass + '\'' + + ", dId=" + dId + + '}'; + } +} +``` + +```java +package util; + +import java.sql.*; + +public class DBUtil { //characterEncoding=utf8 + private static final String url="jdbc:mysql:///companyManager?characterEncoding=utf8"; + private static final String user="root"; + private static final String pow="root"; + 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(url, user, pow); + return conn; + } + public static ResultSet getQuery(String sql,Object ...flop) throws SQLException { + Connection conn = getConn(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setObject((i+1),flop[i]); + } + ResultSet rs = prs.executeQuery(); + return rs; + } + public static int getUpdate (String sql,Object ...flop) throws SQLException { + Connection conn = getConn(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setObject((i+1),flop[i]); + } + int i = prs.executeUpdate(); + return i; + } + public static void close(Connection conn,PreparedStatement prs,ResultSet rs){ + try { + if (rs!=null){ + rs.close(); + } + if (prs!=null){ + prs.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` + +```java +package servlet; + +import bean.Emp; +import util.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 list extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + ArrayList list = new ArrayList<>(); + String sql = "select * from emp e,Dept d where d.DeptID=e.DepId"; + ResultSet rs = null; + try { + rs = DBUtil.getQuery(sql); + while (rs.next()) { + int eId = rs.getInt(1); + String name = rs.getString(2); + String sex = rs.getString(3); + int age = rs.getInt(4); + String tel = rs.getString(5); + String pass = rs.getString(6); + int id = rs.getInt(7); + list.add(new Emp(eId, name, sex, age, tel, pass, id)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null, null, rs); + } + 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"); + ArrayList list = new ArrayList<>(); + String name = request.getParameter("name"); + String sql = "select * from emp e,Dept d where d.DeptID=e.DepId and e.EmpName like ?"; + ResultSet rs = null; + try { + rs = DBUtil.getQuery(sql, "%"+name+"%"); + while (rs.next()) { + int eId = rs.getInt(1); + String eName = rs.getString(2); + String sex = rs.getString(3); + int age = rs.getInt(4); + String tel = rs.getString(5); + String pass = rs.getString(6); + int id = rs.getInt(7); + list.add(new Emp(eId,eName,sex,age,tel,pass,id)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null, null, rs); + } + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request, response); + } +} + +package servlet; + +import bean.Dept; +import util.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 add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + ArrayList list = new ArrayList<>(); + String sql = "select * from dept"; + ResultSet rs = null; + try { + rs = DBUtil.getQuery(sql); + while (rs.next()) { + int eId = rs.getInt(1); + String eName = rs.getString(2); + list.add(new Dept(eId, eName)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null, null, rs); + } + 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 name = request.getParameter("name"); + String sex = request.getParameter("sex"); + if (sex.equals("male")){ + sex="男"; + } else if (sex.equals("female")) { + sex="女"; + } + int age = Integer.parseInt(request.getParameter("age")); + String tel = request.getParameter("tel"); + String pass = request.getParameter("pass"); + String dId = request.getParameter("dId"); + String sql ="insert into emp values (?,?,?,?,?,?,?)"; + int i = 0; + try { + i = DBUtil.getUpdate(sql, 0, name, sex, age, tel, pass, dId); + } catch (SQLException e) { + throw new RuntimeException(e); + } + if (i>0){ + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + }else { + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 15:29 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 首页 + + + +
+ + + + +
姓名:
+
+添加员工 + + + + + + + + +<%-- --%> + + + + + + + + + --%> +<%-- --%> +<%-- --%> + +
编号姓名性别年龄电话所属部门操作
${bar.eId}${bar.eName}${bar.sex}${bar.age}${bar.tel} + 开发部 + + + 测试部 + + + UI部 + +<%-- 修改删除
+ + + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 16:29 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加员工 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
姓名
性别男 + 女 +
年龄
电话
密码
所属部门 + +
+
+ + + +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 17:11 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示 + + +${msg} +
+返回首页 + + +``` +