28 |

29 | <% if (way.equals("1")) {%>
30 |
There are <%=students.size() %> records who's <%=subject %> score ≥90
31 | <% } else if (way.equals("2")) { %>
32 |
There are <%=students.size() %> records who's <%=subject %> score between 80~90
33 | <% } else if (way.equals("3")) { %>
34 |
There are <%=students.size() %> records who's <%=subject %> score between 70~80
35 | <% } else if (way.equals("4")) { %>
36 |
There are <%=students.size() %> records who's <%=subject %> score between 60~70
37 | <% } else if (way.equals("5")) { %>
38 |
There are <%=students.size() %> records who's <%=subject %> score ≤60
39 | <% } %>
40 |
41 |
42 |
43 | | StudentID |
44 | Name |
45 | Class |
46 | Math |
47 | Physics |
48 | Oop |
49 | Sport |
50 |
51 |
52 | <%
53 | for (int i = 0; i < students.size(); i++) {
54 | Student student = students.get(i);
55 | %>
56 |
57 | | <%= student.getStudentID()%> |
58 | <%= student.getName()%> |
59 | <%= student.getClassID()%> |
60 | <%= student.getMathScore()%> |
61 | <%= student.getPhysicsScore()%> |
62 | <%= student.getOopScore()%> |
63 | <%= student.getSportScore()%> |
64 |
65 | <%} %>
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/src/stuPackage/AddProcess1.java:
--------------------------------------------------------------------------------
1 | package stuPackage;
2 |
3 | import java.io.IOException;
4 |
5 | import javax.servlet.ServletException;
6 | import javax.servlet.http.HttpServlet;
7 | import javax.servlet.http.HttpServletRequest;
8 | import javax.servlet.http.HttpServletResponse;
9 | import javax.servlet.RequestDispatcher;
10 |
11 | public class AddProcess1 extends HttpServlet {
12 |
13 | public void service (HttpServletRequest req, HttpServletResponse res)
14 | throws ServletException,IOException {
15 | // System.out.println("Start!");
16 | int number = 0;
17 | String classID = (String)req.getSession().getAttribute("classID"); //获取选择的班级
18 | // System.out.println("classID: " + classID);
19 |
20 |
21 |
22 | try {
23 | String num = (String)req.getSession().getAttribute("num"); //获取学生数
24 | // System.out.println("Num: " + a);
25 | number = Integer.parseInt(num);
26 | } catch (NumberFormatException e) {
27 | System.out.println(e);
28 | }
29 |
30 | // System.out.println("Number is: "+number);
31 |
32 | Student.initialize();
33 | for (int i = 0; i < number; i++) {
34 | // 转换成对应名称以获取jsp中的数据
35 | String id = "id" + i;
36 | String name1 = "name" + i;
37 | String math = "math" + i;
38 | String physics = "physics" + i;
39 | String oop = "oop" + i;
40 | String sport = "sport" + i;
41 |
42 | // 获取对应数据
43 | String studentID = req.getParameter(id);
44 | String name = req.getParameter(name1);
45 | int mathScore = Integer.parseInt(req.getParameter(math));
46 | int physicsScore = Integer.parseInt(req.getParameter(physics));
47 | int oopScore = Integer.parseInt(req.getParameter(oop));
48 | int sportScore = Integer.parseInt(req.getParameter(sport));
49 |
50 | // 创建对应实例
51 | Student student = new Student(studentID, name, classID, mathScore, physicsScore, oopScore, sportScore);
52 |
53 | // 写入数据库
54 | try {
55 | student.update();
56 | } catch (NotFoundException e) {
57 | System.out.println(e);
58 | }
59 | }
60 |
61 | Student.terminate();
62 |
63 | req.setAttribute("selectedway", "1");
64 | req.setAttribute("content", classID);
65 |
66 | String address = "/JSP/Add.jsp";
67 | RequestDispatcher dispatcher = req.getRequestDispatcher(address);
68 | dispatcher.forward(req, res);
69 |
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/WebRoot/JSP/Add1.jsp:
--------------------------------------------------------------------------------
1 | <%-- 按班级录入页面 --%>
2 | <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
3 | <%@ page import="stuPackage.*" %>
4 |
5 |
6 |
7 |
AddByClass
8 |
20 |
21 |
22 |
23 |
24 |
25 |

26 | <%
27 | String way = (String)request.getAttribute("way");
28 | String selectedClass = (String)request.getAttribute("selectedClass");
29 | ArrayList
students = (ArrayList)request.getAttribute("students");
30 | session.setAttribute("num", String.valueOf(students.size()));
31 | session.setAttribute("classID", selectedClass);
32 | %>
33 | 您选中的班级是: <%=selectedClass %>
34 | 数据库中一共有<%=students.size() %> 个记录
35 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/WebRoot/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 | Judge the way and display jsp
8 | AddController1
9 | AddController1
10 | stuPackage.AddController1
11 |
12 |
13 | process data from class add/change
14 | AddProcess1
15 | AddProcess1
16 | stuPackage.AddProcess1
17 |
18 |
19 | process data from course add/change
20 | AddProcess2
21 | AddProcess2
22 | stuPackage.AddProcess2
23 |
24 |
25 | receive inquire choice and get data
26 | InquireProcess1
27 | InquireProcess1
28 | stuPackage.InquireProcess1
29 |
30 |
31 | receive filter condition and transmit data
32 | FilterProcess1
33 | FilterProcess1
34 | stuPackage.FilterProcess1
35 |
36 |
37 | count and analyse record
38 | CountProcess
39 | CountProcess
40 | stuPackage.CountProcess
41 |
42 |
43 |
44 |
45 |
46 | AddController1
47 | /AddController1
48 |
49 |
50 | AddProcess1
51 | /AddProcess1
52 |
53 |
54 | AddProcess2
55 | /AddProcess2
56 |
57 |
58 | InquireProcess1
59 | /InquireProcess1
60 |
61 |
62 | FilterProcess1
63 | /FilterProcess1
64 |
65 |
66 | CountProcess
67 | /CountProcess
68 |
69 |
70 |
71 |
72 | index.jsp
73 |
74 |
75 |
--------------------------------------------------------------------------------
/src/stuPackage/Student.java:
--------------------------------------------------------------------------------
1 | package stuPackage;
2 | import java.util.*;
3 | public class Student
4 | {
5 | // Student的对象属性(信息+分数)
6 | private String studentID;
7 | private String name;
8 | private String classID;
9 | private int mathScore;
10 | private int physicsScore;
11 | private int oopScore;
12 | private int sportScore;
13 |
14 | // 构造方法
15 | public Student(String studentID,String name,String classID,
16 | int mathScore, int physicsScore, int oopScore, int sportScore)
17 | {
18 | setStudentID(studentID);
19 | setName(name);
20 | setClassID(classID);
21 | setMathScore(mathScore);
22 | setPhysicsScore(physicsScore);
23 | setOopScore(oopScore);
24 | setSportScore(sportScore);
25 | }
26 |
27 | // getters,setters方法
28 | public String getStudentID() {
29 | return studentID;
30 | }
31 | public void setStudentID(String studentID) {
32 | this.studentID = studentID;
33 | }
34 | public String getName() {
35 | return name;
36 | }
37 | public void setName(String name) {
38 | this.name = name;
39 | }
40 | public String getClassID() {
41 | return classID;
42 | }
43 | public void setClassID(String classID) {
44 | this.classID = classID;
45 | }
46 | public int getMathScore() {
47 | return mathScore;
48 | }
49 | public void setMathScore(int mathScore) {
50 | this.mathScore = mathScore;
51 | }
52 | public int getPhysicsScore() {
53 | return physicsScore;
54 | }
55 | public void setPhysicsScore(int physicsScore) {
56 | this.physicsScore = physicsScore;
57 | }
58 | public int getOopScore() {
59 | return oopScore;
60 | }
61 | public void setOopScore(int oopScore) {
62 | this.oopScore = oopScore;
63 | }
64 | public int getSportScore() {
65 | return sportScore;
66 | }
67 | public void setSportScore(int sportScore) {
68 | this.sportScore = sportScore;
69 | }
70 | // 为获取和设置特定分数数据而设计的方法
71 | public int getScore(String subject) {
72 | if (subject.equals("Math"))
73 | return mathScore;
74 | else if (subject.equals("Physics"))
75 | return physicsScore;
76 | else if (subject.equals("Oop"))
77 | return oopScore;
78 | else if (subject.equals("Sport"))
79 | return sportScore;
80 | return 0;
81 | }
82 | public void setScore(String subject, int score) {
83 | if (subject.equals("Math"))
84 | setMathScore(score);
85 | else if (subject.equals("Physics"))
86 | setPhysicsScore(score);
87 | else if (subject.equals("Oop"))
88 | setOopScore(score);
89 | else if (subject.equals("Sport"))
90 | setSportScore(score);
91 | }
92 |
93 | // 获取学生信息
94 | public String getDetial() {
95 | String info = getStudentID()+" "+getName()+"\t"+getClassID()+" "+
96 | getMathScore()+" "+getPhysicsScore()+" "+getOopScore()+
97 | " "+getSportScore();
98 | return info;
99 | }
100 |
101 | // 数据库静态方法
102 | public static void initialize()
103 | {
104 | StudentDA.initialize();
105 | }
106 | public static void terminate()
107 | {
108 | StudentDA.terminate();
109 | }
110 | public static Student find(String key) throws NotFoundException {
111 | return StudentDA.find(key);
112 | }
113 | public static ArrayList find(String key,String type) throws NotFoundException
114 | {
115 | return StudentDA.find(key,type);
116 | }
117 |
118 | // 数据库实例方法
119 | public void add() throws DuplicateException
120 | {
121 | StudentDA.add(this);
122 | }
123 | public void delete() throws NotFoundException
124 | {
125 | StudentDA.delete(this);
126 | }
127 | public void update() throws NotFoundException
128 | {
129 | StudentDA.update(this);
130 | }
131 | public static ArrayList getspecific(int botton,int top,String project) throws NotFoundException
132 | {
133 | return StudentDA.getspecific(botton,top,project);
134 | }
135 |
136 | }
137 |
--------------------------------------------------------------------------------
/src/stuPackage/StudentDA.java:
--------------------------------------------------------------------------------
1 | package stuPackage;
2 | import java.sql.*;
3 | import java.util.*;
4 |
5 | public class StudentDA
6 | {
7 | // Student实例
8 | static Student astudent;
9 |
10 | // 数据库连接使用的对象
11 | static String url="jdbc:odbc:StudentDB1";
12 | static Connection aConnection;
13 | static Statement aStatement;
14 |
15 | // 用于暂存数据
16 | static String stuID;
17 | static String name;
18 | static String classID;
19 | static int mathScore;
20 | static int physicsScore;
21 | static int oopScore;
22 | static int sportScore;
23 |
24 | // 连接数据库
25 | public static Connection initialize()
26 | {
27 | try
28 | {
29 | // 加载程序
30 | Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
31 |
32 | // 创建连接
33 | aConnection=DriverManager.getConnection(url,"","");
34 | aStatement=aConnection.createStatement();
35 | }
36 | catch(ClassNotFoundException e)
37 | {
38 | System.out.println(e);
39 | }
40 | catch(SQLException e)
41 | {
42 | System.out.println(e);
43 | }
44 | return aConnection;
45 | }
46 |
47 |
48 | // 与数据库断开连接
49 | public static void terminate()
50 | {
51 | try
52 | {
53 | aStatement.close();
54 | aConnection.close();
55 | }
56 | catch(SQLException e)
57 | {
58 | System.out.println(e);
59 | }
60 | }
61 |
62 | // 通过StudentID(主键)查找学生记录,返回一个Student实例
63 | public static Student find(String key) throws NotFoundException {
64 | astudent = null;
65 | String sql = "SELECT studentID,name,classID,mathScore,physicsScore,oopScore,sportScore FROM UserT"+
66 | " WHERE studentID='"+key+"'";
67 |
68 | try {
69 | ResultSet rs=aStatement.executeQuery(sql);
70 | if(rs.next())
71 | {
72 | // 获取属性
73 | stuID=rs.getString("studentID");
74 | name=rs.getString("name");
75 | classID=rs.getString("classID");
76 | mathScore=rs.getInt("mathScore");
77 | physicsScore=rs.getInt("physicsScore");
78 | oopScore=rs.getInt("oopScore");
79 | sportScore=rs.getInt("sportScore");
80 |
81 | // 创建实例
82 | astudent=new Student(stuID,name,classID,mathScore,physicsScore,oopScore,sportScore);
83 | }
84 | } catch (SQLException e) {
85 | e.printStackTrace();
86 | }
87 |
88 | return astudent; //返回
89 | }
90 |
91 | // find函数:在找不到相关记录时会返回一个异常
92 | // type为查找类型,有三种选择"findName","findClassID","all"分别对应3种查询方式
93 | // 返回的数据放在一个ArrayList里面
94 | public static ArrayList find(String key,String type) throws NotFoundException
95 | {
96 | astudent=null;
97 | ArrayList
students=new ArrayList ();
98 | String sql="";
99 |
100 | // 判断查找类型
101 | if(type.equals("findName"))
102 | {
103 | sql="SELECT studentID,name,classID,mathScore,physicsScore,oopScore,sportScore FROM UserT"+
104 | " WHERE name='"+key+"'"+
105 | " ORDER BY studentID";
106 | }
107 | if(type.equals("findClassID"))
108 | {
109 | sql="SELECT studentID,name,classID,mathScore,physicsScore,oopScore,sportScore FROM UserT"+
110 | " WHERE classID='"+key+"'"+
111 | " ORDER BY studentID";
112 | }
113 | if(type.equals("all"))
114 | {
115 | sql="SELECT studentID,name,classID,mathScore,physicsScore,oopScore,sportScore FROM UserT"+
116 | " ORDER BY studentID";
117 | }
118 |
119 | try
120 | {
121 | ResultSet rs=aStatement.executeQuery(sql);
122 |
123 | // 获取查找到的学生记录并添加到ArrayList中
124 | if(rs.next())
125 | {
126 | stuID=rs.getString("studentID");
127 | name=rs.getString("name");
128 | classID=rs.getString("classID");
129 | mathScore=rs.getInt("mathScore");
130 | physicsScore=rs.getInt("physicsScore");
131 | oopScore=rs.getInt("oopScore");
132 | sportScore=rs.getInt("sportScore");
133 |
134 | astudent=new Student(stuID,name,classID,mathScore,physicsScore,oopScore,sportScore);
135 | students.add(astudent);
136 | }
137 | else
138 | {
139 | throw(new NotFoundException("There is no such record!"));
140 | }
141 | while(rs.next())
142 | {
143 | stuID=rs.getString("studentID");
144 | name=rs.getString("name");
145 | classID=rs.getString("classID");
146 | mathScore=rs.getInt("mathScore");
147 | physicsScore=rs.getInt("physicsScore");
148 | oopScore=rs.getInt("oopScore");
149 | sportScore=rs.getInt("sportScore");
150 |
151 | astudent=new Student(stuID,name,classID,mathScore,physicsScore,oopScore,sportScore);
152 | students.add(astudent);
153 | }
154 |
155 | rs.close();
156 | }
157 | catch(SQLException e)
158 | {
159 | System.out.println(e);
160 | }
161 |
162 | // 返回Arraylist
163 | return students;
164 | }
165 |
166 |
167 | // add函数:录入功能,当数据表中存在相关数据则返回一个异常
168 | public static void add(Student astudent) throws DuplicateException
169 | {
170 | Student student;
171 |
172 | // 获取学生属性
173 | stuID=astudent.getStudentID();
174 | name=astudent.getName();
175 | classID=astudent.getClassID();
176 | mathScore=astudent.getMathScore();
177 | physicsScore=astudent.getPhysicsScore();
178 | oopScore=astudent.getOopScore();
179 | sportScore=astudent.getSportScore();
180 |
181 | String sql="INSERT INTO UserT (studentID,name,classID,mathScore,physicsScore,oopScore,sportScore) "+
182 | "VALUES ('"+stuID+"','"+name+"','"+classID+"','"+mathScore+"','"+physicsScore+"','"+oopScore+"','"
183 | +sportScore+"')";
184 |
185 | //System.out.println(sql);
186 |
187 | // 若数据库中已有此记录,返回异常;没有就录入数据库
188 | try
189 | {
190 | student=find(stuID);
191 | throw(new DuplicateException("This record is already exits!"));
192 | }
193 | catch(NotFoundException e)
194 | {
195 | try
196 | {
197 | int result=aStatement.executeUpdate(sql);
198 | }
199 | catch(SQLException ee)
200 | {
201 | System.out.println(ee);
202 | }
203 | }
204 | }
205 |
206 |
207 | // delete函数:删除功能,当找不到相关数据时会返回一个异常
208 | public static void delete(Student astudent) throws NotFoundException
209 | {
210 | stuID=astudent.getStudentID();
211 | Student student;
212 | String sql="DELETE FROM UserT "+"WHERE studentID='"+stuID+"'";
213 |
214 | // 查找有无此记录,没有就返回异常。若找到就执行删除操作
215 | try
216 | {
217 | try
218 | {
219 | student=find(stuID);
220 | int result=aStatement.executeUpdate(sql);
221 | }
222 | catch(NotFoundException e)
223 | {
224 | throw(new NotFoundException("Cannot Found the Record While deleting!"));
225 | }
226 |
227 | }
228 | catch(SQLException e)
229 | {
230 | System.out.println(e);
231 | }
232 | }
233 |
234 |
235 | // update函数:更新学生信息,当找不到相关记录时,会返回一个异常
236 | public static void update(Student astudent) throws NotFoundException
237 | {
238 | Student student;
239 |
240 | // 获取属性
241 | stuID=astudent.getStudentID();
242 | name=astudent.getName();
243 | classID=astudent.getClassID();
244 | mathScore=astudent.getMathScore();
245 | physicsScore=astudent.getPhysicsScore();
246 | oopScore=astudent.getOopScore();
247 | sportScore=astudent.getSportScore();
248 |
249 | String sql="UPDATE UserT SET studentID='"+stuID+"',name='"+name+"',classID='"+classID+"',mathScore='"+mathScore
250 | +"',physicsScore='"+physicsScore+"',oopScore='"+oopScore+"',sportScore='"+sportScore
251 | +"' WHERE studentID='"+stuID+"'";
252 | // System.out.println(sql);
253 |
254 | // 若找到学生记录就更新数据
255 | try
256 | {
257 | try
258 | {
259 | student=find(stuID);
260 | int result=aStatement.executeUpdate(sql);
261 | }
262 | catch(NotFoundException e)
263 | {
264 | throw (new NotFoundException("Cannot Found the Record while updating!"));
265 | }
266 |
267 | }
268 | catch(SQLException e)
269 | {
270 | System.out.println("\nupdate"+e);
271 | }
272 | }
273 |
274 | // 获取指定科目指定分数段的学生数据,以ArrayList返回,并从高到低排列
275 | // 挑选出 botton <= score < top
276 | // project可以传入“math”,“physics”,“oop”,“sport”
277 | public static ArrayList getspecific(int botton,int top,String project) throws NotFoundException
278 | {
279 | astudent=null;
280 | ArrayList students=new ArrayList ();
281 | String sql="";
282 |
283 | // 判断科目,匹配对应的sql语句
284 | if(project.equals("math"))
285 | {
286 | sql="SELECT studentID,name,classID,mathScore,physicsScore,oopScore,sportScore FROM UserT"+
287 | " WHERE mathScore>="+botton+" AND mathScore<"+top+
288 | " ORDER BY mathScore DESC";
289 | }
290 | if(project.equals("physics"))
291 | {
292 | sql="SELECT studentID,name,classID,mathScore,physicsScore,oopScore,sportScore FROM UserT"+
293 | " WHERE physicsScore>="+botton+" AND physicsScore<"+top+
294 | " ORDER BY physicsScore DESC";
295 | }
296 | if(project.equals("oop"))
297 | {
298 | sql="SELECT studentID,name,classID,mathScore,physicsScore,oopScore,sportScore FROM UserT"+
299 | " WHERE oopScore>="+botton+" AND oopScore<"+top+
300 | " ORDER BY oopScore DESC";
301 | }
302 | if(project.equals("sport"))
303 | {
304 | sql="SELECT studentID,name,classID,mathScore,physicsScore,oopScore,sportScore FROM UserT"+
305 | " WHERE sportScore>="+botton+" AND sportScore<"+top+
306 | " ORDER BY sportScore DESC";
307 | }
308 |
309 | // 执行数据库操作并将结果放入ArrayList
310 | try
311 | {
312 | ResultSet rs=aStatement.executeQuery(sql);
313 |
314 | if(rs.next())
315 | {
316 | stuID=rs.getString("studentID");
317 | name=rs.getString("name");
318 | classID=rs.getString("classID");
319 | mathScore=rs.getInt("mathScore");
320 | physicsScore=rs.getInt("physicsScore");
321 | oopScore=rs.getInt("oopScore");
322 | sportScore=rs.getInt("sportScore");
323 |
324 | astudent=new Student(stuID,name,classID,mathScore,physicsScore,oopScore,sportScore);
325 | students.add(astudent);
326 | }
327 | else
328 | {
329 | throw(new NotFoundException("There is no such record!"));
330 | }
331 |
332 | while(rs.next())
333 | {
334 | stuID=rs.getString("studentID");
335 | name=rs.getString("name");
336 | classID=rs.getString("classID");
337 | mathScore=rs.getInt("mathScore");
338 | physicsScore=rs.getInt("physicsScore");
339 | oopScore=rs.getInt("oopScore");
340 | sportScore=rs.getInt("sportScore");
341 |
342 | astudent=new Student(stuID,name,classID,mathScore,physicsScore,oopScore,sportScore);
343 | students.add(astudent);
344 | }
345 |
346 | rs.close();
347 | }
348 | catch(SQLException e)
349 | {
350 | System.out.println(e);
351 | }
352 |
353 | // 返回ArrayList
354 | return students;
355 | }
356 | }
--------------------------------------------------------------------------------