前言:
本次实验老师要求我们用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发表博客,自己喜欢应用编程也有两三年了,实在羞愧。