diff --git "a/16 \346\236\227\346\210\220\351\270\277/20230607 JDBC\344\275\234\344\270\232.md" "b/16 \346\236\227\346\210\220\351\270\277/20230607 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..f77464f923165c2f1888d72ac8b7cb99a014e993 --- /dev/null +++ "b/16 \346\236\227\346\210\220\351\270\277/20230607 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,408 @@ +```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); + } + + } +} + +``` + +```java +<%@ 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} +
+返回 + + + +``` +