软件工程概论课堂测试
1 设计思想
首先定义一个类来存放这三个课程信息。然后通过连接数据库,将用户输入的数据添加进去。这是初步阶段。完成对数据库的添加之后。之后,要判断用户输入的内容是否符合要求。加入一些判断。
2 源代码
1 package com.kao.msg.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 import com.kao.msg.Util.DBUtil; 9 import com.kao.msg.model.Kecheng; 10 11 public class Xinxi implements Ixinxi { 12 public void add(Kecheng kecheng){ 13 Connection connection = DBUtil.getConnection(); 14 String sql ; 15 PreparedStatement preparedStatement = null; 16 ResultSet resultSet = null; 17 sql = "insert into t_add(name,teacher,address) values (?,?,?)"; 18 try { 19 preparedStatement = connection.prepareStatement(sql); 20 preparedStatement.setString(1, kecheng.getName()); 21 preparedStatement.setString(2,kecheng.getTeacher()); 22 preparedStatement.setString(3, kecheng.getAddress()); 23 preparedStatement.executeUpdate(); 24 25 } catch (SQLException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } finally { 29 //关闭资源 30 DBUtil.close(resultSet); 31 DBUtil.close(preparedStatement); 32 DBUtil.close(connection); 33 } 34 35 36 37 38 } 39 40 }
1 package com.kao.msg.model; 2 3 public class Kecheng { 4 String teacher; 5 String name; 6 String address; 7 public String getTeacher() { 8 return teacher; 9 } 10 public void setTeacher(String teacher) { 11 this.teacher = teacher; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 public String getAddress() { 20 return address; 21 } 22 public void setAddress(String address) { 23 this.address = address; 24 } 25 26 27 }
1 package com.kao.msg.Util; 2 3 import java.sql.*; 4 5 public class DBUtil { 6 public static Connection getConnection(){ 7 try { 8 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 9 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 10 // TODO Auto-generated catch block 11 e.printStackTrace(); 12 } 13 String user = "sa"; 14 String password = "20163433"; 15 String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=kaoshi"; 16 Connection connection = null; 17 try { 18 //2 创建链接对象connection 19 connection = DriverManager.getConnection(url,user,password); 20 } catch (SQLException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } 24 return connection; 25 26 } 27 public static void close(Connection connection ) { 28 try { 29 if (connection != null) { 30 connection.close(); 31 } 32 33 } catch (SQLException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 } 38 public static void close(PreparedStatement preparedStatement ) { 39 try { 40 if (preparedStatement != null) { 41 preparedStatement.close(); 42 } 43 44 } catch (SQLException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 } 49 public static void close(ResultSet resultSet ) { 50 try { 51 if (resultSet != null) { 52 resultSet.close(); 53 } 54 55 } catch (SQLException e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } 59 } 60 61 62 63 } 64
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <%if(request.getAttribute("error1")!=null){ 11 out.print("<script language='javaScript'>alert('课程不能为空');</script>"); 12 } 13 if(request.getAttribute("error2")!=null){ 14 out.print("<script language='javaScript'>alert('教师不能为空');</script>"); 15 } 16 if(request.getAttribute("error3")!=null){ 17 out.print("<script language='javaScript'>alert('教室不能为空');</script>"); 18 } 19 if(request.getAttribute("error4")!=null){ 20 out.print("<script language='javaScript'>alert('教师格式错误');</script>"); 21 } 22 if(request.getAttribute("error5")!=null){ 23 out.print("<script language='javaScript'>alert('教室格式错误');</script>"); 24 } 25 %> 26 <form action="add.jsp" method="get"> 27 <table align="center" border="1" width="500"> 28 <tr> 29 <td>课程名称 : </td> 30 <td> 31 <input type="text" name="name" /> 32 </td> 33 </tr> 34 <tr> 35 <td>任课教师:</td> 36 <td> 37 <input type="text" name="teacher" /> 38 </td> 39 </tr> 40 <tr> 41 <td>上课地点:</td> 42 <td> 43 <input type="text" name="address" /> 44 </td> 45 </tr> 46 47 <tr align="center"> 48 <td colspan="2"> 49 <input type="submit" value="提交" /><br> 50 </td> 51 </tr> 52 </table> 53 </body> 54 </html>
1 package com.kao.msg.Util; 2 3 public class JisuanException extends RuntimeException{ 4 5 public JisuanException() { 6 super(); 7 // TODO Auto-generated constructor stub 8 } 9 10 public JisuanException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 11 super(message, cause, enableSuppression, writableStackTrace); 12 // TODO Auto-generated constructor stub 13 } 14 15 public JisuanException(String message, Throwable cause) { 16 super(message, cause); 17 // TODO Auto-generated constructor stub 18 } 19 20 public JisuanException(String message) { 21 super(message); 22 // TODO Auto-generated constructor stub 23 } 24 25 public JisuanException(Throwable cause) { 26 super(cause); 27 // TODO Auto-generated constructor stub 28 } 29 30 } 31
1 <%@page import="com.kao.msg.model.Kecheng"%> 2 <%@page import="com.kao.msg.dao.Xinxi"%> 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <% 13 //接收客户端传递过来的参数 14 String name = request.getParameter("name"); 15 String teacher = request.getParameter("teacher"); 16 String address = request.getParameter("address"); 17 Kecheng kecheng=new Kecheng(); 18 if(name == null || "".equals(name.trim())){ 19 request.setAttribute("error1", "课程不能为空"); 20 21 22 %> 23 <jsp:forward page="addinput.jsp"></jsp:forward> 24 <% 25 } 26 if(teacher == null || "".equals(teacher.trim())){ 27 request.setAttribute("error2", "用户名不能为空"); 28 29 30 %> 31 <jsp:forward page="addinput.jsp"></jsp:forward> 32 <% 33 } 34 if(address == null || "".equals(address.trim())){ 35 request.setAttribute("error3", "用户名不能为空"); 36 37 38 %> 39 <jsp:forward page="addinput.jsp"></jsp:forward> 40 <% 41 } 42 if(teacher.equals("王建民")||teacher.equals("刘力嘉")||teacher.equals("刘丹")||teacher.equals("王辉")||teacher.equals("杨子光")){ 43 %> 44 <h1>教师保存成功!!<h1><br> 45 <% 46 } 47 else{ 48 request.setAttribute("error4", "无此教师"); 49 %> 50 <jsp:forward page="addinput.jsp"></jsp:forward> 51 52 53 <% 54 } 55 56 if(address.startsWith("一教")||address.startsWith("二教")||address.startsWith("三教")||address.startsWith("基教")){ 57 kecheng.setName(name); 58 kecheng.setTeacher(teacher); 59 kecheng.setAddress(address); 60 Xinxi xinxi=new Xinxi(); 61 xinxi.add(kecheng); 62 63 64 %> 65 <h1>教室保存成功!!<h1><br> 66 67 68 <% 69 } 70 else{ 71 request.setAttribute("error5", "教室格式错误"); 72 %> 73 <jsp:forward page="addinput.jsp"></jsp:forward> 74 <% 75 } 76 77 %> 78 </body> 79 </html> 80 81 add
3 运行结果截图
项目计划总计
日期 任务 |
听课 |
编写程序 |
日总结 |
周二 |
|
|
|
上午 |
50 |
50 |
|
下午 |
|
30 |
130 |
时间记录日志
日期 |
开始时间 |
中断时间 |
净时间 |
活动 |
备注 |
11/28 |
|
|
|
|
|
上午 |
8:00 |
8:50 |
50 |
听课 |
Psp讲解 |
|
9:00 |
9:50 |
50 |
编程序 |
课堂测试 |
下午 |
1:50 |
2:15 |
25 |
编程序 |
完善课堂测试内容 |
缺陷记录日志
日期 |
编号 |
类型 |
引入阶段 |
排除阶段 |
修复时间 |
修复缺陷 |
11/28 |
1 |
|
|
|
3min |
|
|
描述:忘记导入数据库的jar包,导致报错。 |
|||||
|
2 |
|
|
|
2min |
|
|
描述:数据库存入的内容为空。原因是在赋值阶段应该使用set方法,误用get方法导致赋值失败,将空值存入了数据库 |
|||||
|
3 |
|
|
|
25min |
|
|
描述:用户输入界面不能输入空值,而且输入不符合要求的信息所提示的错误不明确。然后我在jsp中添加了一些if判断,使程序更加健壮。 |