diff --git "a/09 \346\233\271\346\255\243\346\263\242/20230609 JAVA-JDBC\346\210\277\345\261\213\347\256\241\347\220\206.md" "b/09 \346\233\271\346\255\243\346\263\242/20230609 JAVA-JDBC\346\210\277\345\261\213\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..189e05fb726c919bdd91923f2eaddf70794584cb --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20230609 JAVA-JDBC\346\210\277\345\261\213\347\256\241\347\220\206.md" @@ -0,0 +1,580 @@ +# 作业 + +## MYSQL + +### 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 + (null,'一室一厅一卫'), + (null,'两室一厅三卫'), + (null,'三室一厅二卫'); +# 表: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), # 详细地址不允许为空 + foreign key (house_type_id) references house_type (id) +); +#插入数据 +insert into house_info values + (null,'整租',3888,'张三','押一付三',1,'哈拉少'), + (null,'整租',3588,'李四','押一付三',2,'乌拉'), + (null,'合租',1888,'王五','押一付三',3,'伏特加'); +``` + +## bean + +### HouseInfo + +```java +package bean; + +public class HouseInfo { + private int id; // 编号主键,自动增长列 + private String lease_mode; //租赁方式可以为空 + private String rent; // 租金不允许为空 + private String contacts; // 联系人可以为空 + private String deposit_method; // 押金方式可以为空 + private int house_type_id; // 房屋类型外键 + private String address; //详细地址不允许为空 + private String type; //房屋类型 + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", lease_mode='" + lease_mode + '\'' + + ", rent='" + rent + '\'' + + ", contacts='" + contacts + '\'' + + ", deposit_method='" + deposit_method + '\'' + + ", house_type_id=" + house_type_id + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } + + 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 String getRent() { + return rent; + } + + public void setRent(String 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; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public HouseInfo() { + } + + public HouseInfo(int id, String lease_mode, String rent, String contacts, String deposit_method, int house_type_id, String address, String type) { + 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; + this.type = type; + } +} + +``` + +### HouseType + +```java +package bean; + +public class HouseType { + private int id; + + @Override + public String toString() { + return "HouseType{" + + "id=" + id + + ", 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; + } + + public HouseType() { + } + + public HouseType(int id, String type) { + this.id = id; + this.type = type; + } + + private String type; +} + +``` + +## DBUtil + +### DBUtil + +```java +package DBUtil; + +import java.sql.*; + +public class DBUtil { + private static String url="jdbc:mysql:///test?useSSL=false&characterEncoding=utf8"; + private static String user="root"; + private static String password="root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + System.out.println("驱动包加载异常"); + } + } + public static Connection getconn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + e.printStackTrace(); + } + return conn; + } + public static ResultSet query(String sql, Object ...key){ + ResultSet rs = null; + try { + Connection ge = getconn(); + PreparedStatement prt = ge.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + prt.setObject((i+1),key[i]); + } + rs = prt.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + } + public static int upda(String sql, Object ...key){ + int rs = 0; + try { + Connection ge = getconn(); + PreparedStatement prt = ge.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + prt.setObject((i+1),key[i]); + } + rs = prt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + } +} + +``` + +## Servler + +### Select + +```java +package Servler; + +import DBUtil.DBUtil; +import bean.HouseInfo; + +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("/sele") +public class Select extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql="select * from house_info i,house_type t where i.house_type_id=t.id "; + ResultSet que = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (que.next()){ + int id=que.getInt("id"); // 编号主键,自动增长列 + String lease_mode=que.getString("lease_mode"); //租赁方式可以为空 + String rent=que.getString("rent"); // 租金不允许为空 + String contacts=que.getString("contacts"); // 联系人可以为空 + String deposit_method=que.getString("deposit_method"); // 押金方式可以为空 + int house_type_id= que.getInt("house_type_id"); // 房屋类型外键 + String address= que.getString("address"); //地址 + String type= que.getString("type"); //房屋类型 + HouseInfo hou = new HouseInfo(id, lease_mode, rent, contacts, deposit_method, house_type_id,address,type); + list.add(hou); + } + } catch (SQLException e) { + e.printStackTrace(); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/sele.jsp").forward(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + String user = req.getParameter("user"); + String sql="select * from house_info i,house_type t where i.house_type_id=t.id and contacts like ?"; + ResultSet que = DBUtil.query(sql, "%" + user + "%"); + ArrayList list = new ArrayList<>(); + try { + while (que.next()){ + int id=que.getInt("id"); // 编号主键,自动增长列 + String lease_mode=que.getString("lease_mode"); //租赁方式可以为空 + String rent=que.getString("rent"); // 租金不允许为空 + String contacts=que.getString("contacts"); // 联系人可以为空 + String deposit_method=que.getString("deposit_method"); // 押金方式可以为空 + int house_type_id= que.getInt("house_type_id"); // 房屋类型外键 + String address= que.getString("address"); //地址 + String type= que.getString("type"); //房屋类型 + HouseInfo hou = new HouseInfo(id, lease_mode, rent, contacts, deposit_method, house_type_id,address,type); + list.add(hou); + } + } catch (SQLException e) { + e.printStackTrace(); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/sele.jsp").forward(req,resp); + } +} + +``` + +### Address + +```java +package Servler; + +import DBUtil.DBUtil; +import bean.HouseInfo; +import bean.HouseType; + +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 Address extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql="select * from house_type"; + ResultSet que = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (que.next()){ + int id=que.getInt("id"); // 编号主键,自动增长列 + String type= que.getString("type"); //房屋类型 + HouseType hou = new HouseType(id, type); + list.add(hou); + } + } 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"); + String sql="insert into house_info values (?,?,?,?,?,?,?)"; + String leaseMode = req.getParameter("lease_mode"); + String rent = req.getParameter("rent"); + String contacts = req.getParameter("contacts"); + String deposit_method = req.getParameter("deposit_method"); + String type = req.getParameter("type"); + String address = req.getParameter("address"); + int i = DBUtil.upda(sql, null, leaseMode, rent, contacts, deposit_method, type, address); + if (i>0){ + req.setAttribute("list","添加成功!"); + req.getRequestDispatcher("/WEB-INF/xinxi.jsp").forward(req,resp); + }else{ + req.setAttribute("list","添加失败!"); + req.getRequestDispatcher("/WEB-INF/xinxi.jsp").forward(req,resp); + } + + } +} + +``` + +### Delete + +```java +package Servler; + +import DBUtil.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; +@WebServlet("/dele") +public class Delete extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + String sql="delete from house_info where id=?"; + String id = req.getParameter("id"); + int i = DBUtil.upda(sql, id); + if (i>0){ + req.setAttribute("list","删除成功!!"); + req.getRequestDispatcher("/WEB-INF/dele.jsp").forward(req,resp); + }else{ + req.setAttribute("list","删除失败!"); + req.getRequestDispatcher("/WEB-INF/dele.jsp").forward(req,resp); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + } +} + +``` + +## JSP + +### sele + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址功能
${n.id}${n.lease_mode}${n.rent}${n.contacts}${n.deposit_method}${n.type}${n.address} + 删除 +
+
+ 姓名: +
+ + + +``` + +### add + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
凭租方式 + +
租金 + +
联系人 + +
押金方式 + +
房屋类型 + +
详细地址 + +
+ +
+
+ + + +``` + +### dele + +```jsp +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +

${list}

+ + + + +``` + +### xinxi + +```jsp +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 信息提示 + + +

${list}


+ + + + +``` +