diff --git "a/19 \345\275\255\345\213\207\346\226\214/\346\237\245\350\257\242.md" "b/19 \345\275\255\345\213\207\346\226\214/\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..70219aa01bcef74e30134e2b6d5ec191dcd40cf5 --- /dev/null +++ "b/19 \345\275\255\345\213\207\346\226\214/\346\237\245\350\257\242.md" @@ -0,0 +1,390 @@ +写一个数据库 + +```mysql +# 数据库名称:CompanyManager +create database CompanyManager charset utf8; +use CompanyManager; + +# 表: Dept (部门信息表)x`z0.0 +create table Dept +( +# 字段显示 字段名 数据类型 默认值 备注和说明 + DeptID int primary key auto_increment, # 部门编号 主键,自动增长列 + DeptName varchar(20) not null # 部门名称 不允许为空 +); +#添加数据 +insert into Dept values +(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) unicode not null, # 联系电话 不允许为空(唯一约束) + PassWord varchar(12) not null, # 密码 不允许为空 + DeptID int, # 部门编号 关联部门表DeptID字段 + foreign key (DeptID) references Dept (DeptID) #外键 +); +#添加数据 +insert into Emp values + (null,'友利奈绪','女',18,'185326','10086',4), + (null,'符玄','女',20,'155326','10086',1), + (null,'张三','男',18,'1595326','10086',3), + (null,'李四','男',18,'185123326','10086',2), + (null,'王五','男',18,'18515326','10086',2); +``` + +创建dept包 + +```java +package dept; + +public class Emp { + int empId; + String empName; + String sex; + int age; + String tel; + String password; + int deptId; + String 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(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; + } + + public Emp() { + } +} +package dept; + +public class Dept { + int deptId; + 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(int deptId, String deptName) { + this.deptId = deptId; + this.deptName = deptName; + } + + public Dept() { + } +} + + +``` + +写一个工具类 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + //定义好数据库主机,库名,字符集,用户名,密码 + static String URL = "jdbc:mysql:///companymanager?characterEncoding=utf8"; + static String user = "root"; + static String pwd = "root"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + } + + public static Connection getconn() throws SQLException { + Connection conn = DriverManager.getConnection(URL, user, pwd); + return conn; + } + + public static ResultSet query(String sql, Object... keys) { + ResultSet re = null; + try { + Connection conn = getconn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i + 1), keys[i]); + } + re = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return re; + } + + public static int s(String sql, Object... keys) { + int re = 0; + try { + Connection conn = getconn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i + 1), keys[i]); + } + re = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return re; + } +} + +``` + +写一个网页 + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-08 + Time: 16:39 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + +
+| 编号 | +姓名 | +性别 | +年龄 | +电话 | +密码 | +部门 | +
|---|---|---|---|---|---|---|
| ${charloot.deptId} | +${charloot.empName} | +${charloot.sex} | +${charloot.age} | +${charloot.tel} | +${charloot.password} | +${charloot.deptName} | +