码迷,mamicode.com
首页 > 其他好文 > 详细

课堂测试——增加信息

时间:2018-01-17 11:23:56      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:tco   dao   保存   base   sys   function   nec   not   als   

 

 

 

一、程序设计思想

 按照JSP+Servlet+JavaBean的开发模式,将编码任务划分成以下部分:

JSP:页面上显示输入框和提交按钮,并且在界面上插入JavaScript脚本,用来检验输入信息是否满足要求,若满足要求则点击提交按钮可以提交,否则仍会停留在当前界面。

Servlet:控制流程,说白了就是把JSP上输入的合法信息通过Servlet文件中的执行语句将数据写入数据表中。

JavaBean:定义Course类,规定有授课教师teacher,课程名称course,授课地点place。Course类的对象是整个流程中调控的对象。

Dao:数据库访问,实现了把Course对象写入数据表的方法,Servlet的执行流程会调用这些方法。

二、源代码

 1 //Course.Java
 2 public class Course {
 3       private String teacher;
 4       private String course;
 5       private String place;
 6       public Course() {
 7           
 8       }
 9     public String getTeacher() {
10         return teacher;
11     }
12     public void setTeacher(String teacher) {
13         this.teacher = teacher;
14     }
15     public String getCourse() {
16         return course;
17     }
18     public void setCourse(String course) {
19         this.course = course;
20     }
21     public String getPlace() {
22         return place;
23     }
24     public void setPlace(String place) {
25         this.place = place;
26     }
27       
28 }
  1 //Dao.Java
  2 import java.sql.Connection;
  3 import java.sql.DriverManager;
  4 import java.sql.Statement;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 
  9 public class Dao {
 10     public static Connection getConnection() {
 11         Connection con = null;
 12         Statement stmt = null;
 13         String url = "jdbc:sqlserver://localhost:1433;DatabaseName=DrinkeryManage";
 14         try {
 15             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
 16             con = DriverManager.getConnection(url,"sa","leomessi10");
 17           } catch (ClassNotFoundException | SQLException e) {
 18               System.out.println(e.getMessage());
 19           }
 20         close(stmt);
 21         return con;
 22     }
 23     
 24     public static void close(Connection con) {
 25        
 26             try {
 27                 if(con!=null)
 28                      con.close();
 29             } catch (SQLException e) {
 30                 System.out.println(e.getMessage());
 31             }
 32     }
 33     
 34     public static void close(PreparedStatement pre) {
 35         try {
 36                 if(pre!=null)
 37                      pre.close();
 38             } catch (SQLException e) {
 39                 System.out.println(e.getMessage());
 40             }
 41     }
 42     
 43     public static void close(Statement stmt) {
 44         try {
 45                 if(stmt!=null)
 46                      stmt.close();
 47             } catch (SQLException e) {
 48                 System.out.println(e.getMessage());
 49             }
 50     }
 51     
 52     public static void close(ResultSet rs) {
 53         try {
 54                 if(rs!=null)
 55                      rs.close();
 56             } catch (SQLException e) {
 57                 System.out.println(e.getMessage());
 58             }
 59     }
 60 }
 61 
 62 //UserDao.Java
 63 import Bean.Course;
 64 import java.sql.Connection;
 65 import java.sql.DriverManager;
 66 import java.sql.Statement;
 67 
 68 
 69 import java.sql.PreparedStatement;
 70 import java.sql.ResultSet;
 71 import java.sql.SQLException;
 72 
 73 public class UseDao {
 74       public void add(Course course) {
 75           Connection con = Dao.getConnection();
 76           PreparedStatement pre = null;
 77           try {
 78             pre = con.prepareStatement("insert into Teacher(course,teacher,place) values(?,?,?)");
 79             pre.setString(1,course.getCourse());
 80             pre.setString(2,course.getTeacher());
 81             pre.setString(3,course.getPlace());
 82             pre.executeUpdate();
 83         } catch (SQLException e) {
 84             // TODO 自动生成的 catch 块
 85             e.printStackTrace();
 86             System.out.println(e.getMessage());
 87         }
 88           Dao.close(pre);
 89           Dao.close(con);
 90       }
 91 }
 92 
 93 //Servlet.Java
 94 import Bean.Course;
 95 import Dao.*;
 96 
 97 
 98 import java.io.IOException;
 99 import javax.servlet.http.HttpServlet;
100 import javax.servlet.ServletException;
101 import javax.servlet.http.HttpServletRequest;
102 import javax.servlet.http.HttpServletResponse;
103 
104 @SuppressWarnings("serial")
105 public class Servlet extends HttpServlet{
106     public void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
107         req.setCharacterEncoding("utf-8");
108         String method = req.getParameter("method");
109         if(method.equals("add"))add(req,resp); 
110     }
111     public void add(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
112         req.setCharacterEncoding("utf-8");
113       String course = req.getParameter("course");
114       String teacher = req.getParameter("teacher");
115       String place = req.getParameter("place");
116       Course cou = new Course();
117       cou.setCourse(course);
118       cou.setTeacher(teacher);
119       cou.setPlace(place);
120       UseDao usedao = new UseDao();
121       usedao.add(cou);
122     }
123 }
 1 //login.jsp
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <%@ page language="java" contentType="text/html; charset=UTF-8"
 6     pageEncoding="UTF-8"%>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <script type="text/javascript">
 9 function check(form){
10     course = form.course.value;
11     teacher = form.teacher.value;
12     place = form.place.value;
13     if(course==""){
14         alert("课程名称不能为空");
15         return false;
16     }
17     if(teacher!="王建民"&&teacher!="刘立嘉"&&teacher!="刘丹"&&teacher!="王辉"&&teacher!="杨子光"){
18         alert("老师必须为王建民、刘立嘉、刘丹、王辉、杨子辉中的一位");
19         return false;
20     }
21     if(place.indexOf("一教")!=0&&place.indexOf("二教")!=0&&place.indexOf("三教")!=0&&place.indexOf("基教")!=0){
22         alert("上课地点不对");
23         return false;
24     }
25     str = "我他妈呵呵"
26     console.err(str);
27     return true;
28 }
29 </script>
30 <title>增加信息</title>
31 </head>
32 <body>
33 <form name="form1" action="${pageContext.request.contextPath}/Servlet/Servlet?method=add"  method="post" target="_self" onsubmit="return check(form1)">
34 <table>
35    <tr>
36       <td colspan="2">课程名称:<input type="text" name="course"></td>
37       
38    </tr>
39    <tr>
40       <td colspan="2">教师姓名:<input type="text" name="teacher"></td>
41       
42    </tr>
43    <tr>
44       <td colspan="2">上课地点:<input type="text" name="place"></td>
45       
46    </tr>
47    
48    <tr>
49       <td><input type="submit" value="保存"></td>
50    </tr>
51 </table>
52 </form>
53 </body>
54 </html>
1 //在web.xml中添加以下内容
2  <servlet>
3       <servlet-name>Servlet</servlet-name>
4       <servlet-class>Servlet.Servlet</servlet-class>
5   </servlet>
6   <servlet-mapping>
7       <servlet-name>Servlet</servlet-name>
8       <url-pattern>/Servlet/Servlet</url-pattern>
9   </servlet-mapping>

三、验证截图

技术分享图片

技术分享图片

 

 技术分享图片

技术分享图片

技术分享图片

 

 

--------------------------------------------------------------------------------------------------------------------------------------------END

课堂测试——增加信息

标签:tco   dao   保存   base   sys   function   nec   not   als   

原文地址:https://www.cnblogs.com/messi2017/p/8301351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!