⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 doctor.java

📁 数据库: hospital
💻 JAVA
字号:
package hospital.db.dboperation;

import java.sql.*;
import hospital.db.*;
import java.text.*;

/**
 * 继承自DBOperation类,封装医生角色
 *
 * 作者:Fido Dido
 */
public class Doctor
		extends DBOperation{
	public static final String NAME="Name";
	public static final String DATE="Date";

	/**
	 * 构造器
	 *
	 * 参数:
	 * id-医生编号
	 */
	public Doctor(String id)
			throws InvalidUserException{
		super(id);
	}

	/**
	 * 用户登录
	 *
	 * 参数:
	 * password-密码
	 *
	 * 返回值:操作结果
	 * 1-登录成功
	 * 0-抛出一般异常
	 * -1-抛出数据库异常
	 * -2-登录失败
	 */
	public int login(String password){
		int res=0;
		conn=DBConnection.getConnection();

		try{
			strSQL="SELECT DID,Password FROM doctor WHERE DID='" + this.id + "' AND Password=password('" + password + "')";
			stmt=conn.createStatement();
			rs=stmt.executeQuery(strSQL);

			if(!rs.next())
				throw new IllegalAccessException("Password invalid.");

			res=1;
		}
		catch(SQLException sqle){
			Debug.log(Debug.getExceptionMsg(sqle));
			res= -1;
		}
		catch(IllegalAccessException iae){
			Debug.log(Debug.getExceptionMsg(iae));
			res= -2;
		}
		catch(Exception e){
			res=0;
		}
		finally{
			return res;
		}
	}

	/**
	 * 获取所有患者病历
	 *
	 * 参数:
	 * orderBy-结果的排序方式
	 *
	 * 返回值-操作结果:
	 * 1-操作成功
	 * 0-抛出一般异常
	 * -1-抛出数据库异常
	 */
	public int getPatientsHistory(String orderBy){
		conn=DBConnection.getConnection();
		int res=0;

		if(orderBy == null || orderBy.equals(""))
			orderBy=Doctor.NAME;

		try{
			strSQL=
					"SELECT history.HID,history.Diagnose,history.Description,history.Rx,history.SDate,history.FDate,history.Finished,patient.Name,patient.Age,patient.Sex,patient.Address,patient.Phone" +
					" FROM patient,doctor,history WHERE history.Doctor='" + this.id + "' AND doctor.DID='" + this.id +
					"' AND history.Patient=patient.PID AND Finished=0 ORDER BY " + orderBy;
			stmt=conn.createStatement();
			rs=stmt.executeQuery(strSQL);
			result=rs;

			res=1;
		}
		catch(SQLException sqle){
			Debug.log(Debug.getExceptionMsg(sqle));
			res= -1;
		}
		catch(Exception e){
			res=0;
		}
		finally{
			return res;
		}
	}

	/**
	 * 获取与hid匹配的患者病历
	 *
	 * 参数:
	 * hid-病历号
	 *
	 * 返回值-操作结果:
	 * 1-操作成功
	 * 0-抛出一般异常
	 * -1-抛出数据库异常
	 */

	public int getPatientHistory(int hid){
		conn=DBConnection.getConnection();
		int res=0;

		try{
			strSQL=
					"SELECT history.HID,history.Diagnose,history.Description,history.Rx,history.SDate,history.FDate,history.Finished,patient.Name,patient.Age,patient.Sex,patient.Address,patient.Phone" +
					" FROM patient,history WHERE HID=" + hid + " AND history.Patient=patient.PID";
			stmt=conn.createStatement();
			rs=stmt.executeQuery(strSQL);
			result=rs;

			res=1;
		}
		catch(SQLException sqle){
			Debug.log(Debug.getExceptionMsg(sqle));
			res= -1;
		}
		catch(Exception e){
			res=0;
		}
		finally{
			return res;
		}
	}

	/**
	 * 获取预约队列中患者信息
	 *
	 * 返回值-操作结果:
	 * 1-操作成功
	 * 0-抛出一般异常
	 * -1-抛出数据库异常
	 */
	public int getPatientInQueueInfo(){
		int res=0;
		conn=DBConnection.getConnection();
		strSQL="SELECT QID,Date,Name,Age,Sex,Phone,Address FROM pinqueue,patient WHERE Doctor='" + this.id +
				"' AND PID=Patient";

		try{
			stmt=conn.createStatement();
			rs=stmt.executeQuery(strSQL);

			res=1;
			result=rs;
		}
		catch(SQLException sqle){
			res= -1;
			Debug.log(Debug.getExceptionMsg(sqle));
		}
		catch(Exception e){
			res=0;
		}
		finally{
			return res;
		}
	}

	/**
	 * 检查用户名合法性
	 *
	 * 参数:
	 * id-医生编号
	 */
	protected void checkUser(String id)
			throws InvalidUserException{
		conn=DBConnection.getConnection();

		strSQL="SELECT DID FROM doctor WHERE DID='" + id + "'";

		try{
			stmt=conn.createStatement();
			rs=stmt.executeQuery(strSQL);

			if(!rs.next())
				throw new InvalidUserException();
		}
		catch(SQLException sqle){
			Debug.log(Debug.getExceptionMsg(sqle));
		}
	}

	/**
	 * 创建病历
	 *
	 * 参数:
	 * qid-患者在预约队列中编号
	 * diagnose-诊断
	 * rx-处方
	 * description-症状
	 *
	 * 返回值-操作结果:
	 * 1-操作成功
	 * 0-抛出一般异常
	 * -1-抛出数据库异常
	 * -2-非法qid值
	 */
	public synchronized int createHistory(int qid,String diagnose,String rx,String description){
		int res=0;
		Connection conn=DBConnection.getConnection();
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date=dateFormat.format(new java.util.Date());
		String[] days={"SunA","SunP","MonA","MonP","TueA","TueP","WedA","WedP","ThuA","ThuP","FriA","FriP","SatA","SatP"};
		int index,curpc,day,ap;
		int pid;
		String did;

		try{
			strSQL="SELECT * FROM pinqueue WHERE QID=" + qid + " AND Doctor='" + this.id + "'";
			stmt=conn.createStatement();
			rs=stmt.executeQuery(strSQL);
			if(!rs.next())
				throw new IllegalArgumentException("Invalid QID or permission denied.");
			pid=rs.getInt("Patient");
			day=rs.getInt("Day");
			ap=rs.getInt("AP");
			did=rs.getString("Doctor");
			index=day * 2 + ap;

			strSQL="SELECT * FROM curappointment WHERE DID='" + did + "'";
			rs=stmt.executeQuery(strSQL);
			rs.next();
			curpc=rs.getInt(days[index]);

			conn.setAutoCommit(false);
			stmt=conn.createStatement();

			strSQL="INSERT INTO history (Diagnose,Patient,Doctor,Rx,Description,Finished,SDate) VALUES ('" + diagnose + "'," +
					pid + ",'" + this.id + "','" + rx + "','" + description + "',0,'" + date + "')";
			stmt.addBatch(strSQL);

			strSQL="DELETE FROM pinqueue WHERE QID=" + qid;
			stmt.addBatch(strSQL);

			strSQL="UPDATE curappointment SET " + days[index] + "=" + (curpc + 1) + " WHERE DID='" + did + "'";
			stmt.addBatch(strSQL);

			stmt.executeBatch();
			conn.commit();
			res=1;
		}
		catch(SQLException sqle){
			res= -1;
			conn.rollback();
			Debug.log(Debug.getExceptionMsg(sqle));
		}
		catch(IllegalArgumentException iae){
			Debug.log(Debug.getExceptionMsg(iae));
			res= -2;
		}
		catch(Exception e){
			res=0;
		}
		finally{
			return res;
		}
	}

	/**
	 * 修改病历
	 *
	 * 参数:
	 * hid-病历编号
	 * diagnose-诊断
	 * rx-处方
	 * description-症状
	 *
	 * 返回值-操作结果:
	 * 1-操作成功
	 * 0-抛出一般异常
	 * -1-抛出数据库异常
	 * -2-非法hid值
	 */
	public synchronized int updateHistory(int hid,String description,String rx,String diagnose){
		conn=DBConnection.getConnection();
		int res=0;

		try{
			strSQL="SELECT HID FROM history WHERE HID=" + hid + " AND Doctor='" + this.id + "' AND Finished=0";
			stmt=conn.createStatement();
			rs=stmt.executeQuery(strSQL);
			if(!rs.next())
				throw new IllegalArgumentException("Invalid HID or permission denied.");

			strSQL="UPDATE history SET Description='" + description + "',Rx='" + rx + "',Diagnose='" + diagnose +
					"' WHERE HID=" + hid;
			stmt.executeUpdate(strSQL);
			res=1;
		}
		catch(SQLException sqle){
			res= -1;
			Debug.log(Debug.getExceptionMsg(sqle));
		}
		catch(IllegalArgumentException iae){
			res= -2;
			Debug.log(Debug.getExceptionMsg(iae));
		}
		catch(Exception e){
			res=0;
		}
		finally{
			return res;
		}
	}

	/**
	 * 结束治疗
	 *
	 * 参数:
	 * hid-病历编号
	 *
	 * 返回值-操作结果:
	 * 1-操作成功
	 * 0-抛出一般异常
	 * -1-抛出数据库异常
	 * -2-非法hid值
	 */
	public synchronized int finishHistory(int hid){
		int res=0;
		conn=DBConnection.getConnection();
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date=dateFormat.format(new java.util.Date());

		try{
			stmt=conn.createStatement();

			strSQL="SELECT HID FROM history WHERE HID=" + hid + " AND Doctor='" + this.id + "'";
			rs=stmt.executeQuery(strSQL);
			if(!rs.next())
				throw new IllegalArgumentException("Invalid HID or permission denied.");

			strSQL="UPDATE history SET Finished=1,FDate='" + date + "' WHERE HID=" + hid;
			stmt.executeUpdate(strSQL);

			res=1;
		}
		catch(SQLException sqle){
			res= -1;
			Debug.log(Debug.getExceptionMsg(sqle));
		}
		catch(Exception e){
			res=0;
		}
		finally{
			return res;
		}
	}

	/**
	 * 更改密码
	 *
	 * 参数:
	 * password-新密码
	 *
	 * 返回值-操作结果:
	 * 1-操作成功
	 * 0-抛出一般异常
	 * -1-抛出数据库异常
	 */
	public synchronized int updatePassword(String password){
		int res=0;
		conn=DBConnection.getConnection();

		try{
			strSQL="UPDATE doctor SET Password=password('" + password + "') WHERE DID='" + this.id + "'";
			stmt=conn.createStatement();
			stmt.executeUpdate(strSQL);

			res=1;
		}
		catch(SQLException sqle){
			res= -1;
			Debug.log(Debug.getExceptionMsg(sqle));
		}
		catch(Exception e){
			Debug.log(Debug.getExceptionMsg(e));
			res=0;
		}
		finally{
			return res;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -