利用XML编程实践简易学生成绩管理系统
来源:互联网 发布:mysql备份与恢复 编辑:程序博客网 时间:2024/05/21 21:02
1.需求:
利用XML存储数据来实现简易的学生成绩管理系统
2.实现思路:
这种模式就是典型的MVC模式,由下层的model(domain)层提供数据对象一步一步地到dao层,再到UI层。
3.XML文件(数据层)
// exam.xml<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam> <student examid="123" idcard="111"> <name>Cecilia</name> <location>北京</location> <grade>90</grade> </student> <student examid="456" idcard="112"> <name>Mary</name> <location>深圳</location> <grade>88</grade> </student> <student examid="789" idcard="113"> <name>Fient</name> <location>上海</location> <grade>95</grade> </student> <student examid="222" idcard=""> <name>cc</name> <location>广州</location> <grade>99.0</grade> </student> <student examid="123" idcard="114"> <name>Nacy</name> <location>广州</location> <grade>80.0</grade> </student></exam>
4.(model)domain层:
/** * 学生实体类 * @author 芷若初荨 * */public class Student { private String idcard; private String examid; private String name; private String location; private double garde; public String getIdcard() { return idcard; } public void setIdcard(String idcard) { this.idcard = idcard; } public String getExamid() { return examid; } public void setExamid(String examid) { this.examid = examid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public double getGarde() { return garde; } public void setGarde(double garde) { this.garde = garde; }}
5.辅助类
/** * 工具类(当中的方法一般约束为静态的方法) * @author 芷若初荨 * */public class XMLUtils { private static String fileName="msg/exam.xml";// 得到document文档 public static Document getDocument() throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); return builder.parse(fileName); }// 将数据写回XML文件中 public static void write2XML(Document document) throws Exception{ TransformerFactory factory=TransformerFactory.newInstance(); Transformer tf=factory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(fileName))); }}
6.dao层(数据访问层)
/** * 数据访问对象 * @author 芷若初荨 * @Exception StudentNotExistException */public class StudentDao {// 添加学生信息 public void add(Student s){ try { Document document=XMLUtils.getDocument();// 创建封装学生信息的标签 Element student_tag=document.createElement("student"); student_tag.setAttribute("idcard",s.getIdcard()); student_tag.setAttribute("examid", s.getExamid());// 创建用于封装学生姓名、所在地、成绩信息的标签 Element name=document.createElement("name"); Element location=document.createElement("location"); Element grade=document.createElement("grade"); name.setTextContent(s.getName()); location.setTextContent(s.getLocation()); grade.setTextContent(s.getGarde()+"");// 将这些信息追加到父标签上 student_tag.appendChild(name); student_tag.appendChild(location); student_tag.appendChild(grade);// 把封装了的学生信息标签写到文档中 document.getElementsByTagName("exam").item(0).appendChild(student_tag);// 更新内存 XMLUtils.write2XML(document); } catch (Exception e) { // TODO Auto-generated catch block //checked exception(编译时异常),一般自己抓解决,并把这个异常告诉给上一层// 将这异常转型给unchecked异常,即运行时异常 throw new RuntimeException(e); } }// 查询学生信息 public Student find(String examid){ try { Document document=XMLUtils.getDocument();// 根据学生考证id来查询学生信息 NodeList lists=document.getElementsByTagName("student"); for(int i=0;i<lists.getLength();i++){ Element student_tag=(Element) lists.item(i); if(student_tag.getAttribute("examid").equals(examid)){// 找到与examid相匹配的学生呢个,new出一个student对象封装这个学生的信息返回 Student s=new Student(); s.setExamid(examid); s.setIdcard(student_tag.getAttribute("idcard")); s.setName(student_tag.getElementsByTagName("name").item(0).getTextContent()); s.setLocation(student_tag.getElementsByTagName("location").item(0).getTextContent()); s.setGarde(Double.parseDouble(student_tag.getElementsByTagName("grade").item(0).getTextContent())); return s; } } return null; } catch (Exception e) { // TODO Auto-generated catch block throw new RuntimeException(e); } }// 删除学生信息 public void delete(String name) throws StudentNotExistException{// 获取document对象 try { Document document=XMLUtils.getDocument();// 将name的标签存储在list表中 NodeList list=document.getElementsByTagName("name"); for(int i=0;i<list.getLength();i++){// 如果list中取出的name的文本内容和所要比较的那么相等 Node node=list.item(i); if(node.getTextContent().equals(name)){// 删除该节点 node.getParentNode().getParentNode().removeChild(node.getParentNode()); XMLUtils.write2XML(document); return;//如果没有删除成功,就会返回 } }// 自定义异常 throw new StudentNotExistException(name+"不存在!"); } catch (Exception e) { // TODO Auto-generated catch block throw new RuntimeException(e);// 如果抛运行时异常,上一级遇到这个可能会忘记异常进行处理,所以这里需要使用编译型异常,如果没有删除成功,就不会给用户提供提示 } }}
7.异常类
==异常小结:==
**在后台中一般出现异常就会throws或者try,在前台和用户发生交互时,异常必须try自己处理,然后提供给用户一些友好提示就行,不能直接抛,不然会出现异常抛到了用户界面,这对用户评价我们的程序有着不好的影响。
**对于编译时异常,我们一般会自己抓并且将其记录下来然后转给下一层;对于运行时异常,只是将这个异常抛出去,抛给上一层,上一层遇到这个异常时可能会处理,也可能不会处理,对程序运行没有多大的影响。
/** * 自定义异常 * 注意:写异常时需要注意它属于哪种类型的,父亲是运行时runtime异常还是编译时的异常 * @author 芷若初荨 @Exception StudentNotExistException * */public class StudentNotExistException extends Exception { public StudentNotExistException() { // TODO Auto-generated constructor stub } public StudentNotExistException(String message) { super(message); // TODO Auto-generated constructor stub } public StudentNotExistException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } public StudentNotExistException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public StudentNotExistException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); // TODO Auto-generated constructor stub }}
**对于这个系统程序中存在add添加的方法,我们不能采用(checked Exception)编译时异常,需要将其转型成运行时异常(unchecked Exception),并且自定义一个异常StudentNotExistException(对于自定义异常时,创建时必须要搞清楚我们的异常是属于哪一类,父亲是编译时异常还是runtime异常,然后重写父亲异常的方法就行。),自定义异常比较简单,关键还是在于我们为什么要自定义异常,选择哪一类异常。
8.交互类 Main.java
/** * 交互类 * @author 芷若初荨 * */public class StudentMain { public static void main(String[] args) { System.out.println("a:添加学生 b:删除学生 c:查找学生"); System.out.println("请输入操作类型:");// 注意:一般和用户打交道的异常不能抛出去,不然会一直往上抛,最后抛给了用户,这导致不好的结果// 只要界面的程序的异常,只能自己抓取,在后台记录异常 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try { String type=br.readLine(); if("a".equals(type)){ System.out.println("请输入您的学号:"); String idcard=br.readLine(); System.out.println("请输入您的姓名:"); String name=br.readLine(); System.out.println("请输入您的准考证号:"); String examid=br.readLine(); System.out.println("请输入您的所在地:"); String location=br.readLine(); System.out.println("请输入您的成绩:"); String grade=br.readLine();// 添加内容 Student s=new Student(); s.setIdcard(idcard); s.setExamid(examid); s.setName(name); s.setLocation(location); s.setGarde(Double.parseDouble(grade));// 创建dao对象,执行添加 StudentDao dao=new StudentDao(); dao.add(s); System.out.println("添加成功!"); } else if("b".equals(type)){ System.out.println("请输入需要删除的姓名:"); String name=br.readLine();// 此处有异常,必须处理,不然上抛到用户 try {// 创建dao对象,执行删除 StudentDao dao=new StudentDao(); dao.delete(name); System.out.println("删除成功!"); } catch (StudentNotExistException e) { // TODO Auto-generated catch block System.out.println("您删除的用户不存在!"); } }else if("c".equals(type)){ System.out.println("请输入需要查询的准考证号:"); String examid=br.readLine();// 创建dao对象,执行查询 StudentDao dao=new StudentDao(); Student s=new Student(); if(dao.find(examid)!=null){ System.out.println("查询成功!"); System.out.println("您查询的准考证号为:"+examid); }else{ System.out.println("对不起,您查找的信息不存在!"); } }else{ System.out.println("对不起,您的操作有误!"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("对不起,系统正在忙!"); } }}
9.案例小结
***对于异常的分析和使用,必须需要注意,总结也就几个点:
**哪种场景需要哪类异常?(前台和后端)
**是采用编译时异常还是运行时异常?
**是自定义还是?又What?
***MVC模型的具体实现思路简单来说就是对象上传数据到dao层进行处理最后提交给交互层来向用户展示出来,具体细节部分还是得自己多仔细琢磨。
0 0
- 利用XML编程实践简易学生成绩管理系统
- 简易学生成绩管理系统(c++)
- 简易版学生成绩管理系统(c++)
- java项目之简易学生成绩管理系统
- java开发的简易学生成绩管理系统
- 学生成绩管理系统 - Linux Shell 编程
- shell 编程实验--实现学生成绩管理系统
- C语言链表实现的简易学生成绩管理系统
- 简易版学生成绩管理系统(数组版和链表版都有)
- c++链表实现学生成绩管理系统(简易版)
- Java数组实例-实现简易的学生成绩管理系统
- 学生成绩管理系统
- 学生成绩管理系统
- 学生成绩管理系统
- 学生成绩管理系统
- 学生成绩管理系统
- 学生成绩管理系统
- 学生成绩管理系统
- 如何制作android相机程序
- python学习:headers和data的获取
- 置换密码算法说明
- DAY:10结业作业—有时候想黑掉世界
- Jquery----停止动画stop
- 利用XML编程实践简易学生成绩管理系统
- MySQL的初次见面礼基础实战篇
- 第四届省赛原题 序号互换
- Hibernate 级联操作,映射文件中的默认的Lazy属性导致的异常
- PreparedStatement与Statement
- Oracle查询表空间使用情况
- xshell常用命令
- HZAU 1199 Little Red Riding Hood
- IntelliJ IDEA 2017.1注册码失效问题解决方案