表示层:
public class StudentUI {
//入口
public static void main(String[] args) throws Exception {
input();
}
public static void input() throws Exception{
Scanner scanner = new Scanner(System.in);
System.out.print("学员的编号:");
String id = scanner.nextLine();
System.out.print("更新学员的姓名:");
String name = scanner.nextLine();
System.out.print("更新学员的年龄:");
String age = scanner.nextLine();
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
StudentDao studentDao = new StudentDao();
studentDao.update(student);
/*键盘输入学员信息
Scanner scanner = new Scanner(System.in);
System.out.print("查询学员的编号:");
String id = scanner.nextLine();
StudentDao studentDao = new StudentDao();
studentDao.read(id);
Scanner scanner = new Scanner(System.in);
System.out.print("删除学员的编号:");
String id = scanner.nextLine();
StudentDao studentDao = new StudentDao();
studentDao.delete(id);
Scanner scanner = new Scanner(System.in);
System.out.print("用户名:" );
String name = scanner.nextLine();
System.out.print("年龄:" );
String age = scanner.nextLine();
//封装成JavaBean对象
Student student = new Student();
student.setId(IdUtil.getId());
student.setName(name);
student.setAge(age);
//调用Dao对象
StudentDao studentDao = new StudentDao();
boolean flag = studentDao.create(student);
if(flag){
System.out.println("操作成功");
}else{
System.out.println("操作失败");
}
*/
}
}
工具:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class IDUtil {
public static String getID() throws Exception
{
//
BufferedReader br = new BufferedReader(new FileReader("src/day2/mode2/ui/id.txt"));
String id = br.readLine();
int temp = Integer.parseInt(id)+1; //(int 型)
BufferedWriter bw = new BufferedWriter(new FileWriter("src/day2/mode2/ui/id.txt"));
bw.write(temp+"");
bw.flush();
bw.close();
return id;
}
}
访问层:
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import cn.itcast.xml.example3.domain.Student;
public class StudentDao {
//根据编号更新学员信息
public void update(Student student) throws Exception {
Document document = getDocument();
String xpath = "//student[@id='"+student.getId()+"']";
Element element = (Element) document.selectSingleNode(xpath);
if(element!=null){
element.element("name").setText(student.getName());
element.element("age").setText(student.getAge());
write2xml(document);
}else{
System.out.println("查无此学员");
}
}
//根据编号显示学员信息
public void read(String id) throws Exception{
Document document = getDocument();
String xpath = "//student[@id='"+id+"']";
Element element = (Element) document.selectSingleNode(xpath);
if(element!=null){
System.out.println("编号:" + element.attributeValue("id"));
System.out.println("姓名:" + element.elementText("name"));
System.out.println("年龄:" + element.elementText("age"));
}else{
System.out.println("查无此学员");
}
}
//根据编号删除某位学员的信息
public void delete(String id) throws Exception{
Document document = getDocument();
String xpath = "//student[@id='"+id+"']";
Element element = (Element) document.selectSingleNode(xpath);
if(element!=null){
element.getParent().remove(element);
write2xml(document);
}else{
System.out.println("查无此学员");
}
}
//增加学员的信息
public boolean create(Student student) throws Exception{
boolean flag = false;
if(student!=null){
Document document = null;
try {
document = getDocument();
} catch (Exception e) {
//创建空XML文件
document = DocumentHelper.createDocument();
//创建<students>根元素
document.addElement("students");
}
Element rootElement = document.getRootElement();
Element studentElement = rootElement.addElement("student");
studentElement.addAttribute("id",student.getId());
studentElement.addElement("name").setText(student.getName());
studentElement.addElement("age").setText(student.getAge());
write2xml(document);
flag = true;
}
return flag;
}
//将内存中的Document写到硬盘
private void write2xml(Document document) throws Exception {
OutputFormat format = OutputFormat.createPrettyPrint();
OutputStream os = new FileOutputStream("src/cn/itcast/xml/example3/db/students.xml");
XMLWriter xmlWriter = new XMLWriter(os,format);
xmlWriter.write(document);
xmlWriter.close();
}
//取得Document对象
private Document getDocument() throws Exception {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("src/cn/itcast/xml/example3/db/students.xml"));
return document;
}
}
数据层:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="2011100801">
<name>keke</name>
<age>11</age>
</student>
</students>
实体层
public class Student {
private String id;
private String name;
private String age;
public Student(){}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) throws Exception {
if(name.matches("[a-zA-Z]+")){
this.name = name;
}else{
throw new Exception();
}
}
public String getAge() {
return age;
}
public void setAge(String age) throws Exception {
if(age.matches("[0-9]+")){
this.age = age;
}else{
throw new Exception();
}
}
}
转载于:https://blog.51cto.com/357712148/2104133