标签:
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; } }
1、switch可以使用穿透,就是多个选项同一个效果。
switch(c){ case 1: case 2: System.out.println("nihao"); break; default: System.out.println("byby"); }
2、接收到的数据类型是否对应类型可以不必使用Scanner的hasNextInt(),而使用try、catch异常处理判断类型接受的是否正确,因为错误的类型会抛出异常。
3、在实现多个同级模块的功能后返回初始的选择可以使用while循环镶嵌switch达到,返回的话只需要在需要的位置跳出switch循环就好了。
4、学会使用三目运算符简化代码。
标签:
原文地址:http://www.cnblogs.com/aigeileshei/p/5523842.html