📄 teachsvlt.java
字号:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import myBean.Teach;
public class TeachSvlt extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String class_id =req.getParameter("class_id");
int success = 0;
String action = req.getParameter("action");
Teach tea = null;
String message="",tea_id,cour_id,cour_time,room_id;
if ("new".equalsIgnoreCase(action)) {
room_id=req.getParameter("room_id");
tea_id=req.getParameter("tea_id");
cour_id=req.getParameter("cour_id");
cour_time=req.getParameter("cour_time");
if(isTrue(req,res,class_id,cour_time) && hasMoreclass(tea_id,cour_id,cour_time,room_id,req,res)
&& hasMoreteacher(tea_id,cour_time,room_id,req,res)&&hasMoretea(req,res) ){
tea = doNew(req,res,class_id,tea_id,cour_id,cour_time);
sendBean(req, res, tea,"/getTeach.jsp");
}
}
if ("update".equalsIgnoreCase(action)) {
room_id=req.getParameter("room_id");
tea_id=req.getParameter("tea_id");
cour_id=req.getParameter("cour_id");
cour_time=req.getParameter("cour_time");
if(isTrue(req,res,class_id,cour_time) && hasMoreclass(tea_id,cour_id,cour_time,room_id,req,res)
&& hasMoreteacher(tea_id,cour_time,room_id,req,res)&&hasMoretea(req,res)){
try{
tea = doUpdate(req,res,class_id,tea_id,cour_id,cour_time);
sendBean(req,res,tea,"/getTeach.jsp");
}catch(SQLException e){}
}
}
if ("delete".equalsIgnoreCase(action)) {
cour_id=req.getParameter("cour_id");
cour_time=req.getParameter("cour_time");
try{
success = doDelete(class_id,cour_id,cour_time);
}
catch(SQLException e){}
if (success < 1) {
doError(req, res, "TeachSvlt: Delete unsuccessful. Rows affected: " + success);
} else {
res.sendRedirect("http://localhost:8080/0903/getTeach.jsp");
}
}
}
public Teach doNew(HttpServletRequest req,HttpServletResponse res,String class_id,String tea_id,String cour_id,String cour_time)
throws ServletException,IOException{
Teach tea= new Teach();
String room_id=req.getParameter("room_id");
tea.setClass_Id(class_id);
tea.setTea_id(tea_id);
tea.setCour_id(cour_id);
tea.setRoom_id(room_id);
tea.setCour_time(cour_time);
tea.addTeach();
return tea;
}
private boolean hasMoretea(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
String class_id = req.getParameter("class_id");
String cour_id = req.getParameter("cour_id");
String tea_id = req.getParameter("tea_id");
boolean f = true;
Teach tea = new Teach();
String temp = tea.hasMoretea(class_id,cour_id);
if(temp.equals("no")||temp.equals(tea_id))
f = true;
else{
f = false;
temp = "对不起,该班级("+class_id+")的该课程("+cour_id+")已经有老师("+tea_id+")";
doError(req,res,temp);
}
return f;
}
private boolean hasMoreteacher(String tea_id,String cour_time,String room_id,HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
String temp = "";
boolean f = true;
Teach tea=new Teach();
temp=tea.hasMoreteacher(cour_time,room_id);
if(temp.equals("no")||temp.equals(tea_id))
f=true;
else{
f=false;
temp="对不起,该教室("+room_id+")在该时间已经安排有课"+temp;
doError(req,res,temp);
}
return f;
}
public boolean hasMoreclass(String tea_id,String cour_id,String cour_time,String room_id,HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
boolean f=true;
String temp="";
String message="";
Teach tea=new Teach();
ResultSet rs=tea.hasMorecourse(tea_id,cour_time);
try{
if(rs.next()){
temp = rs.getString("COUR_ID");
if(temp.equals(cour_id)&&rs.getString("ROOM").equals(room_id))
f=true;
else {
f=false;
message="对不起,该教师("+tea_id+")在"+cour_time+"时间已经安排有课"+temp;
doError(req,res,message);
}
}
}catch(SQLException e){ System.out.print(e.getMessage());}
return f;
}
public boolean hasChange(String tea_id,String cour_time,
HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
boolean f=false;
String tea_id0=req.getParameter("tea_id0");
String cour_time0=req.getParameter("cour_time0");
if(!tea_id.equals(tea_id0) || !cour_time.equals(cour_time0) )
f=true;
return f;
}
public Teach doUpdate(HttpServletRequest req,HttpServletResponse res,String id,String tea_id,String cour_id,String cour_time)
throws ServletException,IOException,SQLException {
Teach tea = new Teach();
String room_id=req.getParameter("room_id");
if(hasChange(tea_id,cour_time,req,res)){
tea.updateTeach(id,tea_id,cour_id,room_id,cour_time);
}else{
tea.updateTeach(id,cour_id,room_id);
}
return tea;
}
public int doDelete(String id,String cour_id,String cour_time) throws SQLException {
int num=0;
Teach tea=new Teach();
num=tea.deleteTeach(id,cour_id,cour_time);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
Teach tea, String target)
throws ServletException, IOException {
req.setAttribute("tea", tea);
RequestDispatcher rd = getServletContext().getRequestDispatcher(target);
rd.forward(req, res);
}
public void doError(HttpServletRequest req,
HttpServletResponse res,
String str)
throws ServletException, IOException {
req.setAttribute("problem", str);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/errorpage.jsp");
rd.forward(req, res);
}
public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
String id,String cour_time)
throws ServletException, IOException {
Teach tea = new Teach();
boolean f=true;
String message ="";
if(id==null || id.equals("")) {
f=false;
message="错误,班级号不能为空!";
doError(req,res,message);
}
else if(tea.hasLogin(id,cour_time)){
f=false;
message="对不起,记录重复了!";
doError(req,res,message);
}
return f;
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -