网页版增加信息---添加

实验要求:

要求判断任课教师为王建民、刘立嘉、刘丹、封筠、韩培花五位教师的其中一位。

要求上课地点开头为“一教”、“二教”、“三教”、“基教”中的一种

实现数据存储功能。

程序设计思想:

先创建类模型,user

再通过DBUtil和数据库连接

然后在UserDaoImple中实现添加功能

再addInput.jsp文件中实现页面的构建

在add.jsp文件中实现网页添加和判断的功能。

源代码:

 package com.jaovo.msg.model;
 2 
 3 public class User {
 4    private int id;
 5    private String lesson;
 6    private String teacher;
 7    private String status;
 8 public User(int int1, String string, String string2, String string3) {
 9     // TODO Auto-generated constructor stub
10 }
11 public User() {
12     // TODO Auto-generated constructor stub
13 }
14 public int getId() {
15     return id;
16 }
17 public void setId(int id) {
18     this.id = id;
19 }
20 public String getLesson() {
21     return lesson;
22 }
23 public void setLesson(String lesson) {
24     this.lesson = lesson;
25 }
26 public String getTeacher() {
27     return teacher;
28 }
29 public void setTeacher(String teacher) {
30     this.teacher = teacher;
31 }
32 public String getStatus() {
33     return status;
34 }
35 public void setStatus(String status) {
36     this.status =status;
37 }
38    
39 }
1 package com.jaovo.msg.dao;
2 import com.jaovo.msg.model.User;
3 import java.util.List;
4 public interface IUserDao {
5     public void add(User user);//添加用户
6 }
 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 
11 import com.jaovo.msg.Util.DBUtil;
12 import com.jaovo.msg.Util.UserException;
13 import com.jaovo.msg.model.User;
14 
15 import sun.net.www.content.text.plain;
16 
17 public class UserDaoImpl implements IUserDao {
18 
19     @Override
20     public void add(User user) {
21         //获得链接对象
22         Connection connection = DBUtil.getConnection();
23         //准备sql语句
24         String sql = "select count(*) from t_user where lesson = ?";
25         //创建语句传输对象
26         PreparedStatement preparedStatement = null;
27         ResultSet resultSet = null;
28         try {
29             preparedStatement = connection.prepareStatement(sql);
30             preparedStatement.setString(1, user.getLesson());
31             //接收结果集
32             resultSet = preparedStatement.executeQuery();
33             //遍历结果集
34             while(resultSet.next()) {
35                 if (resultSet.getInt(1) > 0) {
36                     throw new UserException("用户已存在") ;
37                 }
38             }
39             
40             sql = "insert into t_user(lesson,teacher,status) value (?,?,?)";
41             preparedStatement = connection.prepareStatement(sql);
42             preparedStatement.setString(1, user.getLesson());
43             preparedStatement.setString(2, user.getTeacher());
44             preparedStatement.setString(3, user.getStatus());
45             preparedStatement.executeUpdate();
46         } catch (SQLException e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }finally {
50             //关闭资源
51             DBUtil.close(resultSet);
52             DBUtil.close(preparedStatement);
53             DBUtil.close(connection);
54         }
55         
56     }
57 }
 1 package com.jaovo.msg.filter;
 2 import java.io.IOException;
 3 
 4 import javax.servlet.Filter;
 5 import javax.servlet.FilterChain;
 6 import javax.servlet.FilterConfig;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10 public class CharFilter implements Filter{
11    String encoding=null;
12 @Override
13 public void destroy() {
14     // TODO Auto-generated method stub
15     
16 }
17 
18 @Override
19 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
20     // TODO Auto-generated method stub
21     request.setCharacterEncoding("utf-8");    
22     request.setCharacterEncoding("encoding");
23     chain.doFilter(request, response);
24 }
25 
26 @Override
27 public void init(FilterConfig filterConfig) throws ServletException {
28     // TODO Auto-generated method stub
29     encoding = filterConfig.getInitParameter("encoding");
30 }
31    
32 }
1 package com.jaovo.msg.Util;
2 
3 import com.jaovo.msg.dao.UserDaoImpl;
4 
5 public class DaoFactory {
6    public static UserDaoImpl getDaoImpl() {
7        return new UserDaoImpl();
8    }
9 }
 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/jaovo_msg";
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 }
 1 package com.jaovo.msg.Util;
 2 
 3 public class UserException extends RuntimeException{
 4 
 5     public UserException() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9     public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
10         super(message, cause, enableSuppression, writableStackTrace);
11         // TODO Auto-generated constructor stub
12     }
13     public UserException(String message, Throwable cause) {
14         super(message, cause);
15         // TODO Auto-generated constructor stub
16     }
17 
18     public UserException(String message) {
19         super(message);
20         // TODO Auto-generated constructor stub
21     }
22 
23     public UserException(Throwable cause) {
24         super(cause);
25         // TODO Auto-generated constructor stub
26     }
27     
28 }
ValidateUtil
1
package com.jaovo.msg.Util; 2 import java.util.HashMap; 3 import java.util.Map; 4 import javax.servlet.http.HttpServletRequest; 5 public class ValidateUtil { 6 public static boolean validateNull(HttpServletRequest request,String[] fileds) 7 { 8 boolean validate=true; 9 //map对象用来装载不同的错误信息 10 Map<String,String> errorMsg=new HashMap(); 11 for(String filed:fileds) 12 { 13 String value=request.getParameter(filed); 14 if(value==null||"".equals(value.trim())) 15 { 16 validate=false; 17 errorMsg.put(filed,filed+"不能为空"); 18 } 19 } 20 return validate; 21 } 22 public static String showError(HttpServletRequest request,String filed) 23 { 24 Map<String,String> errorMsg=(Map<String,String>)request.getAttribute("errormsg"); 25 if(errorMsg==null) 26 { 27 return ""; 28 } 29 String msg=errorMsg.get(filed); 30 if(msg==null) 31 { 32 return ""; 33 } 34 return msg; 35 } 36 }
add.jsp 
1
<%@page import="com.jaovo.msg.Util.UserException"%> 2 <%@page import="com.jaovo.msg.dao.UserDaoImpl"%> 3 <%@page import="com.jaovo.msg.model.User"%> 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 <% 9 //接收客户端传递过来的参数 10 String lesson = request.getParameter("lesson"); 11 String teacher = request.getParameter("teacher"); 12 String status = request.getParameter("status"); 13 if(lesson == null || "".equals(lesson.trim())){ 14 request.setAttribute("error", "用户名不能为空"); 15 //} 16 %> 17 <jsp:forward page="addInput.jsp"></jsp:forward> 18 <% 19 } 20 if(lesson!=null&&(lesson.equals("软件工程概论")||lesson.equals("统一建模")||lesson.equals("数据结构")||lesson.equals("离散数学")||lesson.equals("马克思原理"))) 21 { 22 23 } 24 else 25 { 26 request.setAttribute("error", "课程信息不在范围里"); 27 //} 28 %> 29 <jsp:forward page="addInput.jsp"></jsp:forward> 30 <% 31 } 32 if(teacher!=null&&(teacher.equals("刘丹")||teacher.equals("王建民")||teacher.equals("封筠")||teacher.equals("韩培花")||teacher.equals("刘力嘉"))) 33 { 34 35 } 36 else 37 { 38 request.setAttribute("error", "教师姓名不在范围里"); 39 //} 40 %> 41 <jsp:forward page="addInput.jsp"></jsp:forward> 42 <% 43 } 44 if(status!=null&&(status.equals("基教")||status.equals("二教")||status.equals("三教")||status.equals("九教")||status.equals("学院楼"))) 45 { 46 47 } 48 else 49 { 50 request.setAttribute("error", "任课地点不在范围里"); 51 //} 52 %> 53 <jsp:forward page="addInput.jsp"></jsp:forward> 54 <% 55 } 56 User user = new User(); 57 user.setLesson(lesson); 58 user.setTeacher(teacher); 59 user.setStatus(status); 60 61 UserDaoImpl userDao = new UserDaoImpl(); 62 try{ 63 userDao.add(user); 64 %> 65 用户保存成功!!<br> 66 <a href="addInput.jsp">继续添加</a><br> 67 <a href="#">用户列表</a> 68 <% 69 }catch(UserException e){ 70 %> 71 <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2> 72 <% 73 } 74 %> 75 </html>
addInput.jsp
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 <title>用户添加页面</title> 7 </head> 8 <body> 9 <%=request.getAttribute("error") %> 10 <form action="add.jsp" method="get"> 11 <table align="center" border="1" width="500"> 12 <tr> 13 <td>课程名称 : </td> 14 <td> 15 <input type="text" name="lesson" /> 16 </td> 17 </tr> 18 <tr> 19 <td>任课教师:</td> 20 <td> 21 <input type="text" name="teacher" /> 22 </td> 23 </tr> 24 <tr> 25 <td>任课地点:</td> 26 <td> 27 <input type="text" name="status" /> 28 </td> 29 </tr> 30 <tr align="center"> 31 <td colspan="2"> 32 <input type="submit" value="提交" /> 33 <input type="reset" value="重置" /> 34 </td> 35 </tr> 36 </table> 37 </form> 38 </body> 39 </html>

运行结果截图:

 

出现的错误分析:

出错的最基本点还是在特别容易连不上数据库,因为一些小的问题系统就出错误,而且在添加判断的时候,由于对html语言不太熟悉,因为一个花括号加错了一个位置,而出现了整个程序就出了问题,直接网页出现404的错误,当时确实特别懵逼,但是通过李志强及时看出了这个错误,让完成的时间减少了不少。

没完成的地方

就是关于地点的判断还达不到后缀有几零几。

心得:

希望完成这次博客园之后,找一些解决方案,对系统做进一步的分析。

 

posted @ 2017-11-28 17:44  Ayeah~夭夭  阅读(598)  评论(0编辑  收藏  举报