xml实现student考试成绩数据库

首页

1.实现选择a可以添加学生相关信息

2.选择b提示输入examid可以查询学生信息

3.选择c输入学生姓名可以删除学生

分析:

1.我们要有一个xml文件存放数据

2.我们需要一个javabean来封装我们获得的xml数据

3.我们需要一个dao层来操作xml

4.要有一个面向用户的视图层

5.需要有一个工具类 来获得docment,更新xml文件

 

首先在src下创建一个student.xml文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?><data>
<student examid="897654" idcard="123">
<name>张三</name>
<score>89.2</score>
</student>
<student examid="897655" idcard="124">
<name>李四</name>
<score>76.8</score>
</student>

</data>

 

javabean  Student类

package com.study.xml.domain;

public class Student {
private int idcard;
private int examid;
private String name;
private double score;

public Student(int idcard, int examid, String name, double score) {
super();
this.idcard = idcard;
this.examid = examid;
this.name = name;
this.score = score;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public int getIdcard() {
return idcard;
}
public void setIdcard(int idcard) {
this.idcard = idcard;
}
public int getExamid() {
return examid;
}
public void setExamid(int examid) {
this.examid = examid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}

@Override
public String toString() {
return "idcard:"+this.idcard+",examid:"+this.examid+",name:"+this.name+",score:"+this.score;
}

}

 

 

xml.util;工具类

package com.study.xml.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.omg.CORBA.DomainManager;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class xmlUtil {
private static String fileName="src/Student.xml";

public static Document getDocument () throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
return builder.parse(new File(fileName));
}

public static void flushDocument (Document document) throws FileNotFoundException, TransformerException{
TransformerFactory fac=TransformerFactory.newInstance();
Transformer tf=fac.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(fileName)));
}
}

 

dao层操作xml数据库的类StudentDao

package com.study.xml.dao;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.study.xml.domain.Student;
import com.study.xml.util.xmlUtil;

public class StudentDao {


public void add(Student stu){
try {
Document document=xmlUtil.getDocument();
//创建封装学生的标签
Element student_tag=document.createElement("student");
student_tag.setAttribute("idcard", stu.getIdcard()+"");
student_tag.setAttribute("examid", stu.getExamid()+"");

//创建学生信息的子标签
Element name_tag= document.createElement("name");
Element score_tag= document.createElement("score");

name_tag.setTextContent(stu.getName());
score_tag.setTextContent(stu.getScore()+"");

student_tag.appendChild(name_tag);
student_tag.appendChild(score_tag);

//将封装好的student标签放到xml的总标签下
document.getElementsByTagName("data").item(0).appendChild(student_tag);

//刷新内存
xmlUtil.flushDocument(document);
System.out.println("添加成功!");
} catch (Exception e) {
//异常转换为RuntimeException
throw new RuntimeException(e);
}
}


public Student find(int examid){
Student stu=null;
try {
//获取document
Document document=xmlUtil.getDocument();

//获取student标签的nodelist集合
NodeList nodes=document.getElementsByTagName("student");

//遍历nodelist
for(int i=0;i<nodes.getLength();i++){
Element student_tag=(Element) nodes.item(i);
//查找student的examid属性和传入的值相等的student
if((student_tag.getAttribute("examid")).equals(examid+"")){
//获取student标签的idcard属性内容
int idcard=Integer.parseInt(student_tag.getAttribute("idcard"));
//获取student标签的name子标签的内容
String name=student_tag.getElementsByTagName("name").item(0).getTextContent();
//获取student标签的score子标签的内容
double score=Double.parseDouble(student_tag.getElementsByTagName("score").item(0).getTextContent());

//将获取到的student信息封装到stu对象当中
stu=new Student(idcard, examid, name, score);
}
}

} catch (Exception e) {
//异常转换为RuntimeException
throw new RuntimeException(e);
}
//返回对象
return stu;
}



public void del(String name){
Document document;
try {
document = xmlUtil.getDocument();

//获取name标签的nodelist集合
NodeList nodes=document.getElementsByTagName("name");
//遍历nodelist
for(int i=0;i<nodes.getLength();i++){
Element name_tag=(Element) nodes.item(i);

//判断name和传入的变量相等时
if(name.equals(name_tag.getTextContent())){
//获取name的父标签student
Element student_tag=(Element) name_tag.getParentNode();
//获取student标签的子标签score
Element score_tag=(Element) student_tag.getElementsByTagName("score").item(0);
//先删除student标签的子标签name、score
student_tag.removeChild(name_tag);
student_tag.removeChild(score_tag);
//再删除student标签
student_tag.getParentNode().removeChild(student_tag);

//结果写入xml文件
xmlUtil.flushDocument(document);
System.out.println("学生删除成功!!");
}


}

} catch (Exception e) {
//异常转换为RuntimeException
throw new RuntimeException(e);
}

}

}

 

 

 

面向用户层

package com.study.xml.util;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.study.xml.dao.StudentDao;
import com.study.xml.domain.Student;


public class Test {
private static Student stu=null;
private static StudentDao dao=new StudentDao();

public static void main(String[] args) {
while(true){
init();
operate();
}

}
private static void init() {
System.out.println("\t\t学生成绩管理系统");
System.out.println("=========================================");
System.out.println(" a.添加学生\t b.查询学生\t c.删除学生");
System.out.println("请选择:");

}

private static void operate() {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
String str = br.readLine();
if(str.equals("a")){
addStu(br);
}
if(str.equals("b")){
findStu(br);
}
if(str.equals("c")){
delStu(br);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private static void addStu(BufferedReader br ) throws IOException {
System.out.println("请输入学生name:");
String name=br.readLine();
System.out.println("请输入学生idcard:");
String idcard=br.readLine();
System.out.println("请输入学生examid:");
String examid=br.readLine();
System.out.println("请输入学生score:");
String score=br.readLine();
Student stu=new Student(Integer.parseInt(idcard),Integer.parseInt(examid), name, Double.parseDouble(score));

dao.add(stu);


}

private static void findStu(BufferedReader br) throws IOException {
System.out.println("请输入学生examid:");
String examid=br.readLine();

System.out.println(dao.find(Integer.parseInt(examid)));
}


private static void delStu(BufferedReader br) throws IOException {
System.out.println("请输入学生name:");
String name=br.readLine();

dao.del(name);
}


}

 

posted @ 2018-04-14 19:08  pc伊人  阅读(315)  评论(0编辑  收藏  举报