diff --git "a/18 \345\276\220\346\260\270\346\267\263/20230608 jsp\345\221\230\345\267\245\350\200\203\345\213\244\347\263\273\347\273\237.md" "b/18 \345\276\220\346\260\270\346\267\263/20230608 jsp\345\221\230\345\267\245\350\200\203\345\213\244\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..eb6306ad628fddc88c3e996ed0e25e17543ef3fa --- /dev/null +++ "b/18 \345\276\220\346\260\270\346\267\263/20230608 jsp\345\221\230\345\267\245\350\200\203\345\213\244\347\263\273\347\273\237.md" @@ -0,0 +1,400 @@ +```mysql +create database CompanyManager charset utf8; +use CompanyManager; +create table Dept +( + DeptID int primary key auto_increment, #主键 部门编号 自动增长列, + DeptName varchar(20) not null #不允许为空部门名称 +); +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 (null, '格斗部'), + (null, 'dg细胞部'), + (null, 'Gundam fight部'); +insert into Emp +values (null, 'shining', '男', 23, '123214324', '23424', 1), + (null, 'god', '男', 23, '12344444', '23424', 3), + (null, 'master', '男', 80, '123266666', '23424', 2); + + + +``` + + + +```java +bean类 +dept类型 +package bean; + +public class Dept { + private int deptID; + private String 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; + } +} + +``` + + + +```java +emp类 +package bean; + +public class Emp { + private int empId; + private String empName; + private String sex; + private int age; + private String tel; + private String pwd; + private int deptID; + private String deptName; + + public String getDeptName() { + return deptName; + } + + public void setDeptName(String deptName) { + this.deptName = deptName; + } + + public Emp(int empId, String empName, String sex, int age, String tel, String pwd, int deptID, String deptName) { + this.empId = empId; + this.empName = empName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.pwd = pwd; + this.deptID = deptID; + this.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 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 Emp() { + } + + public Emp(int empId, String empName, String sex, int age, String tel, String pwd, int deptID) { + this.empId = empId; + this.empName = empName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.pwd = pwd; + this.deptID = deptID; + } +} + +``` + + + +```java +DBUtil类 +package DBUtil; + +import java.sql.*; + +public class DBUtil { + private static final String url = "jdbc:mysql:///companyManager?useSSL=false&characterEncoding=utf8"; + private static final String a = "root"; + private static final String b = "root"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + public static Connection getCon() throws SQLException { + Connection connection = DriverManager.getConnection(url, a, b); + return connection; + } + public static ResultSet select(String sql, Object...key){ + ResultSet re = null; + try { + Connection con = getCon(); + PreparedStatement pr = con.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + pr.setObject((i+1),key[i]); + } + re = pr.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return re; + } + + public static int update(String sql, Object...key){ + int e = 0; + try { + Connection con = getCon(); + PreparedStatement pr = con.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + pr.setObject((i+1),key[i]); + } + e = pr.executeUpdate(); + } catch (SQLException ew) { + ew.printStackTrace(); + } + return e; + } +} + +``` + + + +```java +servlect类型 +package servlect; + +import DBUtil.DBUtil; +import bean.Emp; + +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 Servlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from Emp e,dept d where e.deptid=d.deptid"; + ResultSet select = DBUtil.select(sql); + ArrayList list = new ArrayList<>(); + try { + while (select.next()) { + int empid = select.getInt(1); + int age = select.getInt(4); + int deptid = select.getInt(7); + String name = select.getString(2); + String sex = select.getString(3); + String tel = select.getString(5); + String pwd = select.getString(6); + String deptname = select.getString(9); + Emp emp = new Emp(empid, name, sex, age, tel, pwd, deptid, deptname); + list.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/chaxun.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + + + +```java +查询类 +package servlect; + +import DBUtil.DBUtil; +import bean.Emp; + +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("/chaxun") +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 { + request.setCharacterEncoding("utf-8"); + String parameter = request.getParameter("username"); + + String sql = "select * from Emp e,dept d where e.deptid=d.deptid and empname like ?"; + ResultSet select = DBUtil.select(sql,"%"+parameter+"%"); + ArrayList list = new ArrayList<>(); + try { + while (select.next()) { + int empid = select.getInt(1); + int age = select.getInt(4); + int deptid = select.getInt(7); + String name = select.getString(2); + String sex = select.getString(3); + String tel = select.getString(5); + String pwd = select.getString(6); + String deptname = select.getString(9); + Emp emp = new Emp(empid, name, sex, age, tel, pwd, deptid, deptname); + list.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/chaxun.jsp").forward(request, response); + } +} + + +``` + + + +```jsp +jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 86173 + Date: 2023/6/8 + Time: 19:50 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ 姓名: +
+ + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${emp.empId}${emp.empName}${emp.sex}${emp.age}${emp.tel}${emp.deptName}
+ + + +``` +