一个以XML作数据库的简易考生成绩管理系统的实现,包含了:
·基于java面向对象思想的程序架构设计
·xml解析技术
·异常抓取
·IO文件流相关知识。
麻雀虽小,五脏俱全!~
程序结构图:
//xml文档,在这里相当于数据库,用以存放、读取数据 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <exam> <student examid="333" idcard="3333"> <name>张三</name> <location>长沙</location> <grade>89</grade> </student> <student examid="444" idcard="4444"> <name>李四</name> <location>岳阳</location> <grade>97</grade> </student> <student examid="555" idcard="5555"> <name>王五</name> <grade>99.0</grade> <location>永州</location> </student> </exam> //domain包(java bean),Student类,包括其各种属性 package he.junhua.domain; public class Student { private String idcard; private String examid; private String name; private String location; private double grade; 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 getGrade() { return grade; } public void setGrade(double grade) { this.grade = grade; } } //DAO包(Data access object),StudentDao类,用以实现Student的各种方法 package he.junhua.dao; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import he.junhua.domain.Student; import he.junhua.exception.StudentNotExistException; import he.junhua.util.XmlUtil; public class StudentDao { public void add(Student s) { try { // 得到得到文档的document Document document = XmlUtil.getDocument(); // 创建一个用以封装学生信息的标签 Element student_tag = (Element) document.createElement("student"); student_tag.setAttribute("idcard",s.getIdcard()); student_tag.setAttribute("examid",s.getExamid()); // 创建用以封装学生name location grade的标签 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.getGrade() + ""); student_tag.appendChild(name); student_tag.appendChild(grade); student_tag.appendChild(location); // 将封装了学生信息的标签挂到文档上去 document.getElementsByTagName("exam").item(0).appendChild( student_tag); // 更新文档 XmlUtil.write2Xml(document); } catch (Exception e) { throw new RuntimeException(e); } } public Student find(String examid) throws StudentNotExistException { try { // 得到得到文档的document Document document = XmlUtil.getDocument(); NodeList list = document.getElementsByTagName("student"); for (int i = 0; i < list.getLength(); i++) { Element student_tag = (Element) list.item(i); if (student_tag.getAttribute("examid").equals(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.setGrade(Double.parseDouble(student_tag .getElementsByTagName("grade").item(0) .getTextContent())); return s; } } throw new StudentNotExistException(examid + "不存在!!"); } catch (StudentNotExistException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } public void delete(String name) throws StudentNotExistException { try { Document document = XmlUtil.getDocument(); NodeList list = document.getElementsByTagName("name"); for (int i = 0; i < list.getLength(); i++) { if (list.item(i).getTextContent().equals(name)) { list.item(i).getParentNode().getParentNode().removeChild( list.item(i).getParentNode()); // 更新文档 XmlUtil.write2Xml(document); return; } } throw new StudentNotExistException(name + "不存在!!"); } catch (StudentNotExistException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } } //工具(util)包,用以实现文档获取和文档写入 package he.junhua.util; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class XmlUtil { private static String filename = "src/exam.xml"; public static Document getDocument() throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document document = builder.parse(filename); return document; } public static void write2Xml(Document document) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(new DOMSource(document),new StreamResult(new FileOutputStream(filename))); } } //UI包,用以实现人机交换,存放main函数 package he.junhua.UI; import he.junhua.dao.StudentDao; import he.junhua.domain.Student; import he.junhua.exception.StudentNotExistException; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { /** * @param args * @throws IOException */ public static void main(String[] args) { while (true) { try { System.out.println("(a)添加学生 (b)查询成绩 (c)删除学生 (d)退出"); System.out.print("请输入操作:"); BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); String type = br.readLine(); if (type.equals("a")) { // new出一个student用以封装要添加学生的信息 Student s = new Student(); System.out.print("请输入学生的准考证:"); s.setExamid(br.readLine()); System.out.print("请输入学生的姓名:"); s.setName(br.readLine()); System.out.print("请输入学生的身份证:"); s.setIdcard(br.readLine()); System.out.print("请输入学生的所在地:"); s.setLocation(br.readLine()); System.out.print("请输入学生的成绩:"); s.setGrade(Double.parseDouble(br.readLine())); StudentDao dao = new StudentDao(); dao.add(s); System.out.println("添加成功!"); } else if (type.equals("b")) { System.out.print("请输入要查找学生的准考证号:"); String examid = br.readLine(); StudentDao dao = new StudentDao(); Student s = new Student(); try { s = dao.find(examid); System.out.println("准考证号:" + s.getExamid() + " 姓名:" + s.getName() + " 成绩:" + s.getGrade()); } catch (StudentNotExistException e) { System.out.println("您要查找的学生不存在!"); } } else if (type.equals("c")) { System.out.print("请输入您要删除的学生姓名:"); String name = br.readLine(); StudentDao dao = new StudentDao(); try { dao.delete(name); System.out.println("删除成功!"); } catch (StudentNotExistException e) { System.out.println("您要删除的学生不存在!"); } } else if (type.equals("d")) { break; } else { System.out.println("请输入正确的选择序号!"); } } catch (Exception e) { System.out.println("sorry,出错了!"); } } } }