用xml代替数据库的一个小项目--学生管理系统
Student.java
package com.bean;
public class Student {
private String examid;
private String idcard;
private String name;
private String location;
private double grade;
public String getExamid() {
return examid;
}
public void setExamid(String examid) {
this.examid = examid;
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
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;
}
}
StudentUtil.java
package com.utils;
import java.io.File;
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;
import org.w3c.dom.Element;
import com.bean.Student;
public class StudentUtil {
private static final String file = "src/student.xml";
// 获得Document文件的方法
public static Document getDocument() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
return db.parse(file);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 写入xml文件的方法
public static void writeXml(Document document) {
try {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new File(file)));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//把节点的值封装到bean中
public static Student nodeBean(Element e,Class<Student> c){
try {
Student s = c.newInstance();
s.setExamid(e.getAttribute("examid"));
s.setIdcard(e.getAttribute("idcard"));
s.setName(e.getElementsByTagName("name").item(0).getTextContent());
s.setLocation(e.getElementsByTagName("location").item(0).getTextContent());
s.setGrade(Double.parseDouble(e.getElementsByTagName("grade").item(0).getTextContent()));
return s;
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
StudentDao.java
package com.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.bean.Student;
import com.utils.StudentUtil;
public class StudentDao {
/**
* @param args
*/
//增加学生
public void insertS(Student s){
Document document = StudentUtil.getDocument();
//创建sutudet节点
Element rootNode = document.createElement("student");
//给sutudet节点设置属性
rootNode.setAttribute("examid",s.getExamid());
rootNode.setAttribute("idcard",s.getIdcard());
//创建sutudet子节点
Element name = document.createElement("name");
Element location = document.createElement("location");
Element grade = document.createElement("grade");
//给sutudet子节点设值
name.setTextContent(s.getName());
location.setTextContent(s.getLocation());
grade.setTextContent(s.getGrade()+"");
//把sutudet子节点追加到跟节点后
rootNode.appendChild(name);
rootNode.appendChild(location);
rootNode.appendChild(grade);
//把sutudet节点加入到根节点
document.getElementsByTagName("exam").item(0).appendChild(rootNode);
//写入到xml文件
StudentUtil.writeXml(document);
}
//删除学生
public void deleteS(String name){
Document document = StudentUtil.getDocument();
NodeList list = document.getElementsByTagName("name");
for(int i=0;i<list.getLength();i++){
Element node = (Element) list.item(i);
if(node.getTextContent().equals(name)){
node.getParentNode().getParentNode().removeChild(node.getParentNode());
StudentUtil.writeXml(document);
return;
}
}
}
//查询学生
public Student queryS(String examid){
Document document = StudentUtil.getDocument();
NodeList list = document.getElementsByTagName("student");
for(int i=0;i<list.getLength();i++){
Element studentNode = (Element) list.item(i);
String value = studentNode.getAttribute("examid");
if(value.equals(examid)){
Student student = StudentUtil.nodeBean(studentNode, Student.class);
return student;
}
}
return null;
}
}
StudentTest.java
package com.juil;
import org.junit.Test;
import com.bean.Student;
import com.dao.StudentDao;
public class StudentTest {
/**
* @param args
*/
private Student student = new Student();
private StudentDao dao = new StudentDao();
//测试添加学生
@Test
public void insertTest(){
student.setExamid("12345");
student.setIdcard("1122");
student.setName("杨凯");
student.setLocation("邢台");
student.setGrade(100);
dao.insertS(student);
}
@Test
public void queryTest(){
dao.queryS("111");
System.out.println(student.getName());
}
@Test
public void deleteTest(){
dao.deleteS("李小三");
}
}
Main.java
package com.main;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.bean.Student;
import com.dao.StudentDao;
public class Main {
public static void main(String[] args) {
while (true) {
try {
System.out.println("添加学生(a) 查找学生(b) 删除学生(c) 退出(quit)");
System.out.print("请选择操作的类型:");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String type = br.readLine();
if ("quit".equalsIgnoreCase(type)) {
return;
}
if (!type.matches("[abcABC]")) {
System.out.println("请输入正确的操作类型!!");
continue;
}
if (type.equalsIgnoreCase("a")) {
// 进入添加学生环节
try {
System.out.print("请输入学生姓名:");
String name = br.readLine();
System.out.print("请输入学生准考证号:");
String examid = br.readLine();
System.out.print("请输入学生身份证号:");
String idcard = br.readLine();
System.out.print("请输入学生所在地:");
String location = br.readLine();
System.out.print("请输入学生成绩:");
double grade = Double.parseDouble(br.readLine());
Student s = new Student();
s.setExamid(examid);
s.setGrade(grade);
s.setIdcard(idcard);
s.setLocation(location);
s.setName(name);
StudentDao dao = new StudentDao();
dao.insertS(s);
System.out.println("数据录入成功!!");
} catch (Exception e) {
System.out.println("由于未知原因,录入失败!!");
}
} else if (type.equalsIgnoreCase("b")) {
//进入查找环节
try {
System.out.print("请输入学生准考证号:");
String examid = br.readLine();
Student s = new Student();
s.setExamid(examid);
StudentDao dao = new StudentDao();
s = dao.queryS(examid);
System.out.println("名字 :" + s.getName());
System.out.println("准考证号: " + s.getExamid());
System.out.println("学号: " + s.getIdcard());
System.out.println("所在地: " + s.getLocation());
System.out.println("分数: " + s.getGrade());
System.out.println("查找成功!!!");
} catch (RuntimeException e) {
System.out.println("由于未知原因,查询失败!!");
}
} else {
//进入删除环节
try {
System.out.print("请输入学生姓名:");
String name = br.readLine();
Student s = new Student();
s.setName(name);
StudentDao dao = new StudentDao();
dao.deleteS(name);
System.out.println("删除成功!!!");
} catch (RuntimeException e) {
System.out.println("由于未知原因,删除失败!!");
}
}
} catch (Exception e) {
System.out.println("系统出错了,请重新再试!!!");
}
}
}
}
student.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
<student examid="222" idcard="111">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>
<student examid="444" idcard="333">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
<student examid="111" idcard="222">
<name>xxx</name>
<location>北京</location>
<grade>45.0</grade>
</student>
<student examid="12345" idcard="1122">
<name>杨凯</name>
<location>邢台</location>
<grade>100.0</grade>
</student>
</exam>