文章目录
简述
复习了一下Java基础,做了一个小练习
可以实现员工的增删改查,并且将数据库连接的操作提取出来存放到工具类中,更加方便
使用的jdbc架包是5.1.22版本
idea版本为2020.1
MySQL版本5.6
蓝奏云
1.建立数据库表
CREATE TABLE `employee` (
`empno` int(4) NOT NULL AUTO_INCREMENT,
`ename` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`job` varchar(9) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`mgr` int(4) NULL DEFAULT NULL,
`hiredate` date NULL DEFAULT NULL,
`sal` double(7, 2) NULL DEFAULT NULL,
`comm` double(7, 2) NULL DEFAULT NULL,
`deptno` int(2) NULL DEFAULT NULL,
PRIMARY KEY (`empno`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
2.建立员工类(Employee)
package com.empmgr.entity;
import java.util.Date;
public class Employee {
private int empNo;
private String eName;
private String job;
private int mgr;
private Date hireDate;
private Double sal;
private Double comm;
private int deptNo;
// 构造函数
public Employee(int empNo, String eName, String job, int mgr, Date hireDate, Double sal, Double comm, int deptNo) {
this.empNo = empNo;
this.eName = eName;
this.job = job;
this.mgr = mgr;
this.hireDate = hireDate;
this.sal = sal;
this.comm = comm;
this.deptNo = deptNo;
}
public Employee() {
}
public Employee(int empNo, String job, Double sal) {
this.empNo = empNo;
this.job = job;
this.sal = sal;
}
//重构一个没id编号的,毕竟是主键,自增就行了
public Employee(String eName, String job, int mgr, Date hireDate, Double sal, Double comm, int deptNo) {
this.eName = eName;
this.job = job;
this.mgr = mgr;
this.hireDate = hireDate;
this.sal = sal;
this.comm = comm;
this.deptNo = deptNo;
}
// toString方法重构
@Override
public String toString() {
return "Employee{" +
"empNo=" + empNo +
", eName='" + eName + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hireDate=" + hireDate +
", sal=" + sal +
", comm=" + comm +
", deptNo=" + deptNo +
'}';
}
// getter和setter
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String geteName() {
return eName;
}
public void seteName(String eName) {
this.eName = eName;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Double getComm() {
return comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
public int getDeptNo() {
return deptNo;
}
public void setDeptNo(int deptNo) {
this.deptNo = deptNo;
}
}
3.创建dao层接口
package com.empmgr.dao;
import com.empmgr.entity.Employee;
import java.util.List;
public interface EmployeeDao {
// 增加