?? 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 + -