01_StudentManager
package com.dao; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; import com.entity.Student; /** * 只能用List来保存学生 * */ public class Sms { List<Student> stus ; //构造函数用于初始化对象中非静态属性 public Sms(){ stus = new ArrayList<Student>(); } /** 保存 @param 学生对象 */ public void save(Student stu){ stus.add(stu); } /** 查询所有的学生 */ public List<Student> queryAll(){ return stus; } /** 通过学生的id查找学生的信息 */ public Student queryById(long id){ for(Student stu : stus){ if(stu.getId() == id){ return stu; } } return null; } /** 修改学生信息 */ public void update(Student newStu){ for(Student stu : stus){ if(stu.getId() == newStu.getId()){ stu.setName(newStu.getName()); stu.setAge(newStu.getAge()); } } } /** 删除学生信息 迭代器 */ public void deleteById(long id){ Iterator<Student> iter = stus.iterator(); while(iter.hasNext()){ Student stu = iter.next(); if(stu.getId() == id){ iter.remove(); } } } public void menu(){ System.out.println("********学生管理系统********"); System.out.println("*1. 查询所有学生信息"); System.out.println("*2. 录入学生信息"); System.out.println("*3. 删除学生信息"); System.out.println("*4. 查询单个学生信息"); System.out.println("*5. 修改学生信息"); System.out.println("*exit. 退出"); System.out.println("*help. 帮助"); System.out.println("****************************"); } /** 主方法 */ public static void main(String[] args){ Sms sms = new Sms(); sms.menu(); Scanner sc = new Scanner(System.in); while(true){ try { System.out.print("请输入功能编号:"); String option = sc.nextLine(); //System.out.println("接收了:"+option); switch(option){ case "1": System.out.println("以下是学生的信息:"); List<Student> arr = sms.queryAll(); for(int i=0;i<arr.size();i++){ System.out.println(arr.get(i)); } System.out.println("总计 "+arr.size()+" 个"); break; case "2": while(true){ System.out.println("请输入学生信息【id#name#age】或者输入【break】返回上一级目录"); String stuStr = sc.nextLine(); if(stuStr.equals("break")){ break; } //1001#terry#12 String[] stuArr = stuStr.split("#"); long id = Long.parseLong(stuArr[0]); String name = stuArr[1]; int age = Integer.parseInt(stuArr[2]); //Student stu = new Student(id,name,age); //sms.save(stu); System.out.println("保存成功!"); } break; case "3": while(true){ System.out.println("请输入要删除学生的学号或者输入【break】返回上一级目录"); String idStr = sc.nextLine(); if(idStr.equals("break")){ break;//跳出当前循环,返回主菜单 } //1001#terry#12 long id = Long.parseLong(idStr); Student oldStu = sms.queryById(id); if(oldStu == null){ System.out.println("您要删除的学生不存在!"); continue; } sms.deleteById(id); System.out.println("删除成功!"); } break; case "4": while(true){ System.out.println("请输入学号或者输入【break】返回上一级目录"); String idStr = sc.nextLine(); if(idStr.equals("break")){ break; } //1001#terry#12 long id = Long.parseLong(idStr); Student stu = sms.queryById(id); System.out.println(stu==null?"sorry,not found!":stu); } break; case "5"://修改 while(true){ System.out.println("请输入要修改学生的学号或者输入【break】返回上一级目录"); String idStr = sc.nextLine(); if(idStr.equals("break")){ break;//跳出当前循环,返回主菜单 } //1001#terry#12 long id = Long.parseLong(idStr); Student oldStu = sms.queryById(id); if(oldStu == null){ System.out.println("您要修改的学生不存在!"); continue; } System.out.println("原有信息为:"+oldStu); System.out.println("请输入信息【name#age】"); //获取用户的新信息,并且将其封装为对象 String newStr = sc.nextLine(); String[] newArr = newStr.split("#"); String name = newArr[0]; int age = Integer.parseInt(newArr[1]); //Student newStu = new Student(id,name,age); //调用修改模块的方法,完成修改功能 //sms.update(newStu); System.out.println("修改成功"); } break; case "exit": System.out.println("bye bye,欢迎再次使用!"); System.exit(0); case "help": sms.menu(); break; default: System.out.println("不合法输入!"); } } catch (Exception e) { System.out.println("输入出错!"); //e.printStackTrace(); continue; } } } } package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.entity.Student; import com.util.JDBC; public class StudentDAO { static Connection conn=new JDBC().getConn(); static PreparedStatement pstml=null; static ResultSet rs=null; public void menu(){ System.out.println("********学生管理系统********"); System.out.println("*1. 查询所有学生信息"); System.out.println("*2. 录入学生信息"); System.out.println("*3. 删除学生信息"); System.out.println("*4. 查询单个学生信息"); System.out.println("*5. 修改学生信息"); System.out.println("*exit. 退出"); System.out.println("*help. 帮助"); System.out.println("****************************"); } public int addStudent(Student s) throws SQLException{ String sql="insert into student(name,sex,age,tel)" + "values(?,?,?,?)"; pstml= conn.prepareStatement(sql); //pstml.setInt(1, s.getId()); pstml.setString(1, s.getName()); pstml.setString(2, s.getSex()); pstml.setInt(3, s.getAge()); pstml.setLong(4, s.getTel()); return pstml.executeUpdate(); } public int deleteStudent(int id) throws SQLException{ String sql="delete * from student where id=?"; pstml= conn.prepareStatement(sql); pstml.setInt(1, id); return pstml.executeUpdate(); } public int change(Student s) throws SQLException{ String sql="update student set name=?,sex=?,age=?,tel=? where id=?"; pstml= conn.prepareStatement(sql); pstml.setString(1, s.getName()); pstml.setString(2, s.getSex()); pstml.setInt(3, s.getAge()); pstml.setLong(4, s.getTel()); pstml.setInt(5, s.getId()); return pstml.executeUpdate(); } public Student studentFindById(int id) throws SQLException{ String sql="select * from student where id=?"; pstml= conn.prepareStatement(sql); pstml.setInt(1, id); rs=pstml.executeQuery(); Student s = null; while(rs.next()){ s=new Student(); s.setId(rs.getInt("id")); s.setName(rs.getString("name")); s.setSex(rs.getString("sex")); s.setAge(rs.getInt("age")); s.setTel(rs.getLong("tel")); } return s; } public ArrayList<Student> studentFindAll() throws SQLException{ ArrayList<Student> studentList=new ArrayList<Student>(); String sql="select * from student"; pstml= conn.prepareStatement(sql); rs=pstml.executeQuery(); while(rs.next()){ Student s=new Student(); s.setId(rs.getInt("id")); s.setName(rs.getString("name")); s.setSex(rs.getString("sex")); s.setAge(rs.getInt("age")); s.setTel(rs.getLong("tel")); studentList.add(s); } return studentList; } public static void close() throws SQLException{ if(conn!=null){ conn.close(); }else if(pstml!=null){ pstml.close(); }else if(rs!=null){ rs.close(); } } } package com.entity; public class Student { private int id; private String name; private String sex; private int age; private long tel; public int getId() { return id; } public void setId(int id) { this.id = id; } public Student() { super(); // TODO Auto-generated constructor stub } public Student(int id, String name, String sex, int age, long tel) { super(); this.id = id; this.name = name; this.sex = sex; this.age = age; this.tel = tel; } public Student( String name, String sex, int age, long tel) { super(); this.name = name; this.sex = sex; this.age = age; this.tel = tel; } public String getName() { return name; } public void setName(String name) { this.name = name; } 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 long getTel() { return tel; } public void setTel(long tel) { this.tel = tel; } public String toString(){ return "Student [id="+getId()+", name="+getName()+", age="+getAge()+", sex="+getSex()+", tel="+getTel()+"]"; } } package com.test; import java.util.List; import java.util.Scanner; import com.dao.StudentDAO; import com.entity.Student; public class StudentManager { public static void main(String[] args){ StudentDAO studentDao = new StudentDAO(); studentDao.menu(); Scanner sc = new Scanner(System.in); while(true){ try { System.out.print("请输入功能编号:"); String option = sc.nextLine(); switch(option){ case "1": System.out.println("以下是学生的信息:"); List<Student> arr = studentDao.studentFindAll(); for(int i=0;i<arr.size();i++){ System.out.println(arr.get(i)); } System.out.println("总计 "+arr.size()+" 个"); break; case "2": while(true){ //假设学生年龄在0~80之间 System.out.println("请输入学生信息【name#sex#age#tel】或者输入【break】返回上一级目录"); String stuStr = sc.nextLine(); if(stuStr.equals("break")){ break; } String[] stuArr = stuStr.split("#"); String name = stuArr[0]; String sex = stuArr[1]; int age = Integer.parseInt(stuArr[2]); Long tel=Long.parseLong(stuArr[3]); Student s=new Student(name,sex,age,tel); if( name==null||name.equals("")|| sex==null||sex.equals("")|| (((sex.equals("男")||sex.equals("女"))==false)|| age<=0||age>=80|| ((tel>=10000000&&tel<100000000)||( tel>=10000000000l && tel<100000000000l))==false) ) { System.out.println("信息格式错误!注意:\n" + "1、学生姓名,性别,年龄,电话号码不能为空\n" + "2、性别只能为男和女\n" + "3、年龄在0~80之间\n" + "4、" + "5、tel为电话号码或者手机号码,必须是8位或者11位数字"); continue; }else{ int i=studentDao.addStudent(s); if(i>0){ System.out.println("添加成功!"); }else{ System.out.println("系统异常,信息添加失败!"); } } } break; case "3": while(true){ System.out.println("请输入要删除学生的学号或者输入【break】返回上一级目录"); String idStr = sc.nextLine(); if(idStr.equals("break")){ break;//跳出当前循环,返回主菜单 } int id = Integer.parseInt(idStr); Student oldStu = studentDao.studentFindById(id); if(oldStu == null){ System.out.println("您要删除的学生不存在!"); continue; } int i=studentDao.deleteStudent(id); if(i>0){ System.out.println("添加成功!"); }else{ System.out.println("系统异常,信息添加失败!"); } } break; case "4": while(true){ System.out.println("请输入学号或者输入【break】返回上一级目录"); String idStr = sc.nextLine(); if(idStr.equals("break")){ break; } //1001#terry#12 int id = Integer.parseInt(idStr); Student stu = studentDao.studentFindById(id); System.out.println(stu==null?"sorry,not found!":stu); } break; case "5"://修改 while(true){ System.out.println("请输入要修改学生的学号或者输入【break】返回上一级目录"); String idStr = sc.nextLine(); if(idStr.equals("break")){ break;//跳出当前循环,返回主菜单 } int id = Integer.parseInt(idStr); Student oldStu = studentDao.studentFindById(id); if(oldStu == null){ System.out.println("您要修改的学生不存在!"); continue; } System.out.println("原有信息为:"+oldStu); System.out.println("请输入学生信息【name#sex#age#tel】"); String stuStr = sc.nextLine(); if(stuStr.equals("break")){ break; } String[] stuArr = stuStr.split("#"); int stuId=id; String name = stuArr[0]; String sex = stuArr[1]; int age = Integer.parseInt(stuArr[2]); Long tel=Long.parseLong(stuArr[3]); Student s=new Student(stuId,name,sex,age,tel); if( name==null||name.equals("")|| sex==null||sex.equals("")|| (((sex.equals("男")||sex.equals("女"))==false)|| age<=0||age>=80|| ((tel>=10000000&&tel<100000000)||( tel>=10000000000l && tel<100000000000l))==false) ) { System.out.println("信息格式错误!注意:\n" + "1、学生姓名,性别,年龄,电话号码不能为空\n" + "2、性别只能为男和女\n" + "3、年龄在0~80之间\n" + "4、" + "5、tel为电话号码或者手机号码,必须是8位或者11位数字"); continue; }else{ int i=studentDao.change(s); if(i>0){ System.out.println("修改成功!"); }else{ System.out.println("系统异常,信息添加失败!"); } } } break; case "exit": System.out.println("bye bye,欢迎再次使用!"); System.exit(0); case "help": studentDao.menu(); break; default: System.out.println("不合法输入!"); } } catch (Exception e) { System.out.println("输入出错!"); e.printStackTrace(); continue; } } } } package com.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JDBC { private final String DRIVER="com.mysql.jdbc.Driver"; private final String URL="jdbc:mysql://localhost:3306/test"; String username = "root" ; String password = "root" ; Connection conn=null; public Connection getConn(){ try{ Class.forName(DRIVER); conn =DriverManager.getConnection(URL,username,password) ; }catch(SQLException se){ System.out.println("数据库连接失败!"); se.printStackTrace() ; } catch (ClassNotFoundException e) { e.printStackTrace(); } return conn; } }