📄 patient.java
字号:
package hospital.db.dboperation;
import java.sql.*;
import hospital.db.*;
import java.text.*;
/**
* 继承自DBOperation类,用以封装患者角色
*
* 作者:Fido Dido
*/
public class Patient
extends DBOperation{
public static final String NAME="Name";
public static final String SECTION="Section";
public static final String ANONYMOUS="anonymous";
private int PID;
/**
* 构造器
*
* 参数:
* id-用户名
*/
public Patient(String id)
throws InvalidUserException{
super(id);
}
/**
* 用户登录
*
* 参数:
* password-密码
*
* 返回值-操作结果:
* 1-操作成功
* 0-抛出一般异常
* -1-抛出数据库异常
* -2-登录失败
*/
public int login(String password){
conn=DBConnection.getConnection();
int res=0;
try{
strSQL="SELECT Username,Password,PID FROM patient WHERE Username='" + Util.encode(this.id) +
"' AND Password=password('" + Util.encode(password) + "')";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(strSQL);
if(!rs.next())
throw new IllegalArgumentException("Password invalid.");
this.PID=rs.getInt("PID");
res=1;
}
catch(IllegalArgumentException iae){
Debug.log(Debug.getExceptionMsg(iae));
res= -2;
}
catch(SQLException sqle){
Debug.log(Debug.getExceptionMsg(sqle));
res= -1;
}
catch(Exception e){
res=0;
}
finally{
return res;
}
}
/**
* 用户注册
*
* 参数:
* username-用户名
* name-姓名
* password-密码
* age-年龄
* sex-性别
* address-联系地址
* phone-联系电话
*
* 返回值-操作结果:
* 1-操作成功
* 0-抛出一般异常
* -1-抛出数据库异常
* -2-帐户已存在
*/
public synchronized int addPatient(String username,String name,String password,int age,int sex,String address,
String phone){
int res=0;
conn=DBConnection.getConnection();
try{
strSQL="SELECT pid FROM patient WHERE patient.Username='" + username + "'";
stmt=conn.createStatement();
rs=stmt.executeQuery(strSQL);
if(rs.next())
throw new IllegalArgumentException("Patient Username '" + username + "' already existed.");
strSQL="INSERT INTO patient (Name,Username,Password,Age,Sex,Address,Phone) VALUES('" + name + "','" +
username + "',password('" + password + "')," + age + ",'" + sex + "','" + address + "','" + phone + "')";
stmt=conn.createStatement();
stmt.executeUpdate(strSQL);
res=1;
}
catch(SQLException sqle){
Debug.log(Debug.getExceptionMsg(sqle));
res= -1;
}
catch(IllegalArgumentException iae){
Debug.log(Debug.getExceptionMsg(iae));
res= -2;
}
catch(Exception e){
res=0;
}
finally{
return res;
}
}
/**
* 查询医生信息
*
* 参数:
* orderBy-结果集排序方式
*
* 返回值-操作结果:
* 1-操作成功
* 0-抛出一般异常
* -1-抛出数据库异常
*/
public int getDoctorInfo(String orderBy){
if(orderBy == null || orderBy.equals(""))
orderBy=Patient.NAME;
int res=0;
conn=DBConnection.getConnection();
strSQL="SELECT DID,Name,Age,Sex,Level,Section,Specialism,Phone FROM doctor ORDER BY " + orderBy;
try{
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 getCurrentDoctorInfo(){
int res=0;
conn=DBConnection.getConnection();
strSQL="SELECT Name,Age,Sex,Level,Section,Specialism,Phone FROM doctor,history WHERE history.Patient=" +
this.PID + " AND Finished=0 AND doctor.DID=history.Doctor";
try{
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 getHistory(){
int res=0;
conn=DBConnection.getConnection();
strSQL="SELECT * FROM history WHERE Patient=" + this.PID;
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;
}
}
/**
* 预约
*
* 参数:
* did-医生编号
* day-预约日(0为周日,1为周一……)
* ap-上下午(0为上午,1为下午)
*
* 返回值-操作结果:
* 1-操作成功
* 0-抛出一般异常
* -1-抛出数据库异常
* -2-已在相同时间预约相同医生
* -3-day或ap格式错
*/
public synchronized int makeAppointment(String did,int day,int ap){
int res=0;
conn=DBConnection.getConnection();
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String[] days={"SunA","SunP","MonA","MonP","TueA","TueP","WedA","WedP","ThuA","ThuP","FriA","FriP","SatA","SatP"};
int index=0,curpc=0;
try{
if(day < 0 || day > 6 || ap < 0 || ap > 1)
throw new NumberFormatException();
index=day * 2 + ap;
stmt=conn.createStatement();
strSQL=
"SELECT * FROM curappointment WHERE DID='" +
did + "'";
rs=stmt.executeQuery(strSQL);
if(!rs.next() || (curpc=rs.getInt(days[index])) == 0)
throw new NumberFormatException();
strSQL="SELECT Doctor FROM pinqueue WHERE Doctor='" + did + "' AND Patient=" + this.PID + " AND Day=" + day +
" AND AP=" + ap;
rs=stmt.executeQuery(strSQL);
if(rs.next())
throw new IllegalArgumentException();
conn.setAutoCommit(false);
stmt=conn.createStatement();
strSQL="INSERT INTO pinqueue (Patient,Doctor,Day,AP,Date) VALUES (" + this.PID + ",'" + did + "'," + day + "," +
ap + ",'" + dateFormat.format(new java.util.Date()) + "')";
stmt.addBatch(strSQL);
strSQL="UPDATE curappointment SET " + days[index] + "=" + (curpc - 1) + " WHERE DID='" + did + "'";
stmt.addBatch(strSQL);
stmt.executeBatch();
conn.commit();
res=1;
}
catch(NumberFormatException nfe){
res= -3;
}
catch(SQLException sqle){
conn.rollback();
res= -1;
Debug.log(Debug.getExceptionMsg(sqle));
}
catch(IllegalArgumentException iae){
res= -2;
}
catch(Exception e){
Debug.log(Debug.getExceptionMsg(e));
res=0;
}
finally{
return res;
}
}
/**
* 取消预约
*
* 参数:
* qid-预约编号
*
* 返回值-操作结果:
* 1-操作结果
* 0-抛出一般异常
* -1-抛出数据库异常
*/
public synchronized int cancleAppointment(int qid){
int res=0;
conn=DBConnection.getConnection();
String[] days={"SunA","SunP","MonA","MonP","TueA","TueP","WedA","WedP","ThuA","ThuP","FriA","FriP","SatA","SatP"};
int index,curpc,day,ap;
String did="";
try{
strSQL="SELECT * FROM pinqueue WHERE QID=" + qid;
stmt=conn.createStatement();
rs=stmt.executeQuery(strSQL);
if(!rs.next())
throw new SQLException("QID does not exist.");
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="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){
conn.rollback();
res= -1;
Debug.log(Debug.getExceptionMsg(sqle));
}
catch(Exception e){
res=0;
Debug.log(Debug.getExceptionMsg(e));
Debug.log(e.getMessage());
}
finally{
return res;
}
}
/**
* 查询预约信息
*
* 返回值-操作结果:
* 1-操作成功
* 0-抛出一般异常
* -1-抛出数据库异常
* -2-用户未预约
*/
public int getAppointmentInfo(){
conn=DBConnection.getConnection();
int res=0;
strSQL="SELECT * FROM pinqueue WHERE Patient=" + this.PID;
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(strSQL);
if(!rs.next())
throw new IllegalArgumentException();
res=1;
rs.beforeFirst();
result=rs;
}
catch(SQLException sqle){
res= -1;
Debug.log(Debug.getExceptionMsg(sqle));
}
catch(IllegalArgumentException iae){
res= -2;
}
catch(Exception e){
res=0;
}
finally{
return res;
}
}
/**
* 检查用户名合法性
*
* 参数:
* id-用户名
*/
protected void checkUser(String id)
throws InvalidUserException{
conn=DBConnection.getConnection();
strSQL="SELECT Username FROM patient WHERE Username='" + id + "'";
if(id.equals(Patient.ANONYMOUS))
return;
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(strSQL);
if(!rs.next())
throw new InvalidUserException();
}
catch(SQLException sqle){
Debug.log(Debug.getExceptionMsg(sqle));
}
}
/**
* 查询医生预约信息
*
* 参数:
* did-医生编号
*
* 返回值-操作结果:
* 1-操作成功
* 0-抛出一般异常
* -1-抛出数据库异常
* -2-did不存在
*/
public int getDoctorAppointmentInfo(String did){
int res=0;
Connection conn=DBConnection.getConnection();
strSQL="SELECT * FROM curappointment,appointment WHERE appointment.DID='" + did + "' AND curappointment.DID='" +
did + "'";
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(strSQL);
if(!rs.next())
throw new IllegalArgumentException("Doctor ID " + did + " does not exist.");
rs.beforeFirst();
result=rs;
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;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -