📄 admin.java
字号:
package hospital.db.dboperation;
import java.sql.*;
import hospital.db.*;
/**
* 本类从DBOperation类继承,用于封装管理员角色
*
* 作者:Fido Dido
*/
public class Admin
extends DBOperation{
/**
* 构造器
*
* 参数:
* id-管理员用户名
*/
public Admin(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 Username,Password FROM administrator WHERE Username='" + this.id +
"' AND Password=password('" + password + "')";
stmt=conn.createStatement();
rs=stmt.executeQuery(strSQL);
if(!rs.next())
throw new IllegalAccessException("Password invalid.");
res=1;
Debug.log("Administrator '" + this.id + "' logged in.");
}
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;
}
}
/**
* 添加医生帐户
*
* 参数:
* did-医生编号
* name-姓名
* age-年龄
* sex-性别
* level-职称
* spe-特长
* pPerDay-每天最大可预约数量(周日上午、周日下午、周一上午……)
* phone-联系电话
* section-科室
*
* 返回值-执行结果代码:
* 1-操作成功
* 0-抛出一般异常
* -1-抛出数据库异常
* -2-医生帐户信息已存在
*/
public synchronized int addDoctor(String did,String name,int age,int sex,String level,String spe,
int[] pPerDay,String phone,String section){
int res=0;
conn=DBConnection.getConnection();
try{
strSQL="select DID from doctor where DID='" + did + "'";
stmt=conn.createStatement();
rs=stmt.executeQuery(strSQL);
conn.setAutoCommit(false);
if(rs.next())
throw new IllegalArgumentException("Doctor ID '" + did + "' already exists.");
stmt=conn.createStatement();
strSQL="INSERT INTO doctor (DID,Name,Age,Sex,Level,Specialism,Phone,Password,Section) VALUES ('" +
did + "','" + name + "'," + age + "," + sex + ",'" + level + "','" + spe + "','" + phone + "',password('" +
did + "'),'" + section + "')";
stmt.addBatch(strSQL);
strSQL=
"INSERT INTO appointment (DID,SunA,SunP,MonA,MonP,TueA,TueP,WedA,WedP,ThuA,ThuP,FriA,FriP,SatA,SatP) VALUES ('" +
did + "'";
for(int i=0;i < 14;i++){
strSQL+=",";
strSQL+=pPerDay[i];
}
strSQL+=")";
stmt.addBatch(strSQL);
strSQL=
"INSERT INTO curappointment (DID,SunA,SunP,MonA,MonP,TueA,TueP,WedA,WedP,ThuA,ThuP,FriA,FriP,SatA,SatP) VALUES ('" +
did + "'";
for(int i=0;i < 14;i++){
strSQL+=",";
strSQL+=pPerDay[i];
}
strSQL+=")";
stmt.addBatch(strSQL);
stmt.executeBatch();
conn.commit();
res=1;
}
catch(SQLException sqle){
Debug.log(Debug.getExceptionMsg(sqle));
conn.rollback();
res= -1;
}
catch(IllegalArgumentException iae){
Debug.log(Debug.getExceptionMsg(iae));
res= -2;
}
catch(Exception e){
res=0;
}
finally{
return res;
}
}
/**
* 获取医生帐户信息
*
* 返回值-执行结果代码:
* 1-查询成功
* 0-抛出一般异常
* -1-抛出数据库异常
* -2-医生帐户不存在
*/
public int getDoctorInfo(){
int res=0;
strSQL="SELECT DID,Name,Age,Sex,Level,Section,Specialism,Phone FROM doctor";
conn=DBConnection.getConnection();
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(strSQL);
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;
}
}
/**
* 检查用户名是否存在
*
* 参数:
* id-医生编号
*/
protected void checkUser(String id)
throws InvalidUserException{
conn=DBConnection.getConnection();
strSQL="SELECT Username FROM administrator WHERE Username='" + id + "'";
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-医生帐户不存在
*/
public synchronized int removeDoctor(String did){
int res=0;
conn=DBConnection.getConnection();
strSQL="SELECT DID FROM doctor WHERE DID='" + did + "'";
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(strSQL);
if(!rs.next())
throw new IllegalArgumentException("Doctor " + did + " does not exist.");
conn.setAutoCommit(false);
stmt=conn.createStatement();
strSQL="DELETE FROM doctor WHERE DID='" + did + "'";
stmt.addBatch(strSQL);
strSQL="DELETE FROM appointment WHERE DID='" + did + "'";
stmt.addBatch(strSQL);
strSQL="DELETE FROM curappointment WHERE DID='" + did + "'";
stmt.addBatch(strSQL);
strSQL="DELETE FROM pinqueue WHERE Doctor='" + did + "'";
stmt.addBatch(strSQL);
strSQL="DELETE FROM history WHERE Doctor='" + did + "'";
stmt.addBatch(strSQL);
stmt.executeBatch();
conn.commit();
res=1;
}
catch(IllegalArgumentException iae){
res= -2;
Debug.log(Debug.getExceptionMsg(iae));
}
catch(SQLException sqle){
res= -1;
conn.rollback();
Debug.log(Debug.getExceptionMsg(sqle));
}
catch(Exception e){
res=0;
Debug.log(Debug.getExceptionMsg(e));
}
finally{
return res;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -