考试系统(用xml文件模拟数据库)
Posted vamps0911
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了考试系统(用xml文件模拟数据库)相关的知识,希望对你有一定的参考价值。
1.界面介绍
2.系统结构介绍
我把各个包介绍一下
1.exam.xml文件就是用来模拟数据库的,代码在后面贴出来
2.dao层时操作数据包
3.utils是工具类包
4.bean是数据封装类包
5.exception是自定义异常类包
6.junit测试包
7.ui用户界面包
3.详细代码
1.exam.xml
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?><exam> 2 <student examid="456" idcard="123"> 3 <name>a</name> 4 <location>b</location> 5 <grade>22</grade> 6 </student> 7 8 <student examid="000" idcard="789"> 9 <name>c</name> 10 <location>d</location> 11 <grade>20</grade> 12 </student> 13 14 15 </exam>
2.XmlUtils.java
1 package utils; 2 3 import java.io.FileOutputStream; 4 5 import javax.xml.parsers.DocumentBuilder; 6 import javax.xml.parsers.DocumentBuilderFactory; 7 import javax.xml.transform.Transformer; 8 import javax.xml.transform.TransformerFactory; 9 import javax.xml.transform.dom.DOMSource; 10 import javax.xml.transform.stream.StreamResult; 11 12 import org.w3c.dom.Document; 13 14 //工具类的所有方法都是静态的(约定俗成) 15 public class XmlUtils { 16 17 private static String filename = "src/exam.xml"; 18 19 public static Document getDocument() throws Exception{ 20 21 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 22 DocumentBuilder builder = factory.newDocumentBuilder(); 23 return builder.parse(filename); 24 25 } 26 27 public static void write2Xml(Document document) throws Exception{ 28 29 TransformerFactory factory = TransformerFactory.newInstance(); 30 Transformer tf = factory.newTransformer(); 31 tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename))); 32 33 } 34 35 }
getDocument()方法用来获取标签对象
write2Xml(Document document)方法用来更新xml文件
3.StudentDao.java
1 package dao; 2 3 import org.w3c.dom.Document; 4 import org.w3c.dom.Element; 5 import org.w3c.dom.NodeList; 6 7 import bean.Student; 8 import exception.StudentNotExistException; 9 import utils.XmlUtils; 10 11 public class StudentDao { 12 13 public void add(Student s){ 14 15 try { 16 Document document = XmlUtils.getDocument(); 17 18 //创建出封装学生的标签 19 Element student_tag = document.createElement("student"); 20 student_tag.setAttribute("idcard", s.getIdcard()); 21 student_tag.setAttribute("examid", s.getExamid()); 22 23 //创建用于封装学生姓名,所在地和成绩的标签 24 Element name = document.createElement("name"); 25 Element location = document.createElement("location"); 26 Element grade = document.createElement("grade"); 27 name.setTextContent(s.getName()); 28 location.setTextContent(s.getIdcard()); 29 grade.setTextContent(s.getGrade()+""); 30 31 //设置标签之间的关系 32 student_tag.appendChild(name); 33 student_tag.appendChild(location); 34 student_tag.appendChild(grade); 35 36 //把封装信息的学生标签,要挂在文档上面 37 document.getElementsByTagName("exam").item(0).appendChild(student_tag); 38 39 //更新内存 40 XmlUtils.write2Xml(document); 41 42 } catch (Exception e) { 43 //把异常进行转型,把编译时异常转化成运行时异常 44 throw new RuntimeException(e); 45 } 46 47 48 } 49 50 public Student find(String examid){ 51 52 try { 53 Document document = XmlUtils.getDocument(); 54 NodeList list = document.getElementsByTagName("student"); 55 for(int i=0; i<list.getLength(); i++){ 56 Element student_tag = (Element) list.item(i); 57 if(student_tag.getAttribute("examid").equals(examid)){ 58 //找到与examid相对应的学生,new出一个student对象封装这个学生的信息返回 59 Student s = new Student(); 60 s.setExamid(examid); 61 s.setIdcard(student_tag.getAttribute("idcard")); 62 s.setName(student_tag.getElementsByTagName("name").item(0).getTextContent()); 63 s.setLocation(student_tag.getElementsByTagName("location").item(0).getTextContent()); 64 s.setGrade(Double.parseDouble(student_tag.getElementsByTagName("grade").item(0).getTextContent())); 65 return s; 66 } 67 } 68 return null; 69 } catch (Exception e) { 70 // TODO Auto-generated catch block 71 throw new RuntimeException(e); 72 } 73 } 74 75 public void delete(String name) throws StudentNotExistException{ 76 77 try { 78 Document document = XmlUtils.getDocument(); 79 NodeList list = document.getElementsByTagName("name"); 80 for(int i=0; i<list.getLength(); i++){ 81 if(list.item(i).getTextContent().equals(name)){ 82 list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode()); 83 XmlUtils.write2Xml(document); 84 return; 85 } 86 } 87 throw new StudentNotExistException(name + "不存在!"); 88 }catch(StudentNotExistException e){ 89 throw e; 90 }catch (Exception e) { 91 // TODO Auto-generated catch block 92 throw new RuntimeException(); 93 } 94 95 } 96 97 }
4.student.java
1 package bean; 2 3 public class Student { 4 5 @Override 6 public String toString() { 7 return "Student [name=" + name + ", location=" + location + ", grade=" + grade + ", idcard=" + idcard 8 + ", examid=" + examid + "]"; 9 } 10 private String name; 11 private String location; 12 private double grade; 13 private String idcard; 14 private String examid; 15 16 public String getName() { 17 return name; 18 } 19 public void setName(String name) { 20 this.name = name; 21 } 22 public String getLocation() { 23 return location; 24 } 25 public void setLocation(String location) { 26 this.location = location; 27 } 28 public double getGrade() { 29 return grade; 30 } 31 public void setGrade(double grade) { 32 this.grade = grade; 33 } 34 public String getIdcard() { 35 return idcard; 36 } 37 public void setIdcard(String idcard) { 38 this.idcard = idcard; 39 } 40 public String getExamid() { 41 return examid; 42 } 43 public void setExamid(String examid) { 44 this.examid = examid; 45 } 46 47 }
5.StudentNotExistException.java
1 package exception; 2 3 public class StudentNotExistException extends Exception { 4 5 public StudentNotExistException() { 6 // TODO Auto-generated constructor stub 7 } 8 9 public StudentNotExistException(String message) { 10 super(message); 11 // TODO Auto-generated constructor stub 12 } 13 14 public StudentNotExistException(Throwable cause) { 15 super(cause); 16 // TODO Auto-generated constructor stub 17 } 18 19 public StudentNotExistException(String message, Throwable cause) { 20 super(message, cause); 21 // TODO Auto-generated constructor stub 22 } 23 24 public StudentNotExistException(String message, Throwable cause, boolean enableSuppression, 25 boolean writableStackTrace) { 26 super(message, cause, enableSuppression, writableStackTrace); 27 // TODO Auto-generated constructor stub 28 } 29 30 }
6.Main.java
1 package ui; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 6 import bean.Student; 7 import dao.StudentDao; 8 import exception.StudentNotExistException; 9 10 public class Main { 11 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 try{ 15 System.out.println("添加学生:(a) 删除学生(b) 查找学生(c) 退出(e)"); 16 System.out.print("请输入操作类型:"); 17 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 18 String type = br.readLine(); 19 //switch不能用String 20 if("a".equals(type)){ 21 22 System.out.println("请输入学生姓名:"); 23 String name = br.readLine(); 24 25 System.out.println("请输入学生准考证号:"); 26 String examid = br.readLine(); 27 28 System.out.println("请输入学生身份证号:"); 29 String idcard = br.readLine(); 30 31 System.out.println("请输入学生所在地:"); 32 String location = br.readLine(); 33 34 System.out.println("请输入学生成绩:"); 35 String grade = br.readLine(); 36 37 Student s = new Student(); 38 s.setExamid(examid); 39 s.setGrade(Double.parseDouble(grade)); 40 s.setIdcard(idcard); 41 s.setName(name); 42 s.setLocation(location); 43 44 StudentDao dao = new StudentDao(); 45 dao.add(s); 46 System.out.println("添加成功"); 47 48 }else if("b".equals(type)){ 49 50 System.out.print("请输入要删除学生的姓名:"); 51 String name = br.readLine(); 52 try{ 53 StudentDao dao = new StudentDao(); 54 dao.delete(name); 55 System.out.println("删除成功!"); 56 }catch(StudentNotExistException e){ 57 System.out.println("此学生不存在!"); 58 } 59 60 }else if("c".equals(type)){ 61 62 System.out.println("请输入要查找用户的准考证号"); 63 String examid = br.readLine(); 64 StudentDao dao = new StudentDao(); 65 Student s = dao.find(examid); 66 System.out.println(s); 67 68 }else if("e".equals(type)){ 69 70 System.out.println("退出成功!"); 71 return; 72 73 }else{ 74 System.out.println("不支持你的操作!"); 75 } 76 }catch(Exception e){ 77 e.printStackTrace(); 78 System.out.println("对不起,出错误了"); 79 } 80 } 81 82 }
以上是关于考试系统(用xml文件模拟数据库)的主要内容,如果未能解决你的问题,请参考以下文章
Android - 使用活动和片段导航 - 二进制 XML 文件错误