github地址:https://github.com/qscqesze/StudentManager

简单描述一下:

UI层面用于接受用户的处理信息,然后移交给StudentDao去处理数据。

其中StudentDao通过修改Xml充当的数据库,来反馈数据。

StudentDomain是用来定义数据类型,Studentutils是处理数据时候封装的工具类型。

写了个额外的异常,Exception。

我也是参考着教程写的:《30天轻松掌握JavaWeb视频》 方立勋,网易云课堂里面有这个课程。

然后这个代码就结束了。

具体代码如下:

exam.xml //数据库文件

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
  2. <student examid="222" idcard="111">
  3. <name>张三</name>
  4. <location>沈阳</location>
  5. <grade>89</grade>
  6. </student>
  7. <student examid="444" idcard="333">
  8. <name>李四</name>
  9. <location>大连</location>
  10. <grade>97</grade>
  11. </student>
  12. </exam>

StudentDaoTest.java //测试文件

  1. package junit.test;
  2. import org.junit.Test;
  3. import en.itcast.dao.StudentDao;
  4. import en.itcast.domain.Student;
  5. import en.itcast.exception.StudentNotExistException;
  6. public class StudentDaoTest {
  7. @Test
  8. public void testAdd(){
  9. StudentDao dao = new StudentDao();
  10. Student s = new Student();
  11. s.setExamid("121");
  12. s.setGrade(89);
  13. s.setIdcard("121");
  14. s.setLocation("北京");
  15. s.setName("aa");
  16. dao.add(s);
  17. }
  18. @Test
  19. public void testFind(){
  20. StudentDao dao = new StudentDao();
  21. dao.find("222");
  22. }
  23. @Test
  24. public void testdelete() throws StudentNotExistException{
  25. StudentDao dao = new StudentDao();
  26. dao.delete("aa");
  27. }
  28. }

XmlUtils.java //工具类文件

  1. package en.itcast.utils;
  2. import java.io.FileOutputStream;
  3. import javax.xml.parsers.DocumentBuilder;
  4. import javax.xml.parsers.DocumentBuilderFactory;
  5. import javax.xml.transform.Transformer;
  6. import javax.xml.transform.TransformerFactory;
  7. import javax.xml.transform.dom.DOMSource;
  8. import javax.xml.transform.stream.StreamResult;
  9. import org.w3c.dom.Document;
  10. public class XmlUtils {
  11. // 工具类约定俗成为静态类
  12. private static String filename = "src/exam.xml";
  13. public static Document getDocument() throws Exception {
  14. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  15. DocumentBuilder builder = factory.newDocumentBuilder();
  16. return builder.parse(filename);
  17. }
  18. // 编译异常就是垃圾
  19. public static void write2Xml(Document document) throws Exception {
  20. TransformerFactory factory = TransformerFactory.newInstance();
  21. Transformer tf = factory.newTransformer();
  22. tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream(filename)));
  23. }
  24. }

Main.java //主程序,ui界面的

  1. package en.itcast.UI;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import en.itcast.dao.StudentDao;
  6. import en.itcast.domain.Student;
  7. import en.itcast.exception.StudentNotExistException;
  8. public class Main {
  9. public static void main(String[] args) {
  10. System.out.println("添加用户(a) 删除用户(b) 查找用户(c)");
  11. System.out.print("请输入操作类型:");
  12. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  13. try {
  14. String type = bf.readLine();
  15. if ("a".equals(type)) {
  16. System.out.print("请输入学生姓名:");
  17. String name = bf.readLine();
  18. System.out.print("请输入学生准考证号:");
  19. String examid = bf.readLine();
  20. System.out.print("请输入学生所在地:");
  21. String location = bf.readLine();
  22. System.out.print("请输入学生身份证:");
  23. String idcard = bf.readLine();
  24. System.out.print("请输入学生成绩:");
  25. String grade = bf.readLine();
  26. Student s = new Student();
  27. s.setExamid(examid);
  28. s.setGrade(Double.parseDouble(grade));
  29. s.setIdcard(idcard);
  30. s.setLocation(location);
  31. s.setName(name);
  32. StudentDao dao = new StudentDao();
  33. dao.add(s);
  34. System.out.println("添加成功");
  35. } else if ("b".equals(type)) {
  36. System.out.print("请输入要删除的学生姓名:");
  37. String name = bf.readLine();
  38. try {
  39. StudentDao dao = new StudentDao();
  40. dao.delete(name);
  41. System.out.println("删除成功.");
  42. } catch (StudentNotExistException e) {
  43. System.out.println("你删除的用户不存在.");
  44. }
  45. } else if ("c".equals(type)) {
  46. System.out.print("请输入你要查询的准考证id:");
  47. String examid = bf.readLine();
  48. StudentDao dao = new StudentDao();
  49. Student find_student = dao.find(examid);
  50. if(find_student.equals(null)){
  51. System.out.println("对不起,找不到该学生.");
  52. }else{
  53. System.out.println("该学生的姓名是:"+find_student.getName());
  54. }
  55. } else {
  56. System.out.println("不支持你的操作");
  57. }
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. ;
  61. System.out.println("sorry");
  62. }
  63. }
  64. }

StudentNotExistException.java //自定义异常,处理删除的时候不存在用

  1. package en.itcast.exception;
  2. public class StudentNotExistException extends Exception {
  3. public StudentNotExistException() {
  4. // TODO Auto-generated constructor stub
  5. }
  6. public StudentNotExistException(String message) {
  7. super(message);
  8. // TODO Auto-generated constructor stub
  9. }
  10. public StudentNotExistException(Throwable cause) {
  11. super(cause);
  12. // TODO Auto-generated constructor stub
  13. }
  14. public StudentNotExistException(String message, Throwable cause) {
  15. super(message, cause);
  16. // TODO Auto-generated constructor stub
  17. }
  18. public StudentNotExistException(String message, Throwable cause, boolean enableSuppression,
  19. boolean writableStackTrace) {
  20. super(message, cause, enableSuppression, writableStackTrace);
  21. // TODO Auto-generated constructor stub
  22. }
  23. }

Student.java //封装Student的数据类型

  1. package en.itcast.domain;
  2. public class Student {
  3. private String idcard;
  4. private String examid;
  5. private String name;
  6. private String location;
  7. private double grade;
  8. public String getIdcard() {
  9. return idcard;
  10. }
  11. public void setIdcard(String idcard) {
  12. this.idcard = idcard;
  13. }
  14. public String getExamid() {
  15. return examid;
  16. }
  17. public void setExamid(String examid) {
  18. this.examid = examid;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public String getLocation() {
  27. return location;
  28. }
  29. public void setLocation(String location) {
  30. this.location = location;
  31. }
  32. public double getGrade() {
  33. return grade;
  34. }
  35. public void setGrade(double grade) {
  36. this.grade = grade;
  37. }
  38. }

StudentDao.java //程序处理

  1. package en.itcast.dao;
  2. import org.w3c.dom.Document;
  3. import org.w3c.dom.Element;
  4. import org.w3c.dom.NodeList;
  5. import en.itcast.domain.Student;
  6. import en.itcast.exception.StudentNotExistException;
  7. import en.itcast.utils.XmlUtils;
  8. public class StudentDao {
  9. public void add(Student s) {
  10. try {
  11. Document document = XmlUtils.getDocument();
  12. // 创建出封装学生信息的标签
  13. Element student_tag = document.createElement("student");
  14. student_tag.setAttribute("idcard", s.getIdcard());
  15. student_tag.setAttribute("examid", s.getExamid());
  16. // 创建用于封装学生其他信息的标签
  17. Element name = document.createElement("name");
  18. Element location = document.createElement("location");
  19. Element grade = document.createElement("grade");
  20. name.setTextContent(s.getName());
  21. location.setTextContent(s.getLocation());
  22. grade.setTextContent(s.getGrade() + "");
  23. student_tag.appendChild(name);
  24. student_tag.appendChild(location);
  25. student_tag.appendChild(grade);
  26. // 把封装的信息挂在文档上
  27. document.getElementsByTagName("exam").item(0).appendChild(student_tag);
  28. XmlUtils.write2Xml(document);
  29. } catch (Exception e) {
  30. throw new RuntimeException(e);
  31. }
  32. }
  33. public Student find(String examid) {
  34. try {
  35. Document document = XmlUtils.getDocument();
  36. NodeList list = document.getElementsByTagName("student");
  37. for (int i = 0; i < list.getLength(); i++) {
  38. Element student_tag = (Element) list.item(i);
  39. if (student_tag.getAttribute("examid").equals(examid)) {
  40. // 找到匹配的学生了,new一个student对象
  41. Student s = new Student();
  42. s.setExamid(examid);
  43. s.setIdcard(student_tag.getAttribute("idcard"));
  44. s.setName(student_tag.getElementsByTagName("name").item(0).getTextContent());
  45. s.setLocation(student_tag.getElementsByTagName("location").item(0).getTextContent());
  46. s.setGrade(Double.parseDouble(student_tag.getElementsByTagName("grade").item(0).getTextContent()));
  47. return s;
  48. }
  49. }
  50. return null;
  51. } catch (Exception e) {
  52. throw new RuntimeException(e);
  53. }
  54. }
  55. public void delete(String name) throws StudentNotExistException {
  56. try {
  57. Document document = XmlUtils.getDocument();
  58. NodeList list = document.getElementsByTagName("name");
  59. for (int i = 0; i < list.getLength(); i++) {
  60. if (list.item(i).getTextContent().equals(name)) {
  61. list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode());
  62. XmlUtils.write2Xml(document);
  63. return;
  64. }
  65. }
  66. throw new StudentNotExistException(name + "不存在");
  67. } catch (StudentNotExistException e) {
  68. throw e;
  69. } catch (Exception e) {
  70. throw new RuntimeException(e);
  71. }
  72. }
  73. }

JAVA初学练手项目,学生管理系统的更多相关文章

  1. 10个相见恨晚的 Java 在线练手项目

    10个有意思的Java练手项目: 1.Java 开发简单的计算器 难度为一般,适合具有 Java 基础和 Swing 组件编程知识的用户学习 2.制作一个自己的 Java 编辑器 难度中等,适合 Ja ...

  2. 20个Java练手项目,献给嗜学如狂的人

    给大家推荐一条由浅入深的JAVA学习路径,首先完成 Java基础.JDK.JDBC.正则表达式等基础实验,然后进阶到 J2SE 和 SSH 框架学习.最后再通过有趣的练手项目进行巩固. JAVA基础 ...

  3. 去哪找Java练手项目?

    经常有读者在微信上问我: 在学编程的过程中,看了不少书.视频课程,但是看完.听完之后感觉还是不会编程,想找一些项目来练手,但是不知道去哪儿找? 类似的问题,有不少读者问,估计是大部分人的困惑. 练手项 ...

  4. java客房管理小项目,适合java小白练手的项目!

    java客房管理小项目 这个客房管理小项目,适合java初学者练手.功能虽然不多,但是内容很齐全! 喜欢这样文章的可以关注我,我会持续更新,你们的关注是我更新的动力!需要更多java学习资料的也可以私 ...

  5. web前端学习部落22群分享给需要前端练手项目

    前端学习还是很有趣的,可以较快的上手然后自己开发一些好玩的项目来练手,网上也可以一抓一大把关于前端开发的小项目,可是还是有新手在学习的时候不知道可以做什么,以及怎么做,因此,就整理了一些前端项目教程, ...

  6. webpack练手项目之easySlide(三):commonChunks(转)

    Hello,大家好. 在之前两篇文章中: webpack练手项目之easySlide(一):初探webpack webpack练手项目之easySlide(二):代码分割 与大家分享了webpack的 ...

  7. Python之路【第二十四篇】:Python学习路径及练手项目合集

      Python学习路径及练手项目合集 Wayne Shi· 2 个月前 参照:https://zhuanlan.zhihu.com/p/23561159 更多文章欢迎关注专栏:学习编程. 本系列Py ...

  8. 适合Python的5大练手项目, 你练了么?

    在练手项目的选择上,还存在疑问?不知道要从哪种项目先下手? 首先有两点建议: 最好不要写太应用的程序练手,要思考什么更像是知识,老只会写写爬虫是无用的,但是完全不写也不行. 对于练手的程序,要注意简化 ...

  9. 适合Python 新手的5大练手项目,你练了么?

    接下来就给大家介绍几种适合新手的练手项目. 0.算法系列-排序与查找 Python写swap很方便,就一句话(a, b = b, a),于是写基于比较的排序能短小精悍.刚上手一门新语言练算法最合适不过 ...

随机推荐

  1. Dubbo学习笔记7:Dubbo的集群容错与负载均衡策略

    Dubbo的集群容错策略 正常情况下,当我们进行系统设计时候,不仅要考虑正常逻辑下代码该如何走,还要考虑异常情况下代码逻辑应该怎么走.当服务消费方调用服务提供方的服务出现错误时候,Dubbo提供了多种 ...

  2. BAT及各大互联网公司2014前端笔试面试题--JavaScript篇(昨天某个群友表示写的简单了点,然后我无情的把他的抄了一遍)

    (某个群友)http://www.cnblogs.com/coco1s/ 很多面试题是我自己面试BAT亲身经历碰到的.整理分享出来希望更多的前端er共同进步吧,不仅适用于求职者,对于巩固复习js更是大 ...

  3. HTML的文档类型:<!DOCTYPE >

    <!DOCTYPE> 声明:它不是 HTML 标签而且对大小写不敏感,而是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令.而且 声明必须是 HTML 文档的第一行,位于 ...

  4. 20155315 2016-2017-2 《Java程序设计》第七周学习总结

    教材学习内容总结 第12章 Lambda语法 Lambda定义 一个不用被绑定到一个标识符上,并且可能被调用的函数. 在只有Lambda表达式的情况下,参数的类型必须写出来,如果有目标类型的话,在编译 ...

  5. 20155339 2016-2017-2 《Java程序设计》第5周学习总结

    20155339 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 使用try.catch 使用try.catch语法,JVM会先尝试执行try区块中的代码,如 ...

  6. linux sftp安装【转】

    工具:虚拟机:VMware Workstation Pro.操作系统:CentOS-6.4-x86_64-minimal.终端模拟器:Xshell 5 .ftp:filezilla 一.让虚拟机联网 ...

  7. Python3安装配置【转】

    不建议卸载python2 可能会导致系统内其他软件无法使用,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装python3和python2共存 (前 ...

  8. linux,mac安装sentry

    linux,mac安装sentry 最近需要一个日志监视系统所以选择了sentry.以下是用mac安装,看需求量linux安装类似后面的文章会补充. 安装docker https://download ...

  9. web.js

    var page = require('webpage').create(), system = require('system'), address,output,csvPath,nodePathF ...

  10. 更改jupyter notebook的主题颜色(theme) 包括pycharm

    https://blog.csdn.net/Techmonster/article/details/73382535