外键和日期时间处理
一、外键
上一节我们写了部门表,本节我们以员工表为例:
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类型的时间