1.创建数据库
use teamdb;
drop table if exists employee;
drop table if exists department;
create table department
(
deptId int primary key not null,
deptName varchar(50) not null,
deptAddress varchar(200)
);
alter table department
alter deptAddress set default '地址不详';
drop table if exists employee;
create table employee
(
empId int primary key not null,
empName varchar(20) not null,
empPosition varchar(20) not null,
empHrId int,
empInDate datetime not null,
empSalary float not null,
empBonus float,
deptId int not null
);
alter table employee
change empInDate empInDate timestamp not null default NOW();
alter table employee
add constraint FK_test foreign key (deptId) REFERENCES department(deptId);
insert into department values(10, '总部', '神龙岛');
insert into department values(20, '技术部', '黑木崖');
insert into department values(30, '市场部', '光明顶');
insert into department values(40, '行政部', '嵩山');
insert into department values(50,'总部','光明顶');
insert into employee values(7369, '任盈盈', '职员', 7902, '1980-12-17', 800, NULL, 20);
insert into employee values(7499, '杨逍', '销售人员', 7698, '1981-2-20', 1600, 300, 30);
insert into employee values(7521, '范遥', '销售人员', 7698, '1981-2-22', 1250, 500, 30);
insert into employee values(7566, '任我行', '经理', 7839, '1981-4-2', 2975, NULL, 20);
insert into employee values(7654, '金毛狮王', '销售人员', 7698, '1981-9-28', 1250, 1400, 30);
insert into employee values(7698, '张无忌', '经理', 7839, '1981-5-1', 2850, NULL, 30);
insert into employee values(7782, '苏荃', '经理', 7839, '1981-6-9', 2450, NULL, 10);
insert into employee values(7788, '东方不败', '分析员', 7566, '1982-12-9', 3000, NULL, 20);
insert into employee values(7839, '王小宝', '总裁', NULL, '1981-11-17', 5000, NULL, 10);
insert into employee values(7844, '紫衫龙王', '销售人员', 7698, '1981-9-8', 1500, 0, 30);
insert into employee values(7876, '向王天', '职员', 7788, '1983-1-12', 1100, NULL, 20);
insert into employee values(7900, '小昭', '职员', 7698, '1981-12-3', 950, NULL, 30);
insert into employee values(7902, '令狐冲', '分析员', 7566, '1981-12-3', 3000, NULL, 20);
insert into employee values(7934, '双儿', '职员', 7782, '1982-1-23', 1300, NULL, 10);
insert into employee values(7933, '石中玉', '销售人员', 7782, '1984-12-23', 2850, NULL, 10);
2.在eclipse中,创建一个java项目,进行分层架构,导入对应数据库的驱动包:mqsql.jar
2.0、1. 什么是分层架构
(1) 分层就是将现有的类 分别放置在不同的包当中
(2) 不同的类
① 用于承载数据的类-- 实体类
② 用于连接数据库 进行增删改查操作 --数据访问类 DAO
③ 进行业务逻辑的处理 – 业务逻辑类 BIZ
④ 和用户进行数据交互 – 表示类 UI
2.1、实体层 存放所有所需的实体类
//根据对应的数据库表信息,进行创建对应表信息的属性
public class Employee {
/**empId int primary key not null,
empName varchar(20) not null,
empPosition varchar(20) not null,
empHrId int,
empInDate datetime not null,
empSalary float not null,
empBonus float,
deptId int not null*/
private int empId;
private String empName;
private String empPosition;
private int empHrId;
private Date empInDate;
private float empSalaty;
private float empBonus;
private int deptId;
//额外增加一个属性,是该类对应的数据表的主表的类的对象
private Department dept;
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
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 getEmpPosition() {
return empPosition;
}
public void setEmpPosition(String empPosition) {
this.empPosition = empPosition;
}
public int getEmpHrId() {
return empHrId;
}
public void setEmpHrId(int empHrId) {
this.empHrId = empHrId;
}
public Date getEmpInDate() {
return empInDate;
}
public void setEmpInDate(Date empInDate) {
this.empInDate = empInDate;
}
public float getEmpSalaty() {
return empSalaty;
}
public void setEmpSalaty(float empSalaty) {
this.empSalaty = empSalaty;
}
public float getEmpBonus() {
return empBonus;
}
public void setEmpBonus(float empBonus) {
this.empBonus = empBonus;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public Employee(int empId, String empName, String empPosition,
int empHrId, Date empInDate, float empSalaty, float empBonus,
int deptId) {
super();
this.setDeptId(deptId);
this.setEmpName(empName);
this.setEmpPosition(empPosition);
this.setEmpHrId(empHrId);
this.setEmpInDate(empInDate);
this.setEmpSalaty(empSalaty);
this.setEmpBonus(empBonus);
this.setDeptId(deptId);
}
public Employee() {
super();
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName
+ ", empPosition=" + empPosition + ", empHrId=" + empHrId
+ ", empInDate=" + empInDate + ", empSalaty=" + empSalaty
+ ", empBonus=" + empBonus + ", deptId=" + deptId + "]";
}
public class Department {
private int deptId;
private String deptName;
private String deptAddress;
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 String getDeptAddress() {
return deptAddress;
}
public void setDeptAddress(String deptAddress) {
this.deptAddress = deptAddress;
}
public Department(int deptId, String deptName, String deptAddress) {
super();
this.setDeptId(deptId);
this.setDeptName(deptName);
this.setDeptAddress(deptAddress);
}
public Department() {
super();
}
@Override
public String toString() {
return "Department [deptId=" + deptId + ", deptName=" + deptName
+ ", deptAddress=" + deptAddress + "]";
}
2.2、数据访问层 存放数据的访问类 依赖实体类
package com.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.eneity.Department;
import com.eneity.Employee;
public class EmployeeDao extends BaseDao {
//新增
public int add(Employee emp) {
String sql = "insert into employee values(?,?,?,?,?,?,?,?)";
int count = 0;
try {
super.st = super.conn.prepareStatement(sql);
// 在执行sql指令钱,要先为指令中的参数供值
super.st.setInt(1, emp.getEmpId());
super.st.setString(2, emp.getEmpName());
super.st.setString(3, emp.getEmpPosition());
super.st.setInt(4, emp.getEmpHrId());
// 这里类型转换运行报错
super.st.setDate(5, new java.sql.Date(emp.getEmpInDate().getTime()));// 为sql指令供值
// 提供的应该是数据库所需的数据
super.st.setFloat(6, emp.getEmpSalaty());
super.st.setFloat(7, emp.getEmpBonus());
super.st.setInt(8, emp.getDeptId());
// 执行
count = super.st.executeUpdate();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
super.close_db();
}
return count;
}
// 删除
public int delete(Employee emp) {
String sql = "delete from employee where empId = ?;";
int count = 0;
try {
super.st = super.open_db().prepareStatement(sql);
// 在执行sql指令前,要先为指令中的参数供值
super.st.setInt(1, emp.getEmpId());
// 执行
count = super.st.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
close_db();
}
return count;
}
// 修改
public int modify(Employee emp) {
// 组建SQL指令,set要修改多少列,就在设置多少列的数据
String sql = "update employee set empName = ? where empId=?;";
int count = 0;
try {
super.st = super.open_db().prepareStatement(sql);
super.st.setString(1, emp.getEmpName());
super.st.setInt(2,emp.getEmpId());
// 执行
count = super.st.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
} finally {
close_db();
}
return count;
}
DepartmentDao ddao = new DepartmentDao();
// 全查
public List<Employee> find() {
String sql = "select * from employee;";
List<Employee> emps = new ArrayList<Employee>();// 装载数据结果集的容器
try {
super.st = super.open_db().prepareStatement(sql);
// 为sql指令供职
// super.st.setInt(1, id);
// 查询需要使用结果集
super.rs = super.st.executeQuery();
// 解析结果集,组装集合
Employee emp;
while (super.rs.next()) {
emp = new Employee();
emp.setEmpId(super.rs.getInt(1));
emp.setEmpName(super.rs.getString(2));
emp.setEmpPosition(super.rs.getString(3));
emp.setEmpHrId(super.rs.getInt(4));
// // 这里类型转换运行报错
super.st.setDate(5,new java.sql.Date(emp.getEmpInDate().getTime()));
// 为sql指令供值 提供的应该是数据库所需的数据
emp.setEmpSalaty(super.rs.getFloat(6));
emp.setEmpBonus(super.rs.getFloat(7));
emp.setDeptId(super.rs.getInt(8));
emps.add(emp);
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return emps;
}
// 查询主键列
public List<Employee> primay() {
String sql = "select empId from employee";
List<Employee> emps = new ArrayList<Employee>();// 装载数据结果集的容器
Employee emp = null;
try {
super.st = super.open_db().prepareStatement(sql);
// 为sql指令供职
super.rs = super.st.executeQuery();
while(super.rs.next()){
emp = new Employee();
emp.setEmpId(super.rs.getInt(1));
emps.add(emp);
}
} catch (Exception e) {
// TODO: handle exception
}
return emps;
}
// 根据提供的编号查询部门对象
public Employee findById(int id) {
String sql = "select * from employee where empId =?";
Employee emp = null;
try {
super.st = super.open_db().prepareStatement(sql);
// 为sql指令供职
super.st.setInt(1, id);
// 查询主键,提供结果集
super.rs = super.st.executeQuery();
while (super.rs.next()) {
emp = new Employee();
emp.setEmpId(super.rs.getInt(1));
// emp.setEmpName(super.rs.getString(2));
// emp.setEmpPosition(super.rs.getString(3));
// emp.setEmpHrId(super.rs.getInt(4));
// // 这里类型转换运行报错
// super.st.setDate(5, new java.sql.Date(emp.getEmpInDate().getTime()));// 为sql指令供值 提供的应该是数据库所需的数据
// emp.setEmpSalaty(super.rs.getFloat(6));
// emp.setEmpBonus(super.rs.getFloat(7));
// emp.setDeptId(super.rs.getInt(8));
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return emp;
}
//通用查询
public List<Employee> query(Map<String, Object> pars){
List<Object> objs = new ArrayList<Object>();//存储所有键值对集合中的值,维持存储和提取顺序一致
List<Employee> emps = new ArrayList<Employee>();
//部门的缓存集合
List<Department> depts = ddao.find();
//sql指令
StringBuffer sql = new StringBuffer("select * from employee where 1=1");
//方式2:如果所有的条件,都为等值比较,那么可以使用foreach
for (String s : pars.keySet()) {
sql.append(" and ").append(s).append(" = ?");
//将对应键的值,存入list集合 ,维持顺序
objs.add(pars.get(s));
}
//方式1 如果条件的比较方式各不相同
// if (pars.containsKey("empId")) {
// sql.append(" and empId=? ");
// }
// if (pars.containsKey("empName")) {
//// sql.append(" and empName=? ");
// sql.append(" and instr(empName,?)>0");
// }
// System.out.println(sql.toString());
try {
//开启连接,创建适配器
super.st = super.open_db().prepareStatement(sql.toString());
//为sql的 ? 供职
// int i = 1;
for (int j = 0; j < objs.size(); j++) {
super.st.setObject(j+1, objs.get(j));//从集合中取出顺序下标的数据
}
//执行操作
super.rs = super.st.executeQuery();
//解析结果集
Employee emp = null;
while(super.rs.next()){
emp = new Employee();
//拆分本行中的各个 单元格的数据,存入 对象的属性中
emp.setEmpId(super.rs.getInt("empId"));
emp.setEmpName(super.rs.getString("empName"));
emp.setEmpPosition(super.rs.getString("empPosition"));
emp.setEmpHrId(super.rs.getInt("empHrid"));
emp.setEmpInDate(super.rs.getDate("empInDate"));//分类类型的对象,存子类类型的对象
emp.setEmpSalaty(super.rs.getFloat("empSalary"));
emp.setEmpBonus(super.rs.getFloat("empBonus"));
emp.setDeptId(super.rs.getInt("deptId"));
//emp对象还有一个属性,是该员工对应 的部门对象
//部门编号怎么转换从部门对象
for (int i = 0; i < depts.size(); i++) {
if (depts.get(i).getDeptId() == emp.getDeptId()) {
emp.setDept(depts.get(i));
}
}
// Department d = ddao.findById(emp.getDeptId());
// emp.setDept(d);
//组装完成后,存入集合中
emps.add(emp);
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}finally{
close_db();
}
return emps;
}
}
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.eneity.Department;
public class DepartmentDao extends BaseDao {
// 新增
public int add(Department dept) {
// SQL指令
String sql = "insert into department values(?,?,?);";
int count = 0;
try {
super.st = super.open_db().prepareStatement(sql);
// 在执行sql指令钱,要先为指令中的参数供值
super.st.setInt(1, dept.getDeptId());
super.st.setString(2, dept.getDeptName());
super.st.setString(3, dept.getDeptAddress());
// 执行
count = super.st.executeUpdate();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
close_db();
}
return count;
}
// 删除
public int delete(Department dept) {
String sql = "delete from department where deptId = ?";
int count = 0;
try {
super.st = super.open_db().prepareStatement(sql);
// 在执行sql指令前,要先为指令中的参数供值
super.st.setInt(1, dept.getDeptId());
// 执行
count = super.st.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
close_db();
}
return count;
}
// 修改
public int modify(Department dept) {
// 组建SQL指令
String sql = "update department set deptAddress=?,deptName=? where deptId=?;";
int count = 0;
try {
super.st = super.open_db().prepareStatement(sql);
super.st.setString(1, dept.getDeptAddress());
super.st.setString(2, dept.getDeptName());
super.st.setInt(3, dept.getDeptId());
// 执行
count = super.st.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
} finally {
close_db();
}
return count;
}
// 全查
public List<Department> find() {
String sql = "select * from department ;";
List<Department> depts = new ArrayList<Department>();// 装载数据结果集的容器
try {
super.st = super.open_db().prepareStatement(sql);
// 为sql指令供职
// super.st.setInt(1, id);
// 查询需要使用结果集
super.rs = super.st.executeQuery();
// 解析结果集,组装集合
Department dept;
while (super.rs.next()) {
dept = new Department();
dept.setDeptId(super.rs.getInt(1));
dept.setDeptName(super.rs.getString(2));
dept.setDeptAddress(super.rs.getString(3));
depts.add(dept);
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return depts;
}
// 查询主键列
public List<Department> primay() {
String sql = "select deptId from department";
List<Department> depts = new ArrayList<Department>();// 装载数据结果集的容器
Department dept = null;
try {
super.st = super.open_db().prepareStatement(sql);
// 为sql指令供职
super.rs = super.st.executeQuery();
while(super.rs.next()){
dept = new Department();
dept.setDeptId(super.rs.getInt(1));
depts.add(dept);
}
} catch (Exception e) {
// TODO: handle exception
}
return depts;
}
// 根据提供的编号查询部门对象,主键查询
public Department findById(int id) {
String sql = "select *from department where deptId = ?;";
Department dept = null;
try {
super.st = super.open_db().prepareStatement(sql);
// 为sql指令供职
super.st.setInt(1, id);
// 查询需要使用结果集
super.rs = super.st.executeQuery();
while (super.rs.next()) {
dept = new Department();
dept.setDeptId(super.rs.getInt(1));
dept.setDeptName(super.rs.getString(2));
dept.setDeptAddress(super.rs.getString(3));
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return dept;
}
}
2.3、上面两个类,都继承于BaseDao这个工具类,用来创建连接Mqsql数据库
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://localhost:3306/teamdb";
public static final String UID = "root";
public static final String PWD = "root";
protected Connection conn = null;
// protected Statement st = null;
protected PreparedStatement st = null;
protected ResultSet rs =null;
static{
//在类一创建的时候就加载驱动
try {
Class.forName(DRIVER);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public Connection open_db(){
try {
conn = DriverManager.getConnection(URL,UID,PWD);
//不能再这里创建适配器
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
public void close_db(){
try {
if (rs!=null) {
rs.close();
}
if (st!=null) {
st.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e2) {
e2.printStackTrace();
}
}
}
2.4 、业务逻辑层 依赖实体类 依赖数据访问类
package com.biz;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dao.EmployeeDao;
import com.eneity.Department;
import com.eneity.Employee;
public class EmployeeBiz {
EmployeeDao edao = new EmployeeDao();
// 1.负责传递 承上启下
// 2.实现数据验证 调用数据访问层的多个方法
// 3.可以对数据访问层返回的数据进行处理
public String add(Employee emp){
//数据的完整性验证
//验证新增的部门编号是否和现有的部门编号信息重复
Employee e = edao.findById(emp.getEmpId());
if (e!= null) {
return "该员工信息已存在!";
}else {
return edao.add(emp) == 1 ? "员工表新增成功!" : "员工表新增失败!";
}
}
// 删除
public String delete(Employee emp) {
Employee d = edao.findById(emp.getEmpId());
if (d == null) {
return "部门编号不存在";
} else {
return edao.delete(emp) == 1 ? "删除员工成功!" : "删除员工失败! ";
}
}
//修改
public String modify(Employee emp){
Employee e = edao.findById(emp.getEmpId());
if (e == null) {
return "没有可以更新的部门编号";
}else {
return edao.modify(emp) == 1 ? "更新成功" :"更新失败";
}
}
//全查
public List<Employee> find(Employee emp){
List<Employee> es = edao.find();
return es;
}
//根据主键查询
public List<Employee> primay(Employee emp){
List<Employee> e = edao.primay();
return e;
}
//方法2--根据主键查询
public Employee findById(int id){
Map<String, Object> pars = new HashMap<String, Object>();
pars.put("empId", id);
//该集合最多包含一个对象或者没有对象
List<Employee> emps =edao.query(pars);
return emps.size() ==1 ? null : emps.get(0);
}
//方法2-全查询
public List<Employee> findAll(){
Map<String, Object> pars = new HashMap<String, Object>();
return edao.query(pars);
}
//其他条件查询员工
public List<Employee > query(Map<String, Object> pars){
return edao.query(pars);
}
}
package com.biz;
import java.util.List;
import com.dao.DepartmentDao;
import com.eneity.Department;
public class DepartmentBiz {
DepartmentDao ddao = new DepartmentDao();
// 1.负责传递 承上启下
// 2.实现数据验证 调用数据访问层的多个方法
// 3.可以对数据访问层返回的数据进行处理
public String add(Department dept) {
// 数据的完整性验证
// 验证新增的部门编号是否和现有的部门编号信息重复
Department d = ddao.findById(dept.getDeptId());
if (d == null) {
return ddao.add(dept) == 1 ? "新增部门成功!" : "新增部门失败! ";
} else {
return "部门编号存在";
}
}
// 删除
public String delete(Department dept) {
Department d = ddao.findById(dept.getDeptId());
if (d == null) {
return "部门编号不存在";
} else {
return ddao.delete(dept) == 1 ? "删除部门成功!" : "删除部门失败! ";
}
}
//修改
public String modify(Department dept){
Department d = ddao.findById(dept.getDeptId());
if (d == null) {
return "没有可以更新的部门编号";
}else {
return ddao.modify(dept) == 1 ? "更新成功" :"更新失败";
}
}
//全查
public List<Department> find(Department dept){
List<Department> ds = ddao.find();
return ds;
}
//根据主键查询
public List<Department> primay(Department dept){
List<Department> d = ddao.primay();
return d;
}
}
2.5、表示层/用户接口 和用户进行数据交互
package ui;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import com.biz.EmployeeBiz;
import com.dao.EmployeeDao;
import com.eneity.Department;
import com.eneity.Employee;
public class Test1 {
public static void main(String[] args) {
Employee emp = new Employee();
Scanner input = new Scanner(System.in);
// 新增
/* Employee emp = new Employee();
System.out.println("请输入新增员工的工号:");
emp.setEmpId(input.nextInt());
System.out.println("请输入新增员工的名称:");
emp.setEmpName(input.next());
System.out.println("请输入新增员工的职位:");
emp.setEmpPosition(input.next());
System.err.println("请输入新增员工的上一级:");
emp.setEmpHrId(input.nextInt());
// System.out.println("请输入新增员工的出生日期:");
System.out.println("请输入员工的入职日期:(yyyy/MM/dd)");
String date = input.next();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");// 日期格式化类
// 完整类型转换
try {
// 自动获取当前时间
emp.setEmpInDate(sdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
// System.out.println(emp.getEmpInDate());
// //String -- util.Date
System.out.println("请输入新增员工的工资:");
emp.setEmpSalaty(input.nextFloat());
System.out.println("请输入新增员工的奖金:");
emp.setEmpBonus(input.nextFloat());
System.out.println("请输入新增员工的部门编号:");
emp.setDeptId(input.nextInt());
EmployeeBiz biz = new EmployeeBiz();
System.err.println(biz.add(emp));*/
// 删除
// System.out.println("请输入要删除的员工编号:");
// emp.setEmpId(input.nextInt());
EmployeeBiz biz = new EmployeeBiz();
// System.out.println(biz.delete(emp));
//修改
//更新
// System.err.println("请输入更新的员工名称:");
// emp.setEmpName(input.next());
// System.out.println("请输入更新的员工编号:");
// emp.setEmpId(input.nextInt());
// System.err.println(biz.modify(emp));
// 全查询
// List<Employee> es = biz.find(emp);
// for(Employee employee : es){
// System.out.println(employee.toString());
// }
// 主键查询
// List<Employee> es = biz.primay(emp);
// for(Employee employee :es){
// System.out.println("主键列:" + employee.getEmpId());
// }
// Map<String, Object> pars = new HashMap<String,Object>();
// System.out.println("请输入要查询的员工工号:(*代表任意)");
// String id =input.next();
// if (!id.equals("*")) {
// pars.put("empId", id);
// }
// System.out.println("请输入要查询的员工姓名:(*代表任意)");
// String name = input.next();
// if (!name.equals("*")) {
// pars.put("empName", name);
// }
// EmployeeDao dao = new EmployeeDao();
// dao.query(pars);
//查询所有员工的 姓名 部门名称 部门所在地
EmployeeBiz ebiz=new EmployeeBiz();
List<Employee> emps=ebiz.findAll();
System.out.println(emps.size());
for (Employee e : emps) {
System.out.println(e.getEmpName()+"\t"+e.getDept().getDeptName()+"\t"+e.getDept().getDeptAddress());
}
}
}
2.6、日期类型的转换问题
(1) Java.util.Date-- java.sql.Date
Java.util.Date— getTime()–long–作为参数放到sql.Date(long);
(2) String(2020-3-12) — java.util.Date-- java.sql.Date
String date=input.next();
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy/MM/dd”);//日期格式化类 完整类型转换
try {
emp.setEmpInDate(sdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
JDBC连接Mysql数据库的jar包
https://download.csdn.net/download/weixin_43244120/12258204