基于XML数据库的学生信息管理系统的设计与实现

本项目是在学习之余写的,主要用来练习MVC+DAO的分层设计思想,项目基于一个简单的XML学生数据库,使用XML作为数据库的原因是其十分的小巧与方便,使用dom4j即可进行方便的解析。因为这段时间课程一直比较密集,在Java相关内容的学习上比较滞后。希望用这个小项目来练一练久矣不写代码的手,也作为研一一个月来的学习小结吧。不废话了,具体内容如下:


项目说明:

本项目提供简单的用户界面(命令行),实现对XML数据库中学生数据的增删改查操作。项目要求使用分层设计思想,为什么要分层?分层可以将不同的代码块分离出来,避免不同功能的代码耦合,为程序扩展和维护提供方便。使用分层,也可以简单化一个系统的设计过程。

DB: students.xml
- Entity: Student 类 (packet: cn.myseu.test.stuMana.entity)
- Dao: StudentDao 类,完成具体的增删改查操作 (packet: cn.myseu.test.stuMana.dao)
- UI: StudentUI 类,实现用户输入操作  (packet: cn.myseu.test.stuMana.ui)
- SMS 类,提供主函数和用户界面  ( packet: cn.myseu.test.stuMana )


数据库如下:student.xml

 

<?xml version="1.0" encoding="UTF-8"?>



<students> 

  <student id="133122"> 

    <name>well</name>  

    <age>22</age> 

  </student> 

</students>


设计Student类完成对数据库中数据的封装

 

 

package cn.myseu.test.stuMana.entity;



public class Student {

	private String id;

	private String name;

	private String age;

	

	

	public String getId() {

		return id;

	}

	public void setId(String id) {

		this.id = id;

	}

	public String getName() {

		return name;

	}

	public void setName(String name) {

		this.name = name;

	}

	public String getAge() {

		return age;

	}

	public void setAge(String age) {

		this.age = age;

	}

	

	

}


设计StudentDao类完成对XML数据库的增删改查操作(这里将公操作进行了抽取,分布为getDocument()和write2xml())

 

 

package cn.myseu.test.stuMana.dao;



import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.io.UnsupportedEncodingException;



import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Element;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;



import cn.myseu.test.stuMana.entity.Student;



public class StudentDao {

	

	/**

	 * Add a new student to the database

	 * @param s

	 * @return

	 * @throws Exception 

	 */

	public boolean create(Student s) throws Exception{

		boolean flag = false;

		if(s!=null){

			Document doc = getDocument();

			Element root = doc.getRootElement();

			Element newElement = root.addElement("student");

			newElement.addAttribute("id", s.getId());

			newElement.addElement("name").setText(s.getName());

			newElement.addElement("age").setText(s.getAge());

			write2xml(doc);

			flag = true;

		}

		return flag;

	}

	

	/**

	 * delete a student from the xml-database

	 * @param id  the student_id you want to delete

	 * @return

	 * @throws Exception

	 */

	public boolean delete(String id) throws Exception{

		boolean flag = false;

		Document doc = getDocument();

		String xpath = "//student[@id='"+id+"']";

		Element element = (Element) doc.selectSingleNode(xpath);

		if(element!=null){

			element.getParent().remove(element);

			write2xml(doc);

			flag = true;

		}else

			System.out.println("ID is not existed!");

		return flag;

	}

	

	/**

	 * search the information of a specified student

	 * @param id

	 * @throws Exception

	 */

	public void check(String id) throws Exception{

		Document doc = getDocument();

		String xpath = "//student[@id='"+id+"']";

		Element element = (Element) doc.selectSingleNode(xpath);

		if(element!=null){

			System.out.println("stu_id:"+element.attributeValue("id"));

			System.out.println("stu_name:"+element.elementText("name"));

			System.out.println("stu_age:"+element.elementText("age"));

		}else

			System.out.println("ID is not existed!");

	}

	

	/**

	 * update a specified student

	 * @param s

	 * @return

	 * @throws Exception

	 */

	public boolean update(Student s) throws Exception{

		boolean flag = false;

		Document doc = getDocument();

		String xpath = "//student[@id='"+s.getId()+"']";

		Element element = (Element) doc.selectSingleNode(xpath);

		if(element!=null){			

			element.element("name").setText(s.getName());

			element.element("age").setText(s.getAge());

			write2xml(doc);

			flag = true;

		}

		else

			System.out.println("ID is not existed!");

		return flag;

	}



	/*

	 * the public function

	 * getDocument() & write2xml

	 */

	private void write2xml(Document doc) throws FileNotFoundException,

			UnsupportedEncodingException, IOException {

		OutputStream os = new FileOutputStream(new File("src/cn/myseu/test/stuMana/db/students.xml"));

		OutputFormat format = OutputFormat.createPrettyPrint();

		XMLWriter xw = new XMLWriter(os,format);

		xw.write(doc);

		xw.close();

	}



	private Document getDocument() throws DocumentException {

		SAXReader sr = new SAXReader();

		Document doc = sr.read(new File("src/cn/myseu/test/stuMana/db/students.xml"));

		return doc;

	}

	

}


设计StudentUI类,完成系统功能:

 

 

package cn.myseu.test.stuMana.ui;



import java.util.Scanner;



import cn.myseu.test.stuMana.dao.StudentDao;

import cn.myseu.test.stuMana.entity.Student;



public class StudentUI {

	

	public void create() throws Exception{

		Scanner scanner = new Scanner(System.in);

		System.out.println("Input stu_id:");

		String id = scanner.nextLine();

		System.out.println("Input stu_name:");

		String name = scanner.nextLine();

		System.out.println("Input stu_age:");

		String age = scanner.nextLine();

		

		Student s = new Student();

		s.setId(id);

		s.setName(name);

		s.setAge(age);

		

		StudentDao stuDao = new StudentDao();

		boolean flag = stuDao.create(s);

		if(flag)

			System.out.println("Your create operation is successful!");

		else

			System.out.println("Your create operation is failed!");

	}

	

	public void delete() throws Exception{

		Scanner scanner = new Scanner(System.in);

		System.out.println("Input the stu_id of the student who you want to delete:");

		String id = scanner.nextLine();

		StudentDao stuDao = new StudentDao();

		boolean flag = stuDao.delete(id);

		if(flag)

			System.out.println("Your delete operation is successful!");

		else

			System.out.println("Your delete operation is failed!");

	}

	

	public void check() throws Exception{

		Scanner scanner = new Scanner(System.in);

		System.out.println("Input the stu_id of the student who you want to get:");

		String id = scanner.nextLine();

		StudentDao stuDao = new StudentDao();

		stuDao.check(id);

	}

	

	public void update() throws Exception{

		Scanner scanner = new Scanner(System.in);

		System.out.println("Input the stu_id of the student who you want to update:");

		String id = scanner.nextLine();

		System.out.println("The new name:");

		String name = scanner.nextLine();

		System.out.println("The new age:");

		String age = scanner.nextLine();

		Student s = new Student();

		s.setId(id);

		s.setAge(age);

		s.setName(name);

		StudentDao stuDao = new StudentDao();

		boolean flag = stuDao.update(s);

		if(flag)

			System.out.println("Your delete operation is successful!");

		else

			System.out.println("Your delete operation is failed!");

	}

}


设计SMS类,提供系统菜单和用户输入输出:

 

 

package cn.myseu.test.stuMana;



import java.util.Scanner;



import cn.myseu.test.stuMana.ui.StudentUI;



public class SMS {

	public static void main(String[] args) throws Exception {

		System.out.println("========================================");

		System.out.println("====== Student Management System =======");

		System.out.println("====== Version 1.0 Author:Well   =======");

		System.out.println("========================================");

		System.out.println("1.create a new student");

		System.out.println("2.delete a student from the system");

		System.out.println("3.search information of a student");

		System.out.println("4.update information of a student");

		System.out.println("========================================");

		System.out.println("Please input your operation number:");

		

		Scanner scanner = new Scanner(System.in);

		StudentUI sui = new StudentUI();

		

		int num = Integer.parseInt(scanner.nextLine());

		switch(num){

			case 1:

				sui.create();break;

			case 2:

				sui.delete();break;

			case 3:

				sui.check();break;

			case 4:

				sui.update();break;

			default:

				System.out.println("You should input form [1-4]");;break;

		}

	}

}


最后,简单而丑陋的系统运行界面如下:

 

基于XML数据库的学生信息管理系统的设计与实现


最后,这个简单丑陋的系统仅仅是练习和复习相关的知识所用。但我认为也是一个非常简单的分层设计思想的很好的体现。系统简单容易上手。大概用了3个小时左右设计完成。

 

你可能感兴趣的:(xml)