📄 doctor.java
字号:
package crqs.dboperation;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Date;
import java.util.ArrayList;
import java.text.*;
import crqs.infobeans.*;
import crqs.util.Page;
import crqs.util.DateConvert;
import crqs.util.RateCollection;
import crqs.exceptions.RecordsNotExistException;
public class Doctor extends DBOperation implements Serializable {
private String name; // 医生真实姓名
private double rate; // 预约率
private int pages; // 结果分页数
final static int DAYS_PER_WEEK = 7;
public Doctor() {
}
public Doctor(String un, String pw) {
super(un, pw);
}
public String getName() {
return name;
}
public int getPages() {
return pages;
}
// true:密码修改成功
// false:旧密码不正确
// SQLException:数据库异常
public boolean setPassword(String old, String newPw) throws SQLException {
String sql = "Select Passwd from Doctor where UserName='"
+ this.userName + "'";
try {
executeQuery(sql);
result.next();
if (!result.getString(1).equals(old.trim()))
return false;
else {
sql = "Update Doctor set Passwd='" + newPw
+ "' where UserName='" + this.userName + "'";
executeUpdate(sql);
return true;
}
} finally {
closeConnection();
}
}
// 取得当前一周的预约情况,包括今天的
// 数据库异常, 抛出SQLException
// ResultSet为空,抛出RecordNotExistException
public Schedule[] getSchedule() throws SQLException {
String today = DateConvert.convert(new Date());
String query = "Select * from Schedule where SDate >= '" + today
+ "' and DoctorID = '" + this.userName + "'";
try {
executeQuery(query);
} catch (SQLException e) {
throw e;
}
Schedule[] schedules = new Schedule[DAYS_PER_WEEK];
try {
int i = 0;
Date d = new Date();
if (!result.next()) {
for (; i < DAYS_PER_WEEK; i++) {
schedules[i] = new Schedule(this.userName, d, 0, 0);
DateConvert.tomorrow(d);
}
return schedules;
}
do {
if (i < DAYS_PER_WEEK)
schedules[i++] = new Schedule(this.userName, result
.getDate("SDate"), result.getInt("Maximum"), result
.getInt("Reserved"));
} while (result.next());
if (i < DAYS_PER_WEEK)
for (; i < DAYS_PER_WEEK; i++) {
DateConvert.tomorrow(d);
schedules[i] = new Schedule(this.userName, d, 0, 0);
}
return schedules;
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
// 设置每天的预约量
// 一般异常,返回false
// 操作成功,返回true
// 数据库异常,抛出SQLException
public synchronized void setSchedule(Schedule[] schedules)
throws SQLException {
boolean res = false;
try {
for (int i = 0; i < schedules.length; i++) {
String query = "Select * from Schedule where DoctorID = '"
+ this.userName + "' and SDate = '"
+ schedules[i].getdDate() + "'";
executeQuery(query);
if (result.next()) {
query = "Update Schedule set Maximum = "
+ schedules[i].getdMax() + " where"
+ " DoctorID = '" + this.userName
+ "' and SDate = '" + schedules[i].getdDate() + "'";
} else {
query = "Insert into Schedule values('"
+ schedules[i].getdId() + "', '"
+ schedules[i].getdDate() + "', "
+ schedules[i].getdMax() + ", 0)";
}
executeUpdate(query);
}
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
// 返回医生的基本信息
// 数据库异常,抛出SQLException
// ResultSet为空,抛出RecordNotExistException
public DoctorInfo getInfo() throws SQLException, RecordsNotExistException {
String query = "Select Doctor.*, SName, TName"
+ " From Doctor, Section, Title" + " where UserName='"
+ this.userName + "' and Doctor.SectionNO = Section.SectionNO"
+ " and Doctor.Title = TitleNO";
DoctorInfo Dinfo = new DoctorInfo();
try {
executeQuery(query);
if (result.next()) {
Dinfo.setUserName(result.getString("UserName"));
Dinfo.setRealName(result.getString("DName"));
Dinfo.setAge(result.getInt("Age"));
Dinfo.setPassword(result.getString("Passwd"));
Dinfo.setGender(result.getString("Gender").charAt(0));
Dinfo.setPhone(result.getString("PhoneNO"));
Dinfo.setMailBox(result.getString("Mailbox"));
Dinfo.setResume(result.getString("Resume"));
Dinfo.setSection(result.getString("SName"));
Dinfo.setTitle(result.getString("TName"));
return Dinfo;
} else {
throw new RecordsNotExistException();
}
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
// 返回病人的基本信息
// 数据库异常,抛出SQLException
// ResultSet为空,抛出RecordNotExistException
public PatientInfo getPatientInfo(String logname) throws SQLException,
RecordsNotExistException {
String query = "Select * from Patient where UserName = '" + logname
+ "'";
PatientInfo pinfo = new PatientInfo();
try {
executeQuery(query);
if (result.next()) {
pinfo.setUserName(result.getString("UserName"));
pinfo.setPassword(result.getString("Passwd"));
pinfo.setRealName(result.getString("PName"));
pinfo.setAge(result.getInt("Age"));
pinfo.setGender(result.getString("Gender").charAt(0));
pinfo.setIdentity(result.getString("IDCard"));
pinfo.setPhone(result.getString("PhoneNO"));
pinfo.setMailBox(result.getString("Mailbox"));
pinfo.setAddress(result.getString("Address"));
return pinfo;
} else {
throw new RecordsNotExistException();
}
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
// 修改个人信息
// 一般异常,返回false
// 操作成功,返回true
// 数据库异常, 抛出SQLException
// ResultSet为空, 抛出RecordsNotExistException
public synchronized boolean setInfo(String mailbox, String phone)
throws SQLException, RecordsNotExistException {
boolean res = false;
try {
String query = "Update Doctor set Mailbox = '" + mailbox
+ "', PhoneNO = '" + phone + "' where UserName = '"
+ this.userName + "'";
if (executeUpdate(query) != 0) {
res = true;
return res;
} else {
res = false;
throw new RecordsNotExistException();
}
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
// 得到指定病人的所有病历
// 数据库异常, 抛出SQLException
// ResultSet为空, 抛出RecordsNotExistException
public Page getCaseList(String pid) throws SQLException,
RecordsNotExistException {
String query = "Select CaseNO, DName, DoctorID,ResDate, SName "
+ "from CaseView where " + "PatientID = '"
+ pid + "' and Finished='y'";
CaseItem item = new CaseItem();
ArrayList caselist = new ArrayList();
int i = 0;
try {
executeQuery(query);
if (result.next()) {
// item = new CaseItem[result.getRow()];
do {
item.setCaseID(result.getString("CaseNO"));
item.setDoctorId(result.getString("DoctorID"));
item.setDoctorName(result.getString("DName"));
item.setCaseDate(result.getDate("ResDate"));
item.setSection(result.getString("SName"));
caselist.add(item);
item = new CaseItem();
} while (result.next());
Page page = new Page(caselist);
return page;
} else {
throw new RecordsNotExistException();
}
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
// 根据病历号查看病历,只能查看已经完成的
// 数据库异常, 抛出SQLException
// ResultSet为空, 抛出RecordsNotExistException
public CaseInfo getCase(String caseno) throws SQLException,
RecordsNotExistException {
String query = "Select * From Cases where CaseNO='"+caseno+"'";
CaseInfo cinfo = new CaseInfo();
try {
executeQuery(query);
if (result.next()) {
cinfo.setCaseID(caseno);
cinfo.setDescription(result.getString("Description"));
cinfo.setDiagnose(result.getString("Diagnose"));
cinfo.setPrescription(result.getString("Prescription"));
cinfo.setFinish(result.getString("Finished").charAt(0));
return cinfo;
} else {
throw new RecordsNotExistException();
}
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
// 修改病人的病历
// 操作成功,返回true
// 数据库异常, 抛出SQLException
// ResultSet为空, 抛出RecordsNotExistException
public synchronized boolean setCase(String caseId, String description,
String diagnose, String prescription) throws SQLException,
RecordsNotExistException {
boolean res = false;
try {
String query = "Update Cases set Finished='y', Description = '"
+ description + "', Diagnose = '" + diagnose
+ "', Prescription = '" + prescription
+ "' where CaseNO = '" + caseId + "'and Finished = 'n'";
if (executeUpdate(query) != 0) {
res = true;
return res;
} else {
res = false;
throw new RecordsNotExistException();
}
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
public double getMark(Date from, Date to)
throws SQLException, RecordsNotExistException{
String query = "Select Mark From Reservation where Mark > 0"
+ " and ResDate >= '"
+ DateConvert.convert(from)
+ "' and ResDate <= '"
+ DateConvert.convert(to)
+ "' and Confirmed = 'y' and DoctorID = '"
+ this.userName + "'";
double markTotal = 0;
try {
executeQuery(query);
if(!result.next())
throw new RecordsNotExistException();
int rows = 0;
do{
rows++;
markTotal += result.getInt("Mark");
}while (result.next());
markTotal /= rows;
return markTotal;
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
//返回指定时间段内的预约率
//取得一段时间的预约率
//数据库异常,抛出SQLException
//ResultSet为空, 抛出RecordNotExistException
public RateCollection getResRate(Date from, Date to)
throws SQLException, RecordsNotExistException{
int maxTotal = 0, resTotal = 0;
try {
if (to.before(from))
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
String query = "Select Maximum, Reserved, SDate From Schedule where "
+ "DoctorID = '" + this.userName + "' and SDate >= '"
+ DateConvert.convert(from)
+ "' and SDate <= '"
+ DateConvert.convert(to) + "'";
ArrayList alist = new ArrayList();
int res=0;
int m=0;
double r=0;
Rate rate=null;
try {
executeQuery(query);
if(!result.next())
throw new RecordsNotExistException();
do{
res=result.getInt("Reserved");
m=result.getInt("Maximum");
r=(m==0)?0:(double)res/m;
rate=new Rate();
rate.setDate(result.getDate("SDate"));
rate.setMax(m);
rate.setReserved(res);
rate.setResRate(r);
maxTotal += m;
resTotal += res;
alist.add(rate);
}while (result.next());
r = maxTotal == 0 ?0:(double) resTotal / maxTotal ;
RateCollection rc=new RateCollection(alist);
rc.setAvRate(r);
return rc;
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
// 返回指定时间段内的预约
// 数据库异常,抛出SQLException
// ResultSet为空, 抛出RecordNotExistException
public Page getReservation(Date from, Date to) throws SQLException,
RecordsNotExistException {
String query = "Select PatientID, PName, "
+ "ResNO, ResDate, Mark, Confirmed, SName "
+ "From ReservationView "
+ "where DoctorID = '"
+ this.userName
+ "' and ResDate >= '" + DateConvert.convert(from)
+ "' and ResDate <= '" + DateConvert.convert(to) + "'";
ArrayList alist = new ArrayList();
Reservation reserve = new Reservation();
try {
executeQuery(query);
if (result.next()) {
do {
reserve.setPatientId(result.getString("PatientID"));
reserve.setPatientName(result.getString("PName"));
reserve.setDoctorId(this.userName);
reserve.setDoctorName(this.name);
reserve.setResNo(result.getString("ResNO"));
reserve.setReservationDate(result.getDate("ResDate"));
reserve.setMark(result.getInt("Mark"));
reserve.setConfirmed(result.getString("Confirmed")
.charAt(0));
reserve.setSeciton(result.getString("SName"));
alist.add(reserve);
reserve = new Reservation();
} while (result.next());
Page page = new Page(alist);
return page;
} else {
throw new RecordsNotExistException();
}
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
public boolean login() throws SQLException, RecordsNotExistException {
String sql = "Select DName, Passwd from Doctor where UserName='"
+ this.userName + "'";
try {
executeQuery(sql);
if (result.next()) {
this.name = result.getString("DName");
if (result.getString("Passwd").equals(this.password))
return true;
else
return false;
} else
throw new RecordsNotExistException();
} catch (SQLException e) {
throw e;
} finally {
closeConnection();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -