软工概论课堂测试一添加新课程

  1. 设计思想

(1)创建新的动态网站项目后,先在WebContent->WEB-INF->lib文件夹里导入jstl-1.2.jar和mysql-connector-java-5.1.38-bin.jar两个jar包。

(2)写课程类加入基本的set(),get()函数,写课程接口类,课程实现类实现课程接口类(实现增删改查)。

(3)在数据库中创建新表。

(4)编写数据库操作类。

    连接数据库6步:

  1.加载驱动 2.创建链接对象 3.创建语句传输对象 4.接收结果集对象(主要针对查询) 5.遍历 6.关闭资源

(5)在WebContent目录下新建一个文件夹,在里面编写增加课程的操作文件addInput.jsp和add.jsp。

 在课程添加界面输入要添加的课程名称、任课教师、上课地点,然后接收客户端传递过来的参数用set()方法分别设置lesson属性;

先判断输入的任课教师是否与规定教师相匹配,如不匹配,设置错误信息的参数,页面跳转回增加课程界面,并输出错误信息;

再判断输入的上课地点是否以规定的上课地点开头,如果不符合,同上;

若前两条都通过,调用add方法,该方法里加入查重验证,若输入的课程名称已经存在,则会设置一个错误信息,页面跳转回增加课程界面,输出错误信息;

若前三条都通过,那么可以成功添加新课程,add方法会将输入数据传入数据库保存,并且页面跳转,显示已成功添加课程,并可选择继续添加,或查看课程列表。

  2. 源程序代码

Lesson.java

 1 package com.jaovo.msg.model;
 2 
 3 public class Lesson {
 4     private int id;
 5     private String lessonname;
 6     private String lessonteacher;
 7     private String lessonplace;
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getLessonname() {
15         return lessonname;
16     }
17     public void setLessonname(String lessonname) {
18         this.lessonname = lessonname;
19     }
20     public String getLessonteacher() {
21         return lessonteacher;
22     }
23     public void setLessonteacher(String lessonteacher) {
24         this.lessonteacher = lessonteacher;
25     }
26     public String getLessonplace() {
27         return lessonplace;
28     }
29     public void setLessonplace(String lessonplace) {
30         this.lessonplace = lessonplace;
31     }
32 }

ILessonDao.java

 1 package com.jaovo.msg.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.jaovo.msg.model.Lesson;
 6 
 7 public interface ILessonDao {
 8     public void add(Lesson lesson);
 9     public void delete(int id);
10     public void update(Lesson lesson);
11     public Lesson load(int id);
12     public Lesson load(String lessonname);
13     public List<Lesson> load();
14 }

LessonDaoImpl.java

  1 package com.jaovo.msg.dao;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 import com.jaovo.msg.Util.DBUtil;
 11 import com.jaovo.msg.Util.LessonException;
 12 import com.jaovo.msg.model.Lesson;
 13 
 14 import sun.net.www.content.text.plain;
 15 
 16 public class LessonDaoImpl implements ILessonDao{
 17 
 18     @Override
 19     public void add(Lesson lesson) {
 20         //获得链接对象
 21         Connection connection = DBUtil.getConnection();
 22         //准备sql语句
 23         String sql="select count(*) from t_lesson where lessonname = ?";
 24         //创建语句传输对象
 25         PreparedStatement preparedStatement=null;
 26         ResultSet resultSet=null;
 27         try {
 28             preparedStatement=connection.prepareStatement(sql);
 29             preparedStatement.setString(1, lesson.getLessonname());
 30             //接收结果集
 31             resultSet=preparedStatement.executeQuery();
 32             //遍历结果集
 33             while(resultSet.next())
 34             {
 35                 if(resultSet.getInt(1)>0)//>0说明数据库中已存在该用户
 36                 {
 37                     throw new LessonException("课程已存在");
 38                 }
 39             }
 40             sql="insert into t_lesson(lessonname,lessonteacher,lessonplace) value (?,?,?)";
 41             preparedStatement=connection.prepareStatement(sql);
 42             preparedStatement.setString(1, lesson.getLessonname());
 43             preparedStatement.setString(2, lesson.getLessonteacher());
 44             preparedStatement.setString(3, lesson.getLessonplace());
 45             preparedStatement.executeUpdate();//更改
 46         } catch (SQLException e) {
 47             // TODO Auto-generated catch block
 48             e.printStackTrace();
 49         }finally {
 50             DBUtil.close(resultSet);
 51             DBUtil.close(preparedStatement);
 52             DBUtil.close(connection);
 53         }
 54         
 55     }
 56 
 57     @Override
 58     public void delete(int id) {
 59         Connection connection=DBUtil.getConnection();
 60         String sql="delete from t_lesson where id = ?";
 61         PreparedStatement preparedStatement=null;
 62         try {
 63             preparedStatement=connection.prepareStatement(sql);
 64             preparedStatement.setInt(1,id);
 65             preparedStatement.executeUpdate();
 66         } catch (SQLException e) {
 67             // TODO Auto-generated catch block
 68             e.printStackTrace();
 69         }finally {
 70             DBUtil.close(preparedStatement);
 71             DBUtil.close(connection);
 72         }
 73         
 74     }
 75 
 76     @Override
 77     public void update(Lesson lesson) {
 78         //获得链接对象
 79         Connection connection = DBUtil.getConnection();
 80         //准备sql语句
 81         String sql="update t_lesson set lessonname = ? , lessonteacher = ? , set lessonplace = ? , where id = ?";
 82         //创建语句传输对象
 83         PreparedStatement preparedStatement=null;
 84         try {
 85             preparedStatement=connection.prepareStatement(sql);
 86             preparedStatement.setString(1,lesson.getLessonname());
 87             preparedStatement.setString(2, lesson.getLessonteacher());
 88             preparedStatement.setString(3, lesson.getLessonplace());
 89             preparedStatement.setInt(4, lesson.getId());
 90             preparedStatement.executeUpdate();
 91         } catch (SQLException e) {
 92             // TODO Auto-generated catch block
 93             e.printStackTrace();
 94         }finally {
 95             DBUtil.close(preparedStatement);
 96             DBUtil.close(connection);
 97         }
 98     
 99     }
100 
101     @Override
102     public Lesson load(int id) {
103         //获得链接对象
104         Connection connection = DBUtil.getConnection();
105         //准备sql语句
106         String sql="select * from t_lesson where id = ?";
107         //创建语句传输对象
108         PreparedStatement preparedStatement=null;    
109         ResultSet resultSet=null;
110         Lesson lesson=null;
111         try {
112             preparedStatement=connection.prepareStatement(sql);
113             preparedStatement.setInt(1,id);
114             resultSet=preparedStatement.executeQuery();
115             while(resultSet.next()) {
116                 lesson=new Lesson();
117                 lesson.setId(id);
118                 lesson.setLessonname(resultSet.getString("lessonname"));
119                 lesson.setLessonteacher(resultSet.getString("lessonteacher"));
120                 lesson.setLessonplace(resultSet.getString("lessonplace"));
121 
122             }
123         } catch (SQLException e) {
124             // TODO Auto-generated catch block
125             e.printStackTrace();
126         }finally {
127             DBUtil.close(resultSet);
128             DBUtil.close(preparedStatement);
129             DBUtil.close(connection);
130         }
131         
132         return lesson;
133     }
134 
135     @Override
136     public Lesson load(String lessonname) {
137         //获得链接对象
138         Connection connection = DBUtil.getConnection();
139         //准备sql语句
140         String sql="select * from t_lesson where lessonname = ?";
141         //创建语句传输对象
142         PreparedStatement preparedStatement=null;    
143         ResultSet resultSet=null;
144         Lesson lesson=null;
145         try {
146             preparedStatement=connection.prepareStatement(sql);
147             preparedStatement.setString(1,lessonname);
148             resultSet=preparedStatement.executeQuery();
149             while(resultSet.next()) {
150                 lesson=new Lesson();
151                 lesson.setId(resultSet.getInt("id"));
152                 lesson.setLessonname(lessonname);
153                 lesson.setLessonteacher(resultSet.getString("lessonteacher"));
154                 lesson.setLessonplace(resultSet.getString("lessonplace"));
155 
156             }
157         } catch (SQLException e) {
158             // TODO Auto-generated catch block
159             e.printStackTrace();
160         }finally {
161             DBUtil.close(resultSet);
162             DBUtil.close(preparedStatement);
163             DBUtil.close(connection);
164                 }
165                 
166                 return lesson;
167     }
168 
169     @Override
170     public List<Lesson> load() {
171         //获得链接对象
172         Connection connection = DBUtil.getConnection();
173         //准备sql语句
174         String sql="select * from t_lesson";
175         //创建语句传输对象
176         PreparedStatement preparedStatement=null;    
177         ResultSet resultSet=null;
178         //集合中只能放入user对象
179         List<Lesson>lessons=new ArrayList<Lesson>();
180         Lesson lesson=null;
181         try {
182             preparedStatement=connection.prepareStatement(sql);
183             resultSet=preparedStatement.executeQuery();
184             while(resultSet.next()) {
185                 lesson=new Lesson();
186                 lesson.setId(resultSet.getInt("id"));
187                 lesson.setLessonname(resultSet.getString("lessonname"));
188                 lesson.setLessonteacher(resultSet.getString("lessonteacher"));
189                 lesson.setLessonplace(resultSet.getString("lessonplace"));
190                 lessons.add(lesson);
191             }
192         } catch (SQLException e) {
193             // TODO Auto-generated catch block
194             e.printStackTrace();
195         }finally {
196             DBUtil.close(resultSet);
197             DBUtil.close(preparedStatement);
198             DBUtil.close(connection);
199         }
200                 
201         return lessons;
202     }
203 }

DBUtil.java

 1 package com.jaovo.msg.Util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 public class DBUtil {
10     
11     public  static  Connection getConnection() {
12         try {
13             //1 加载驱动
14             Class.forName("com.mysql.jdbc.Driver").newInstance();
15         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
16             // TODO Auto-generated catch block
17             e.printStackTrace();
18         }
19         String user = "root";
20         String password = "root";
21         String url = "jdbc:mysql://localhost:3306/test";
22         Connection connection = null;
23         try {
24             //2 创建链接对象connection
25              connection = DriverManager.getConnection(url,user,password);
26         } catch (SQLException e) {
27             // TODO Auto-generated catch block
28             e.printStackTrace();
29         }
30         return connection;
31     }
32     
33     //关闭资源的方法
34     public static void close(Connection connection ) {
35         try {
36             if (connection != null) {
37                 connection.close();
38             }
39             
40         } catch (SQLException e) {
41             // TODO Auto-generated catch block
42             e.printStackTrace();
43         }
44     }
45     public static void close(PreparedStatement preparedStatement ) {
46         try {
47             if (preparedStatement != null) {
48                 preparedStatement.close();
49             }
50             
51         } catch (SQLException e) {
52             // TODO Auto-generated catch block
53             e.printStackTrace();
54         }
55     }
56     public static void close(ResultSet resultSet ) {
57         try {
58             if (resultSet != null) {
59                 resultSet.close();
60             }
61             
62         } catch (SQLException e) {
63             // TODO Auto-generated catch block
64             e.printStackTrace();
65         }
66     }
67     
68 }

LessonException.java

 1 package com.jaovo.msg.Util;
 2 
 3 public class LessonException extends RuntimeException{
 4 
 5     public LessonException() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9 
10     public LessonException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
11         super(message, cause, enableSuppression, writableStackTrace);
12         // TODO Auto-generated constructor stub
13     }
14 
15     public LessonException(String message, Throwable cause) {
16         super(message, cause);
17         // TODO Auto-generated constructor stub
18     }
19 
20     public LessonException(String message) {
21         super(message);
22         // TODO Auto-generated constructor stub
23     }
24 
25     public LessonException(Throwable cause) {
26         super(cause);
27         // TODO Auto-generated constructor stub
28     }
29     
30 }

DaoFactory.java

 1 package com.jaovo.msg.Util;
 2 
 3 import com.jaovo.msg.dao.LessonDaoImpl;
 4 
 5 public class DaoFactory {
 6     public static LessonDaoImpl getDaoImpl()
 7     {
 8         return new LessonDaoImpl();
 9     }
10 }

ValidateUtil.java

 1 //用于验证输入信息是否为空
 2 package com.jaovo.msg.Util;
 3 
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 
 9 import com.jaovo.msg.dao.LessonDaoImpl;
10 import com.jaovo.msg.model.Lesson;
11 
12 public class ValidateUtil {
13     public static boolean validateNull(HttpServletRequest request,String[] fileds)
14     {
15         boolean validate=true;
16         //map对象用来装载不同的错误信息
17         Map<String,String> errorMsg = new HashMap();
18         for(String filed : fileds)
19         {
20             String value=request.getParameter(filed);
21             if(value==null||"".equals(value.trim()))
22             {
23                 validate=false;
24                 errorMsg.put(filed, filed+"不能为空");
25             }
26             
27             if(!validate)
28             {
29                 request.setAttribute("errorMsg", errorMsg);//设置errorMsg属性,属性值为errorMsg,即"filed不能为空"
30             }
31         }
32         return validate;
33     }
34     
35     public static String showError(HttpServletRequest request,String filed)
36     {
37         Map<String,String> errorMsg = (Map<String,String>)request.getAttribute("errorMsg");
38         if(errorMsg==null)
39         {
40             return "";
41         }
42         String msg = errorMsg.get(filed);
43         if(msg == null)
44         {
45             return "";
46         }
47         return msg;
48     }
49     
50 }

addInput.jsp

 1 <%@ page import="com.jaovo.msg.Util.ValidateUtil"%>
 2     <%@page import="java.util.Map" %>
 3 <%@ page language="java" contentType="text/html; charset=UTF-8"
 4     pageEncoding="UTF-8"%>
 5 
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <title>课程添加界面</title>
10 </head>
11 <body>
12 <%-- Map<String,String> errorMsg = (Map<String,String>)request.getAttribute("errorMsg"); --%>
13 <%
14     String lessonError = (String)request.getAttribute("lessonError");
15     if(lessonError == null){
16         lessonError = "";
17     }
18 %>
19     <%=lessonError %><br>
20     <form action="add.jsp" method="get">
21             <tr>
22                 <td>课程名称 &nbsp; &nbsp;</td>
23                 <td>
24                     <input type="text" name="lessonname" />
25                     <%=ValidateUtil.showError(request,"lessonname") %>
26                 </td>
27             </tr><br><br>
28             <tr>
29                 <td>任课教师 &nbsp; &nbsp;</td>
30                 <td>
31                     <input type="text" name="lessonteacher"/>
32                     <%=ValidateUtil.showError(request,"lessonteacher") %>
33                 </td>
34             </tr><br><br>
35             <tr>
36                 <td>上课地点 &nbsp; &nbsp;</td>
37                 <td>
38                     <input type="text" name="lessonplace" />
39                     <%=ValidateUtil.showError(request,"lessonplace") %>
40                 </td>
41             </tr><br><br>
42             <tr align="center">
43                 <td colspan="2">
44                     <input type="submit" value="保存" />
45                     <input type="reset" name="重置" />
46                 </td>
47             </tr>    
48     </form>
49 </body>
50 </html>

add.jsp

 1 <%@page import="com.jaovo.msg.Util.LessonException"%>
 2 <%@page import="com.jaovo.msg.dao.LessonDaoImpl" %>
 3 <%@page import="com.jaovo.msg.model.Lesson" %>
 4 <%@page import="com.jaovo.msg.Util.ValidateUtil" %>
 5 <%@ page language="java" contentType="text/html; charset=UTF-8"
 6     pageEncoding="UTF-8"%>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <%
10     //接收客户端传递过来的参数
11     String lessonname=request.getParameter("lessonname");
12     String lessonteacher=request.getParameter("lessonteacher");
13     String lessonplace=request.getParameter("lessonplace");
14     boolean validate=ValidateUtil.validateNull(request,new String[]{"lessonname","lessonteacher","lessonplace"});
15     if(!validate){
16 %>
17     <jsp:forward page="addInput.jsp"></jsp:forward>  
18 <%    }
19     Lesson lesson=new Lesson();
20     lesson.setLessonname(lessonname);
21     lesson.setLessonteacher(lessonteacher);
22     lesson.setLessonplace(lessonplace);
23     
24     LessonDaoImpl LessonDao=new LessonDaoImpl();
25 
26     try
27     {
28     if(!("王建民".equals(lessonteacher)|| "刘立嘉".equals(lessonteacher)||"刘丹".equals(lessonteacher)||"王辉".equals(lessonteacher)||"杨子光".equals(lessonteacher)))
29     {
30         request.setAttribute("lessonError","任课教师不存在");
31     %>
32     <<jsp:forward page="addInput.jsp"></jsp:forward>
33     <%
34     }
35     else if(!(lessonplace.startsWith("一教")||lessonplace.startsWith("二教")||lessonplace.startsWith("三教")||lessonplace.startsWith("基教")))
36     {
37         request.setAttribute("lessonError","上课地点有误");
38         %>
39         <<jsp:forward page="addInput.jsp"></jsp:forward>
40         <%
41     }
42     else
43         LessonDao.add(lesson);
44     %>
45         课程添加成功!<br>
46         <a href="addInput.jsp">继续添加</a><br>
47         <a href="list.jsp">课程列表</a>
48     <%
49     }catch(LessonException e){
50                  //验证出错
51                 request.setAttribute("lessonError", e.getMessage());
52         %>
53         <<jsp:forward page="addInput.jsp"></jsp:forward>
54     <%
55         }
56     %>
57 </html>

list.jsp

 1 <%@page import="com.jaovo.msg.model.Lesson"%>
 2 <%@page import="java.util.List"%>
 3 <%@page import="com.jaovo.msg.dao.LessonDaoImpl"%>
 4 <%@ page language="java" contentType="text/html; charset=UTF-8"
 5     pageEncoding="UTF-8"%>
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>课程展示界面</title>
11 </head>
12 <%
13     LessonDaoImpl lessonDao=new LessonDaoImpl();
14     List<Lesson> lessons=lessonDao.load();
15 %>
16 <body>
17 <h2 align="center">课程展示界面</h2>
18 <hr width="100%" color="blue"  />
19 <!--     
20     <jsp:include page="inc.jsp" >
21         <jsp:param name="op" value="展示" />
22     </jsp:include>
23      -->
24         <table align="center" border="1" width="500">
25         <tr>
26                 <td>课程编号</td>
27                 <td>课程名称</td>
28                 <td>任课教师</td>
29                 <td>上课地点</td>
30         </tr>
31         <%
32             for( Lesson lesson : lessons){
33         %>
34         <tr>
35                 <td><%=lesson.getId() %></td>
36                 <td><%=lesson.getLessonname() %></td>
37                 <td><%=lesson.getLessonteacher() %></td>
38                 <td><%=lesson.getLessonplace() %></td>
39             <!--      
40                 <td><a href="delete.jsp?id=<%=lesson.getId() %>" >删除</a></td>
41                 <td><a href="updateInput.jsp?id=<%=lesson.getId() %>" >更新</a></td>
42             -->
43         </tr>
44         <%
45             }
46         %>    
47         </table>
48 </body>
49 </html>

  

  3. 运行结果截图

原文地址:https://www.cnblogs.com/wang-jx/p/7911356.html