中醫門診電子病歷xml文檔數據讀入數據庫實驗流程演示

前言:

本次實驗老師要求我們用java程序實現,其整個實現流程也滿足了老師提出的實驗要求。由於絕大部分都是在課堂上完成的,寫得有誤的地方還請大家包涵。

1.下載所需的jar包並通過項目Build Path選項添加進來

這次實驗用到了兩個外部jar包,分別是解析xml的dom4j包和jdbc連接mysql數據庫的驅動包。

中醫門診電子病歷單參考:

(圖1.1)

2.根據以上圖片信息編寫xml文檔

(圖1.2)

編寫的比較簡易,沒有添加任何約束和其他必要條件。

將編寫好的xml文件放到工程文件夾下,如下圖所示:

(圖1.3)

 

3.建數據庫和表

這次實驗用到的是mysql數據庫。

4.編寫java代碼

一共建了4個類,如上圖1.3,ReadXml類用於讀取工程下的2.xml數據,PatientBean類用於封裝圖1.1的字段信息,Conndb類用於連接mysql數據庫,MainClass類爲運行主類。部分代碼如下所示:

ReadXml類

package com.xmlread.cn;
/**
 * 
 * @author 李欽弘
 * @description 讀取xml的java類
 */
import java.io.File;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReadXml {
	protected static void readText() {
		//1.獲得Document
		SAXReader saxReader = new SAXReader();
		Document document = null;
		try {
			document = saxReader.read(new File("src/com/xmltext/cn/2.xml"));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		//2.獲得根節點
		Element rootElement = document.getRootElement();
		//3.節點導航
		Element e_info = rootElement.element("element-information");
		//3.1節點導航
		Element d_info = rootElement.element("deagnostic-information");
		//4.封裝屬性值
		PatientBean p = new PatientBean();
		 p.setApartment(e_info.elementText("kesi"));
		 p.setVisittime(e_info.elementText("time"));
		 p.setP_name(e_info.elementText("name"));
		 p.setP_gender(e_info.elementText("sex"));
		 p.setP_age(e_info.elementText("age"));
		 p.setP_tel(e_info.elementText("phone"));
		 p.setP_ID(e_info.elementText("id"));
		 p.setP_addr(e_info.elementText("address"));
		 p.setP_zhushu(d_info.elementText("main-suit"));
		 p.setP_sizhen(d_info.elementText("four-diagnostic"));
		 p.setP_clinical_diagnosis(d_info.elementText("clinical-diagnostic"));
		 p.setP_syndrome(d_info.elementText("zhengxing"));
		 p.setP_history_allergy(d_info.elementText("drug-allergy"));
		 p.setP_treatmethod(d_info.elementText("therapy"));
		 p.setP_location_disease(d_info.elementText("bingwei"));
		 p.setP_nature_disease(d_info.elementText("bingxing"));
		 p.setP_chinese_medicine(rootElement.elementText("prescription"));
		 try {
			Conndb.insertData(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(p.toString());
	}

}

PatientBean類

package com.xmlread.cn;
/**
 * 
 * @author 李欽弘
 * @description 電子病歷實體
 */
public class PatientBean {
	/**
	 * 科室
	 */
	private String apartment;
	/**
	 * 就診時間
	 */
	private String visittime;
	/**
	 * 姓名
	 */
	private String p_name;
	/**
	 * 性別
	 */
	private String p_gender;
	/**
	 * 年齡
	 */
	private String p_age;
	/**
	 * 聯繫電話
	 */
	private String p_tel;
	/**
	 * 身份證號
	 */
	private String p_ID;
	/**
	 * 家庭住址
	 */
	private String p_addr;
	/**
	 * 主訴
	 */
	private String p_zhushu;
	/**
	 * 四診信息
	 */
	private String p_sizhen;
	/**
	 * 臨牀診斷
	 */
	private String p_clinical_diagnosis;
	/**
	 * 證型
	 */
	private String p_syndrome;
	/**
	 * 藥物過敏史
	 */
	private String p_history_allergy;
	/**
	 * 治法
	 */
	private String p_treatmethod;
	/**
	 * 病位
	 */
	private String p_location_disease;
	/**
	 * 病性
	 */
	private String p_nature_disease;
	/**
	 * 中藥處方
	 */
	private String p_chinese_medicine;
	public String getApartment() {
		return apartment;
	}
	public void setApartment(String apartment) {
		this.apartment = apartment;
	}
	public String getVisittime() {
		return visittime;
	}
	public void setVisittime(String visittime) {
		this.visittime = visittime;
	}
	public String getP_name() {
		return p_name;
	}
	public void setP_name(String p_name) {
		this.p_name = p_name;
	}
	public String getP_gender() {
		return p_gender;
	}
	public void setP_gender(String p_gender) {
		this.p_gender = p_gender;
	}
	public String getP_age() {
		return p_age;
	}
	public void setP_age(String p_age) {
		this.p_age = p_age;
	}
	public String getP_tel() {
		return p_tel;
	}
	public void setP_tel(String p_tel) {
		this.p_tel = p_tel;
	}
	public String getP_ID() {
		return p_ID;
	}
	public void setP_ID(String p_ID) {
		this.p_ID = p_ID;
	}
	public String getP_addr() {
		return p_addr;
	}
	public void setP_addr(String p_addr) {
		this.p_addr = p_addr;
	}
	public String getP_zhushu() {
		return p_zhushu;
	}
	public void setP_zhushu(String p_zhushu) {
		this.p_zhushu = p_zhushu;
	}
	public String getP_sizhen() {
		return p_sizhen;
	}
	public void setP_sizhen(String p_sizhen) {
		this.p_sizhen = p_sizhen;
	}
	public String getP_clinical_diagnosis() {
		return p_clinical_diagnosis;
	}
	public void setP_clinical_diagnosis(String p_clinical_diagnosis) {
		this.p_clinical_diagnosis = p_clinical_diagnosis;
	}
	public String getP_syndrome() {
		return p_syndrome;
	}
	public void setP_syndrome(String p_syndrome) {
		this.p_syndrome = p_syndrome;
	}
	public String getP_history_allergy() {
		return p_history_allergy;
	}
	public void setP_history_allergy(String p_history_allergy) {
		this.p_history_allergy = p_history_allergy;
	}
	public String getP_treatmethod() {
		return p_treatmethod;
	}
	public void setP_treatmethod(String p_treatmethod) {
		this.p_treatmethod = p_treatmethod;
	}
	
	public String getP_location_disease() {
		return p_location_disease;
	}
	public void setP_location_disease(String p_location_disease) {
		this.p_location_disease = p_location_disease;
	}
	public String getP_nature_disease() {
		return p_nature_disease;
	}
	public void setP_nature_disease(String p_nature_disease) {
		this.p_nature_disease = p_nature_disease;
	}
	public String getP_chinese_medicine() {
		return p_chinese_medicine;
	}
	public void setP_chinese_medicine(String p_chinese_medicine) {
		this.p_chinese_medicine = p_chinese_medicine;
	}
	
	@Override
	public String toString() {
		return "patientBean [apartment=" + apartment + ", visittime=" + visittime + ", p_name=" + p_name + ", p_gender="
				+ p_gender + ", p_age=" + p_age + ", p_tel=" + p_tel + ", p_ID=" + p_ID + ", p_addr=" + p_addr
				+ ", p_zhushu=" + p_zhushu + ", p_sizhen=" + p_sizhen + ", p_clinical_diagnosis=" + p_clinical_diagnosis
				+ ", p_syndrome=" + p_syndrome + ", p_history_allergy=" + p_history_allergy + ", p_treatmethod="
				+ p_treatmethod + ", p_location_disease=" + p_location_disease + ", p_nature_disease="
				+ p_nature_disease + ", p_chinese_medicine=" + p_chinese_medicine + "]";
	}
	
}

Conndb類

package com.xmlread.cn;
/**
 * 
 * @author 李欽弘
 * @description 連接數據庫的java類
 */
import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class Conndb {
	protected static void  insertData(PatientBean rx) throws ClassNotFoundException, SQLException {
		// 1.加載驅動
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲取Connection對象取
		Connection con = (Connection) DriverManager.getConnection("jdbc:mysql:///表所在數據庫名","你的數據庫登錄名", "登錄密碼");
		// 3.獲取Statement對象
		Statement st = (Statement) con.createStatement();
		// 4.添加操作
		String sql = "insert into patientdata(apartment,visittime,p_name,"
				+ "p_gender,p_age,p_tel,"
				+ "p_ID,p_addr,p_zhushu,"
				+ "p_sizhen,p_clinical_diagnosis,p_syndrome,"
				+ "p_history_allergy,p_treatmethod,p_location_disease,"
				+ "p_nature_disease,p_chinese_medicine) values('"+rx.getApartment()+"','"+rx.getVisittime()+"','"+rx.getP_name()+"',"
				+ "'"+rx.getP_gender()+"','"+rx.getP_age()+"',"
				+ "'"+rx.getP_tel()+"','"+rx.getP_ID()+"','"+rx.getP_addr()+"',"
				+ "'"+rx.getP_zhushu()+"','"+rx.getP_sizhen()+"','"+rx.getP_clinical_diagnosis()+"',"
				+ "'"+rx.getP_syndrome()+"','"+rx.getP_history_allergy()+"','"+rx.getP_treatmethod()+"',"
				+ "'"+rx.getP_location_disease()+"','"+rx.getP_nature_disease()+"','"+rx.getP_chinese_medicine()+"')";
		int row = st.executeUpdate(sql);
		if (row != 0) {
			System.out.println("添加成功");
		}
		// 5.關閉資源
		st.close();
		con.close();
	}

}
表所在數據庫名","你的數據庫登錄名", "登錄密碼");
		// 3.獲取Statement對象
		Statement st = (Statement) con.createStatement();
		// 4.添加操作
		String sql = "insert into patientdata(apartment,visittime,p_name,"
				+ "p_gender,p_age,p_tel,"
				+ "p_ID,p_addr,p_zhushu,"
				+ "p_sizhen,p_clinical_diagnosis,p_syndrome,"
				+ "p_history_allergy,p_treatmethod,p_location_disease,"
				+ "p_nature_disease,p_chinese_medicine) values('"+rx.getApartment()+"','"+rx.getVisittime()+"','"+rx.getP_name()+"',"
				+ "'"+rx.getP_gender()+"','"+rx.getP_age()+"',"
				+ "'"+rx.getP_tel()+"','"+rx.getP_ID()+"','"+rx.getP_addr()+"',"
				+ "'"+rx.getP_zhushu()+"','"+rx.getP_sizhen()+"','"+rx.getP_clinical_diagnosis()+"',"
				+ "'"+rx.getP_syndrome()+"','"+rx.getP_history_allergy()+"','"+rx.getP_treatmethod()+"',"
				+ "'"+rx.getP_location_disease()+"','"+rx.getP_nature_disease()+"','"+rx.getP_chinese_medicine()+"')";
		int row = st.executeUpdate(sql);
		if (row != 0) {
			System.out.println("添加成功");
		}
		// 5.關閉資源
		st.close();
		con.close();
	}

}

MainClass類

package com.xmlread.cn;
/**
 * 
 * @author 李欽弘
 * @description 開始執行的主類
 */
public class MainClass {
public static void main(String[] args) {
	ReadXml.readText();
	}
}

控制檯輸出結果:

數據庫表數據:

5.總結

以上就是整個實驗的解答過程,快速完成了老師佈置的試驗任務,解答思路也得到了老師的表揚,爲此,留作紀念。

6.題外話

這是學生第一次使用CSDN發表博客,自己喜歡應用編程也有兩三年了,實在羞愧。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章