Java面向对象—外键处理和日期处理

java面向对象程序设计 通过继承 Exception 或 RuntimeException 类来创建自定义异常,满足特定业务需求。// 自定义用户不存在异常// 自定义密码错误异常if (!// 抛出用户不存在异常throw new UserNotFoundException("用户不存在");if (!// 抛出密码错误异常throw new PasswordWrongException("密码错误");System.out.println("登录成功");try {// 调用登录方法,可能抛出异常。 阅读详情

外键和日期时间处理

一、外键

上一节我们写了部门表,本节我们以员工表为例:

1、员工实体【Employee】

package _15MVC.d01.cn.com.entity;
​
/**员工实体
 * @author LX
 * @create 2023-12-24 20:24
 */
public class Employee {
    private String emp_no;
    private String emp_name;
    private String emp_sex;
    private int emp_age;
    //private int dep_no;//不需要外键字段,外键封装在主表对象部门中
    private Departement departement;//为外键准备
​
    public Employee() {
    }
    public Employee(String emp_no, String emp_name, String emp_sex, int emp_age, Departement departement) {
        this.emp_no = emp_no;
        this.emp_name = emp_name;
        this.emp_sex = emp_sex;
        this.emp_age = emp_age;
        this.departement = departement;
    }
    public Departement getDepartement() {
        return departement;
    }
    public void setDepartement(Departement departement) {
        this.departement = departement;
    }
    public String getEmp_no() {
        return emp_no;
    }
    public void setEmp_no(String emp_no) {
        this.emp_no = emp_no;
    }
    public String getEmp_name() {
        return emp_name;
    }
    public void setEmp_name(String emp_name) {
        this.emp_name = emp_name;
    }
    public String getEmp_sex() {
        return emp_sex;
    }
    public void setEmp_sex(String emp_sex) {
        this.emp_sex = emp_sex;
    }
    public int getEmp_age() {
        return emp_age;
    }
    public void setEmp_age(int emp_age) {
        this.emp_age = emp_age;
    }
}

2、员工dao层接口

package _15MVC.d01.cn.com.dao;
​
import _15MVC.d01.cn.com.entity.Employee;
import java.util.List;
​
/**员工表dao
 * @author LX
 * @create 2023-12-27 17:04
 */
public interface EmployeeDao {
    public int insert(Employee employee);
    public int delete(Employee employee);
    public int update(Employee employee);
    public List<Employee> selectAll();
    public Employee selectById(Employee employee);
}

3、员工dao层接口的实现类

package _15MVC.d01.cn.com.dao.impl;
​
import _15MVC.d01.cn.com.dao.BaseDao;
import _15MVC.d01.cn.com.dao.EmployeeDao;
import _15MVC.d01.cn.com.entity.Departement;
import _15MVC.d01.cn.com.entity.Employee;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * @author LX
 * @create 2023-12-27 17:07
 */
public class EmployeeDaoImpl extends BaseDao implements EmployeeDao {
    //增加员工
    public int insert(Employee employee) {
        int i=-1;
        openCon();
        String sql="insert into employee(emp_no,emp_name,emp_sex,emp_age,dep_no) values(?,?,?,?,?)";
        try {
            pst=con.prepareStatement(sql);
            pst.setString(1,employee.getEmp_no());
            pst.setString(2,employee.getEmp_name());
            pst.setString(3,employee.getEmp_sex());
            pst.setInt(4,employee.getEmp_age());
            //外键占位符赋值
            pst.setInt(5,employee.getDepartement().getDep_no());
            i=pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }
    //删除员工
    public int delete(Employee employee) {
        int i=-1;
        openCon();
        String sql="delete from employee where emp_no=?";
        try {
            pst=con.prepareStatement(sql);
            pst.setString(1,employee.getEmp_no());
            i=pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }
    //修改员工
    public int update(Employee employee) {
        int i=-1;
        openCon();
        String sql="update employee set emp_name=? where emp_no=?";
        try {
            pst=con.prepareStatement(sql);
            pst.setString(1,employee.getEmp_name());
            pst.setString(2,employee.getEmp_no());
            i=pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }
    //查询所有员工
    public List<Employee> selectAll() {
        List<Employee> list=null;
        openCon();
        String sql="select * from employee";
        try {
            pst=con.prepareStatement(sql);
            ResultSet rs =pst.executeQuery();
            list=new ArrayList<Employee>();
            while (rs.next()){
                Employee employee=new Employee();
                employee.setEmp_no(rs.getString("emp_no"));
                employee.setEmp_name(rs.getString("emp_name"));
                employee.setEmp_sex(rs.getString("emp_sex"));
                employee.setEmp_age(rs.getInt("emp_age"));
                //外键赋值,先存外键值到部门对象,在把部门对象存入员工对象
                Departement d=new Departement();
                d.setDep_no(rs.getInt("dep_no"));
                employee.setDepartement(d);
                list.add(employee);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }
    //根据id查询员工信息
    public Employee selectById(Employee employee) {
        Employee e=new Employee();
        openCon();
        String sql="select * from employee where emp_no=?";
        try {
            pst=con.prepareStatement(sql);
            pst.setString(1,employee.getEmp_no());
            //查询返回结果集
            rs= pst.executeQuery();
            while(rs.next()){
                //从结果集中把数据取出放到对象里
                e.setEmp_no(rs.getString("emp_no"));
                e.setEmp_name(rs.getString("emp_name"));
                e.setEmp_sex(rs.getString("emp_sex"));
                e.setEmp_age(rs.getInt("emp_age"));
                //先存外键值到部门对象,在把部门对象存入员工对象
                Departement d=new Departement();
                d.setDep_no(rs.getInt("dep_no"));
                e.setDepartement(d);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return e;
    }
}

4、员工service层接口和实现层

package _15MVC.d01.cn.com.service;
​
import _15MVC.d01.cn.com.entity.Employee;
import java.util.List;
/**
 * @author LX
 * @create 2023-12-27 17:38
 */
public interface EmployeeService {
    public boolean add(Employee employee);
    public boolean remove(Employee employee);
    public boolean modify(Employee employee);
    public List<Employee> findAll();
    public Employee findById(Employee employee);
}
package _15MVC.d01.cn.com.service.impl;
​
import _15MVC.d01.cn.com.dao.EmployeeDao;
import _15MVC.d01.cn.com.dao.impl.EmployeeDaoImpl;
import _15MVC.d01.cn.com.entity.Employee;
import _15MVC.d01.cn.com.service.EmployeeService;
import java.util.List;
/**
 * @author LX
 * @create 2023-12-27 17:40
 */
public class EmployeeServiceImpl implements EmployeeService {
    //上调下,私有化
    private EmployeeDao employeeDao=new EmployeeDaoImpl();
    public boolean add(Employee employee) {
        return employeeDao.insert(employee)>0;
    }
    public boolean remove(Employee employee) {
        return employeeDao.delete(employee)>0;
    }
    public boolean modify(Employee employee) {
        return employeeDao.update(employee)>0;
    }
    public List<Employee> findAll() {
        return employeeDao.selectAll();
    }
    public Employee findById(Employee employee) {
        return employeeDao.selectById(employee);
    }
}

5、测试类

package _15MVC.d01.cn.com.test;
​
import _15MVC.d01.cn.com.entity.Departement;
import _15MVC.d01.cn.com.entity.Employee;
import _15MVC.d01.cn.com.service.DepartmentService;
import _15MVC.d01.cn.com.service.EmployeeService;
import _15MVC.d01.cn.com.service.impl.DepartmentServiceImpl;
import _15MVC.d01.cn.com.service.impl.EmployeeServiceImpl;
import java.util.List;
import java.util.Scanner;
/**
 * @author LX
 * @create 2023-12-24 22:42
 */
public class Test {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int var=0;//菜单判断,执行什么操作
        boolean flag=false;//接收结果
        DepartmentService departmentService=new DepartmentServiceImpl();
//        Departement departement=null;//在使用时需要先new,否则会报空指针异常
        Departement departement=new Departement();
        EmployeeService employeeService=new EmployeeServiceImpl();
        Employee employee=new Employee();
​
        do {
            System.out.println("----------*欢迎使用员工管理系统*----------");
            System.out.println("----------*增加部门信息请按11*增加员工信息请按12*----------");
            System.out.println("----------*删除部门信息请按21*删除员工信息请按22*----------");
            System.out.println("----------*修改部门信息请按31*修改员工信息请按32*----------");
            System.out.println("----------*查询所有部门信息请按41*查询所有员工信息请按42*----------");
            System.out.println("----------*通过部门编号查询部门请按51*通过员工编号查询员工请按52*----------");
            System.out.println("----------*结束使用请按0*----------");
            System.out.println("----------*请输入你的选择:*----------");
            var=input.nextInt();
            switch (var){
                case 11:
                    System.out.println("----------请输入部门编号:----------");
                    int depno=input.nextInt();
                    System.out.println("----------请输入部门名称:----------");
                    String depname=input.next();
                    System.out.println("----------请输入部门位置:----------");
                    String deploc=input.next();
                    departement.setDep_no(depno);
                    departement.setDep_name(depname);
                    departement.setDep_loc(deploc);
                    flag=departmentService.add(departement);
                    if (flag){
                        System.out.println("添加部门信息成功!");
                    }else{
                        System.out.println("添加部门信息失败!");
                    }
                    break;
                case 21:
                    System.out.println("----------请输入部门编号:----------");
                    depno=input.nextInt();
                    departement.setDep_no(depno);
                    flag=departmentService.remove(departement);
                    if (flag){
                        System.out.println("删除成功!");
                    }else {
                        System.out.println("删除失败!");
                    }
                    break;
                case 31:
                    System.out.println("----------请输入要修改的部门编号:----------");
                    depno=input.nextInt();
                    System.out.println("----------请输入新的部门名称:----------");
                    depname=input.next();
                    flag=departmentService.modify(departement);
                    if (flag) {
                        System.out.println("修改成功!");
                    }else {
                        System.out.println("修改失败!");
                    }
                    break;
                case 41:
                    System.out.println("部门编号\t部门名称\t部门位置");
                    List<Departement> list=departmentService.findAll();
                    for (Departement d:list) {
                        System.out.println(d.getDep_no()+"\t\t"+d.getDep_name()+"\t"+d.getDep_loc());
                    }
                    break;
                case 51:
                    System.out.println("----------请输入要查询的部门编号:----------");
                    depno=input.nextInt();
                    departement.setDep_no(depno);
                    departement=departmentService.findById(departement);
                    System.out.println("部门编号\t部门名称\t部门位置");
                    System.out.println(departement.getDep_no()+"\t"+departement.getDep_name()+"\t"+departement.getDep_loc());
                    break;
                case 12:
                    System.out.println("----------请输入员工编号:----------");
                    String empno=input.next();
                    System.out.println("----------请输入员工姓名:----------");
                    String empname=input.next();
                    System.out.println("----------请输入员工性别:----------");
                    String empsex=input.next();
                    System.out.println("----------请输入员工年龄:----------");
                    int empage=input.nextInt();
                    System.out.println("----------请输入部门编号:----------");
                    depno=input.nextInt();
                    departement.setDep_no(depno);//先把部门编号存到部门对象
                    employee.setEmp_no(empno);
                    employee.setEmp_name(empname);
                    employee.setEmp_sex(empsex);
                    employee.setEmp_age(empage);
                    employee.setDepartement(departement);//在把部门对象存到员工对象
                    flag=employeeService.add(employee);
                    if (flag){
                        System.out.println("添加员工信息成功!");
                    }else{
                        System.out.println("添加员工信息失败!");
                    }
                    break;
                case 22:
                    System.out.println("----------请输入员工编号:----------");
                    empno=input.next();
                    employee.setEmp_no(empno);
                    flag=employeeService.remove(employee);
                    if (flag){
                        System.out.println("删除员工成功!");
                    }else {
                        System.out.println("删除员工失败!");
                    }
                    break;
                case 32:
                    System.out.println("----------请输入要修改的员工编号:----------");
                    empno=input.next();
                    System.out.println("----------请输入新的员工姓名:----------");
                    empname=input.next();
                    employee.setEmp_no(empno);
                    employee.setEmp_name(empname);
                    flag=employeeService.modify(employee);
                    if (flag) {
                        System.out.println("修改员工姓名成功!");
                    }else {
                        System.out.println("修改员工姓名失败!");
                    }
                    break;
                case 42:
                    System.out.println("员工编号\t\t员工姓名\t\t员工性别\t\t员工年龄\t\t所属部门编号");
                    List<Employee> list1=employeeService.findAll();
                    for (Employee e:list1) {
                        System.out.println(e.getEmp_no()+"\t"+e.getEmp_name()+"\t"+e.getEmp_sex()+"\t"+e.getEmp_age()+"\t"+e.getDepartement().getDep_no());
                    }
                    break;
                case 52:
                    System.out.println("----------请输入要查询的员工编号:----------");
                    empno=input.next();
                    employee.setEmp_no(empno);
                    employee=employeeService.findById(employee);
                    System.out.println("员工编号\t员工姓名\t员工性别\t员工年龄\t所属部门编号");
                    System.out.println(employee.getEmp_no()+"\t"+employee.getEmp_name()+"\t"+employee.getEmp_sex()+"\t"+employee.getEmp_age()+"\t"+employee.getDepartement().getDep_no());
                    break;
                case 0:
                    System.out.println("操作结束,欢迎下次光临,再见!!!");
                    break;
                default:
                    System.out.println("输入错误,请重新选择");
                    break;
            }
        } while (var!=0);
       /*System.out.println("-----------增加-----------");
        departement=new Departement(7,"开发部","222");
        if (departmentService.add(departement)) {
            System.out.println("添加成功!");
        }else {
            System.out.println("添加失败!");
        }
​
        System.out.println("-----------删除-----------");
        departement=new Departement(7,"开发部","222");
        if (departmentService.remove(departement)) {
            System.out.println("删除成功!");
        }else {
            System.out.println("删除失败!");
        }
​
        System.out.println("-----------修改-----------");
        departement=new Departement(6,"运维部");
        if (departmentService.modify(departement)) {
            System.out.println("修改成功!");
        }else {
            System.out.println("修改失败!");
        }
​
        System.out.println("-----------查询全部-----------");
        List<Departement> list=departmentService.findAll();
        for (Departement d:list) {
            System.out.println(d.getDep_no()+"\t"+d.getDeo_name()+"\t"+d.getDep_loc());
        }
​
        System.out.println("-----------通过id查询-----------");
        departement=new Departement(6,"运维部","223");
        departement=departmentService.findById(departement);
        System.out.println(departement.getDep_no()+"\t"+departement.getDeo_name()+"\t"+departement.getDep_loc());
    */
​
       /* System.out.println("--------增加员工----------");
        departement=new Departement(2);
        employee=new Employee("mvc01","张大","男",20,departement);
        if (employeeService.add(employee)) {
            System.out.println("添加成功!");
        }else{
            System.out.println("添加失败!");
        }*/
    }
}

总结:外键字段可以看成主表对象,无论是赋值还是取值都用主表对象进行存取

二、日期时间处理

可以先编写日期时间转换的工具类:

package _15MVC.d01.cn.com.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * @author LX  日期和字符串时间转换工具类
 * @create 2024-02-20 12:06
 */
public class DateOrString {
    //方法声明成静态的,就可以直接用类名调用了
    public static Date StrToDate(String str){
        Date date=null;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {//解析
            date=sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
​
    }
    public static String DateTostr(Date date){
        String str=null;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        //格式化
        str=sdf.format(date);
        return str;
    }
}

这里我们以年级表和学生表为例:

年级表我们已经完成,这里就不做展示了。主要看学生表中的日期时间处理:

1、学生实体:【时间属性声明为java.util.Date】

package _15MVC.d01.cn.com.entity;
import java.util.Date;
/**
 * @author LX
 * @create 2024-02-20 10:55
 */
public class Student {
    private int stu_no;//编号
    private String stu_pwd;//密码
    private String stu_name;//姓名
    private String stu_sex;//性别
    private Grade grade;//年级编号,外键字段,使用对象表示
    private String stu_phone;//电话
    private String stu_add;//地址
    private Date stu_born;//出生日期
    private String stu_email;//邮箱
​
    public Student() {
    }
    public Student(int stu_no, String stu_pwd, String stu_name, String stu_sex, Grade grade, String stu_phone, String stu_add, Date stu_born, String stu_email) {
        this.stu_no = stu_no;
        this.stu_pwd = stu_pwd;
        this.stu_name = stu_name;
        this.stu_sex = stu_sex;
        this.grade = grade;
        this.stu_phone = stu_phone;
        this.stu_add = stu_add;
        this.stu_born = stu_born;
        this.stu_email = stu_email;
    }
    public int getStu_no() {
        return stu_no;
    }
    public void setStu_no(int stu_no) {
        this.stu_no = stu_no;
    }
    public String getStu_pwd() {
        return stu_pwd;
    }
    public void setStu_pwd(String stu_pwd) {
        this.stu_pwd = stu_pwd;
    }
    public String getStu_name() {
        return stu_name;
    }
    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }
    public String getStu_sex() {
        return stu_sex;
    }
    public void setStu_sex(String stu_sex) {
        this.stu_sex = stu_sex;
    }
    public Grade getGrade() {
        return grade;
    }
    public void setGrade(Grade grade) {
        this.grade = grade;
    }
    public String getStu_phone() {
        return stu_phone;
    }
    public void setStu_phone(String stu_phone) {
        this.stu_phone = stu_phone;
    }
    public String getStu_add() {
        return stu_add;
    }
    public void setStu_add(String stu_add) {
        this.stu_add = stu_add;
    }
    public Date getStu_born() {
        return stu_born;
    }
    public void setStu_born(Date stu_born) {
        this.stu_born = stu_born;
    }
    public String getStu_email() {
        return stu_email;
    }
    public void setStu_email(String stu_email) {
        this.stu_email = stu_email;
    }
}

2、学生dao层接口

package _15MVC.d01.cn.com.dao;
import _15MVC.d01.cn.com.entity.Student;
import java.util.List;
/**
 * @author LX
 * @create 2024-02-20 12:08
 */
public interface StudentDao {
    public int insert(Student student);
    public int delete(Student student);
    public int update(Student student);
    public List<Student> selectAll();
}

3、学生dao的实现类【在给时间字段的占位符赋值时需要用时间戳】

package _15MVC.d01.cn.com.dao.impl;
​
import _15MVC.d01.cn.com.dao.BaseDao1;
import _15MVC.d01.cn.com.dao.StudentDao;
import _15MVC.d01.cn.com.entity.Grade;
import _15MVC.d01.cn.com.entity.Student;
​
import javax.management.OperationsException;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
​
/**
 * @author LX
 * @create 2024-02-20 12:11
 */
public class StudentDaoImpl extends BaseDao1 implements StudentDao {
    @Override
    public int insert(Student student) {
        int i=-1;
        open();
        String sql="insert into student(stu_no,stu_pwd,stu_name,stu_sex,grad_no,stu_phone,stu_add,stu_born,stu_email) values(?,?,?,?,?,?,?,?,?)";
        try {
            pst=con.prepareStatement(sql);
            pst.setInt(1,student.getStu_no());
            pst.setString(2,student.getStu_pwd());
            pst.setString(3,student.getStu_name());
            pst.setString(4,student.getStu_sex());
            pst.setInt(5,student.getGrade().getGrade_no());
            pst.setString(6,student.getStu_phone());
            pst.setString(7,student.getStu_add());
            //时间处理:
            //在声明属性的时候,我们声明的Date是java.util.Date
            //pst.setDate()这里使用的是java.sql.Date。我们就需要用new Date()转换一下
            //new Date(student.getStu_born().getTime())转换后得到的是时间戳
            pst.setDate(8, new Date(student.getStu_born().getTime()));
            pst.setString(9,student.getStu_email());
            i=pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }
​
    @Override
    public int delete(Student student) {
        int i=-1;
        open();
        String sql="delete from student where stu_no=?";
        try {
            pst=con.prepareStatement(sql);
            pst.setInt(1,student.getStu_no());
            i=pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }
​
    @Override
    public int update(Student student) {
        int i=-1;
        open();
        String sql="update student set stu_pwd=?,stu_born=? where stu_no=?";
        try {
            pst=con.prepareStatement(sql);
            pst.setString(1,student.getStu_pwd());
            pst.setDate(2,new Date(student.getStu_born().getTime()));
            pst.setInt(3,student.getStu_no());
            i=pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }
​
    public List<Student> selectAll() {
        List<Student> list=null;
        open();
        String sql="select * from student";
        try {
            pst=con.prepareStatement(sql);
            rs=pst.executeQuery();
            list=new ArrayList<Student>();
            while(rs.next()){
                Student stu=new Student();
                stu.setStu_no(rs.getInt("stu_no"));
                stu.setStu_pwd(rs.getString("stu_pwd"));
                stu.setStu_name(rs.getString("stu_name"));
                stu.setStu_sex(rs.getString("stu_sex"));
                Grade g=new Grade();
                g.setGrade_no(rs.getInt("grad_no"));
                stu.setGrade(g);
                stu.setStu_phone(rs.getString("stu_phone"));
                stu.setStu_add(rs.getString("stu_add"));
                stu.setStu_born(rs.getDate("stu_born"));
                stu.setStu_email(rs.getString("stu_email"));
                list.add(stu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return list;
    }
}

4、学生service层接口和实现层

package _15MVC.d01.cn.com.service;
import _15MVC.d01.cn.com.entity.Student;
import java.util.List;
/**
 * @author LX
 * @create 2024-02-20 15:23
 */
public interface StudentService {
    public boolean add(Student student);
    public boolean remove(Student student);
    public boolean modify(Student student);
    public List<Student> findAll();
}
package _15MVC.d01.cn.com.service.impl;
import _15MVC.d01.cn.com.dao.StudentDao;
import _15MVC.d01.cn.com.dao.impl.StudentDaoImpl;
import _15MVC.d01.cn.com.entity.Student;
import _15MVC.d01.cn.com.service.StudentService;
import java.util.List;
/**
 * @author LX
 * @create 2024-02-20 15:24
 */
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao=new StudentDaoImpl();
    public boolean add(Student student) {
        return studentDao.insert(student)>0;
    }
​
    public boolean remove(Student student) {
        return studentDao.delete(student)>0;
    }
​
    public boolean modify(Student student) {
        return studentDao.update(student)>0;
    }
​
    public List<Student> findAll() {
        return studentDao.selectAll();
    }
}

5、测试类【给时间字段赋值时,可以使用SimpleDateFormat对象来转[case 12],也可以使用时间工具类来转[case 32]】

package _15MVC.d01.cn.com.test;
​
import _15MVC.d01.cn.com.entity.Grade;
import _15MVC.d01.cn.com.entity.Student;
import _15MVC.d01.cn.com.service.GradeService;
import _15MVC.d01.cn.com.service.StudentService;
import _15MVC.d01.cn.com.service.impl.GradeServiceImpl;
import _15MVC.d01.cn.com.service.impl.StudentServiceImpl;
import _15MVC.d01.cn.com.util.DateOrString;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
 * @author LX
 * @create 2024-02-20 10:10
 */
public class TestGrade {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int var=0;
        boolean flag=false;
        Grade grade=new Grade();
        Student student=new Student();
        GradeService gradeService=new GradeServiceImpl();
        StudentService studentService=new StudentServiceImpl();
        do {
            System.out.println("----------*欢迎使用学生管理系统*----------");
            System.out.println("----------*增加年级信息请按11*增加学生信息请按12*----------");
            System.out.println("----------*删除年级信息请按21*删除学生信息请按22*----------");
            System.out.println("----------*修改年级信息请按31*修改学生信息请按32*----------");
            System.out.println("----------*查询所有年级信息请按41*查询所有学生信息请按42*----------");
            System.out.println("----------*通过年级编号查询年级请按51*通过学生编号查询学生请按52*----------");
            System.out.println("----------*结束使用请按0*----------");
            System.out.println("----------*请输入你的选择:*----------");
            var=input.nextInt();
            switch (var){
                case 11:
                    System.out.println("您选择的是增加年级信息:");
                    System.out.println("请输入年级编号:");
                    int gradeNo=input.nextInt();
                    System.out.println("请输入年级名称:");
                    String gradeName=input.next();
                    System.out.println("请输入年级人数:");
                    int gradeNum=input.nextInt();
                    grade.setGrade_no(gradeNo);
                    grade.setGrade_name(gradeName);
                    grade.setGrade_num(gradeNum);
                    flag=gradeService.add(grade);
                    if (flag){
                        System.out.println("增加年级信息成功!");
                    }else{
                        System.out.println("增加年级信息失败!");
                    }
                    break;
                case 12:
                    System.out.println("您选择的是增加学生信息:");
                    System.out.println("请输入学生编号:");
                    int stu_no=input.nextInt();
                    System.out.println("请输入学生密码");
                    String stu_pwd=input.next();
                    System.out.println("请输入学生姓名");
                    String stu_name=input.next();
                    System.out.println("请输入学生性别");
                    String stu_sex=input.next();
                    System.out.println("请输入年级编号");
                    gradeNo=input.nextInt();
                    grade.setGrade_no(gradeNo);
                    System.out.println("请输入学生电话");
                    String stu_phone=input.next();
                    System.out.println("请输入学生地址");
                    String stu_add=input.next();
                    System.out.println("请输入学生出生日期");
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                    Date stu_born=null;//要在外部声明,不然在try{}中声明就会变成局部变量
                    try {
                        stu_born= sdf.parse(input.next());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    System.out.println("请输入学生邮箱");
                    String stu_email=input.next();
                    student.setStu_no(stu_no);
                    student.setStu_pwd(stu_pwd);
                    student.setStu_name(stu_name);
                    student.setStu_sex(stu_sex);
                    student.setGrade(grade);
                    student.setStu_phone(stu_phone);
                    student.setStu_add(stu_add);
                    student.setStu_born(stu_born);
                    student.setStu_email(stu_email);
                    flag=studentService.add(student);
                    if (flag){
                        System.out.println("增加学生信息成功!");
                    }else{
                        System.out.println("增加学生信息失败!");
                    }
                    break;
                case 21:
                    System.out.println("您选择的是删除年级信息:");
                    System.out.println("请输入要删除的年级编号:");
                    gradeNo=input.nextInt();
                    grade.setGrade_no(gradeNo);
                    flag=gradeService.remove(grade);
                    if (flag){
                        System.out.println("删除年级信息成功!");
                    }else{
                        System.out.println("删除年级信息失败!");
                    }
                    break;
                case 22:
                    System.out.println("您选择的是删除学生信息:");
                    System.out.println("请输入要删除的学生编号:");
                    stu_no=input.nextInt();
                    student.setStu_no(stu_no);
                    flag=studentService.remove(student);
                    if (flag){
                        System.out.println("删除学生信息成功!");
                    }else{
                        System.out.println("删除学生信息失败!");
                    }
                    break;
                case 31:
                    System.out.println("您选择的是修改年级信息:");
                    System.out.println("请输入要修改的年级编号:");
                    gradeNo=input.nextInt();
                    System.out.println("请输入要修改的年级人数:");
                    gradeNum=input.nextInt();
                    grade.setGrade_no(gradeNo);
                    grade.setGrade_num(gradeNum);
                    flag=gradeService.modify(grade);
                    if (flag){
                        System.out.println("修改年级信息成功!");
                    }else{
                        System.out.println("修改年级信息失败!");
                    }
                    break;
                case 32:
                    System.out.println("您选择的是修改学生信息:");
                    System.out.println("请输入要修改的学生编号:");
                    stu_no=input.nextInt();
                    System.out.println("请输入修改的新密码:");
                    stu_pwd=input.next();
                    System.out.println("请输入修改后的出生日期:");
                    /*stu_born=null;
                    sdf=new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        stu_born=sdf.parse(input.next());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }*/
                    student.setStu_no(stu_no);
                    student.setStu_pwd(stu_pwd);
//                    student.setStu_born(stu_born);
                    //使用工具类与不使用的区别
                    student.setStu_born(DateOrString.StrToDate(input.next()));
                    flag=studentService.modify(student);
                    if (flag){
                        System.out.println("修改学生信息成功!");
                    }else{
                        System.out.println("修改学生信息失败!");
                    }
                    break;
                case 41:
                    System.out.println("您选择的是查询年级信息:");
                    List<Grade> list=gradeService.findAll();
                    for (Grade g:list){
                        System.out.println(g.getGrade_no()+"\t"+g.getGrade_name()+"\t"+g.getGrade_num());
                    }
                    break;
                case 42:
                    System.out.println("您选择的是查询学生信息:");
                    List<Student> list1=studentService.findAll();
                    for (Student stu:list1){     System.out.println(stu.getStu_no()+"\t"+stu.getStu_pwd()+"\t"+stu.getStu_name()+"\t"+stu.getStu_sex()+"\t"+stu.getStu_phone()+"\t"+stu.getGrade().getGrade_no()+"\t"+stu.getStu_add()+"\t"+stu.getStu_born()+"\t"+stu.getStu_email());
                    }
                    break;
            }
        }while (var!=0);
    }
}

总结:日期时间字段在处理时,可以提前写一个工具类

在给时间字段赋值时,就可以用工具类把字符串时间转换成Date类型的时间

处理日期与闰年验证的实践指南 本文通过分析《处理日期与闰年验证的实践指南》一章内容,探讨了如何用Java编写代码来处理日期和判断闰年。章节内容涵盖了日期的格式化、验证、以及基于月份和闰年规则的天数计算,展示了面向对象编程中类的设计和方法实现。 阅读详情

相关推荐

通过两个mark点确定某一个点的位置

read_image (Image0, '0.bmp') binary_threshold (Image0, Region, 'max_separability', 'dark', UsedThreshold) connection (Region, ConnectedRegions) fill_up (ConnectedRegions, RegionFillUp) select_shape_std (RegionFillUp, SelectedRegions, 'max_area', 70) red...

优快云博客 3887

MySQL数据库与JDBC编程(一)

JDBC基础 JDBC的全称是Java Database Connectivity,即数据库连接,它是一种可以执行SQL语句的JavaAPI。程序可通过JDBC API连接到关系数据库,并使用结构化查询语言(SQL,数据库标准的查询语言)来完成对数据库的查询、更新。 JDBC简介 通过使用JDBC,就可以使用同一种API访问不同的数据库系统。 JDBC驱动示意图 正是通过JDBC驱动的转换,才使得使用相同JDBC API编写的程序,在不同的数据库系统上运行良好。Sun提供的JDBC可以完成以下三个基本工作

xy87940020的博客 811

保姆级教程:用Multisim仿真快速上手二极管、MOSFET和IGBT的开关特性

本文提供了一份详细的Multisim仿真教程,帮助读者快速掌握二极管、MOSFET和IGBT的开关特性。通过搭建实验电路、观察波形特征和参数影响分析,深入解析了反向恢复过程、米勒平台现象和拖尾电流效应。适合电力电子初学者和工程师通过仿真建立直观理解,提升电路设计能力。

weixin_29290863的博客 498

Java面向对象—JDBC外键和时间的处理

本章介绍在JDBC中针对外键和日期的处理

smx161217的博客 43

Arduino创意作品实战案例:手把手教你做温控风扇

通过这个实用的arduino创意作品,轻松实现温度感应与风扇自动控制,结合传感器与编程逻辑,打造智能散热系统,是入门者理想的arduino创意作品实践项目。

weixin_42311427的博客 608

Windows/Linux双平台指南:3种方法快速释放被占用的GPU内存(含任务管理器替代方案)

本文详细介绍了在Windows和Linux双平台下快速释放被占用GPU内存的3种方法,包括任务管理器替代方案。从基础操作到高阶技巧,帮助用户彻底解决GPU内存驻留问题,提升计算资源利用率。特别适用于深度学习开发者和服务器管理员处理显存泄漏等常见问题。

j0k1l2m3n的博客 259

【Java 基础篇】Java Calendar 类:日期和时间处理指南

Calendar类是 Java 中用于处理日期和时间的抽象类。它提供了一种独立于特定日历系统的方式来处理日期和时间。通过Calendar类,你可以执行许多常见的日期和时间操作,如计算日期差异、添加或减去时间单位等。有时你可能需要获取一周的第一天(通常是星期日)和最后一天(通常是星期六)。// 设置为星期日// 获取一周的第一天// 将日期移动到星期六// 获取一周的最后一天// 设置为月份的第一天// 获取一月的第一天// 将日期移动到下个月的第一天。

繁依Fanyi的博客 1万+

速腾聚创激光雷达复现FAST-LIO

记录复现FAST-LIO算法的过程和,代码梳理和理解。FAST-LIO的简化版、注释版。速腾激光雷达

abanchao的博客 2928

Java中面向对象部分常用API(BigDecimal和部分日期相关)

总的来说,GMT是一个更传统的时间标准,而UTC是一个更现代、更精确的时间标准。在大多数实际应用中,尤其是需要高精度时间的情况下,UTC是首选的时间标准。参考案例二的代码,如果需要根据字符串创建出来指定的日期格式,在进行比较,会非常复杂,在这个基础上,JDK8新推出了时间类型,可以简化操作,同时解决多线程数据安全的问题。public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式)创建简单日期格式化对象,并封装指定的模式。获取日历中的年、月、日、时、分、秒等信息。

FFFFFAAAAC的博客 1649

2023华为od机试C卷【最富裕的小家庭】Python 实现

在一颗树中,每个节点代表一个家庭成员,节点的数字表示其个人的财富值,一个节点及其直接相连的子节点被定义为个小家庭。现给你一颗树,请计算出最富裕的小家庭的财富和。

MISAYAONE的博客 2042

educoder中Hive综合应用案例 — 用户搜索日志分析

Hive综合应用案例 — 用户搜索日志分析

weixin_46026136的博客 4760

JAVA面向对象期末复习总结

Java编程基础概述 作者:何锦源 1 Java编程语言的特点 Java编程语言以其独特的跨平台性、面向对象的特性以及丰富的类库支持,成为当今最受欢迎的编程语言之一。Java的“一次编写,到处运行”的理念,得益于其虚拟机(JVM)架构,确保了Java程序能够在不同的操作系统

2401_87515917的博客 1121

【GameFramework框架内置模块】17、声音(Sound)

声音(Sound)模块是管理音频的模块,比如播放、停止、修改音量等,支持淡入淡出、AudioMixer等高级设置。声音(Sound)的配置面板如下所示:所有的声音统一按照组来分,可以根据需要添加或者溢出组,声音组可以配置Agent的数量和音量等。

优快云博客专家,Unity3D高级软件工程师,《Unity 3D从入门到实战》作者。 1911

java字符串 日期_Java中日期与字符串的处理

Java中日期与字符串的处理Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,下面为大家介绍一下Java中日期与字符串的处理,一起来看看!日期相关的APIjava.util.Date 类,是一个日期类,含年、月、日、时、分、秒信息。构造方法:public Date(); 空参构造,得到当前系统时间的日期。public Date(long date); //根据指定的毫秒数来构造一个日期...

weixin_30717635的博客 641

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

Java日期处理(Date、Calendar、DateFormat)

最多也就是用Date类里的getTime()方法获取Long的毫秒日期格式。

2301_78976656的博客 666

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

JAVA高级进阶04面向对象:常见API&日期时间&Arrays

Math代表数学,是一个工具类,里面提供的都是对数据进行操作的一些静态方法Math类提供的常见方法。

2402_84667776的博客 1179

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

晦涩的Java日期处理

本文实在是旨在对Java标准库中日期处理之晦涩不堪现状的怒斥。在你反驳之前,请确信你了解过Joda(译注1)。我们问题的引发是因为所交付的客户不接受(因为某种原因)在其项目中使用开源的资源。我与我的同事们最近在开发管理类库的一个简单的Java应用程序。昨天,我开始编写一个用来计算过期图书的罚金的模块,完成该模块就要算出截止日期与当前日期的间隔天数。其实也就是两个日期做下减法,可是,Java标准

Robert C. Martin博客中文版 1万+

Java 面向对象详解和JVM底层内存分析

都是解决问题的思维方式,都是代码组织的方式。

m0_68880377的博客 2158

Java8新特性日期时间的处理别再只会用Date了

Java8新特性日期时间的处理Java8时间类旧世纪新世纪LocalDate和LocalTimeLocalDateTimeInstantDurationPeriod简单直接的操纵日期格式化或者解析日期 Java8时间类 旧世纪 在java 1.0中,对日期和时间的支持 只能依赖java.util.Date类。如同类名所示,这个类无法表示日期,只能以毫秒的精度表示时间。由于一些历史的原因,这个类的使用性大大降低。有许多表达不清的功能,比如Date类的toString方法返回的字符串 Thu Dec 24 1

Boting的博客 631

【Java面向对象程序设计】学习总结

个人理解与心得通过对第6章的学习,我们掌握了Java中常用类的使用方法,包括包装类、字符串处理类、日期时间类、数学类和随机数类等,这些类在日常编程中非常实用,能够帮助我们高效地处理各种数据和操作。

2501_90141283的博客 927

Java面向对象程序设计知识点总结

Java面向对象程序设计是软件开发的关键基石。从基础概念到复杂特性,它涵盖丰富内容。无论是环境搭建、语法基础,还是面向对象的进阶知识、异常处理与常用类,都紧密相连。本文将对这些知识点进行系统总结。

m0_74843596的博客 1266

Educoder Java面向对象 - 常用类

Educoder Java面向对象 - 常用类 求大佬带带我 orz 纯菜鸡,如果有更好的Idea可以分享给我,我也想学 第1关:Object类 import java.util.Scanner; public class ObjectTest { public static void main(String[] args) throws CloneNotSupportedException { Scanner sc = new Scanner(System.in); int num1 = s

qq_44921671的博客 7838

Java程序设计实验3 | 面向对象(上)

Java课程实验报告分享之第3篇,含总结心得,源码详解,思维拓展

wyd_333的博客 4442

Java8日期时间(LocalDate、LocalTime、LocalDateTime、ZonedDateTime)与SpringBoot、MyBatis、MySQL、Oracle的联系

Java8日期时间(LocalDate、LocalTime、LocalDateTime、ZonedDateTime)与SpringBoot、MyBatis、MySQL、Oracle的联系。

宋冠巡的博客 4463

Java 面向对象 | 详细知识图谱式讲解

了解 Java 基本语法、面向对象编程(OOP)概念、流程控制语句、数据类型、方法等基础知识。可以通过 Java 编程入门教程、Java 编程思想等书籍进行学习

wei_shuo的博客 1976

7-2 日期问题面向对象设计(聚合一) (35 分)

** 7-2 日期问题面向对象设计(聚合一) (35 分) ** 参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下: 应用程序共测试三个功能: 求下n天 求前n天 求两个日期相差的天数 注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改) 解题报告 写完程序后,想要测试三个功能点的正

lylkp的博客 1266
上一篇: Java面向对象—MVC分层
下一篇: Java面向对象—IO流
得闲饮茶呃
博客等级 码龄4年 303粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值