zoukankan      html  css  js  c++  java
  • 随堂测验——添加课程信息

    要求:

    添加课程信息

    (1)新课程信息必须唯一,如有重复,提示用户“课程名称重复,重新录入”;

    (2)要求判断任课教师为王建民、刘立嘉、刘丹、王辉、杨子光五位教师的其中一位。

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

    (4)将新课程信息添加入库。

    实现:

     1 package dao;
     2 import java.sql.Connection;
     3 import java.sql.SQLException;
     4 import java.sql.Statement;
     5 import DBUtil.DBUtil;
     6 import domain.Course;
     7 public class CourseDao {
     8     public boolean add(Course course) {
     9         String sql = "insert into test(name, teacher, classroom) values"+"('" + course.getName() + "','" + course.getTeacher() + "','" + course.getClassroom() + "')";
    10         Connection conn=DBUtil.getConn();
    11         Statement state=null;
    12         boolean f=false;
    13         int a = 0;
    14         try {
    15             state = conn.createStatement();
    16             state.executeUpdate(sql);
    17         } catch (SQLException e) {
    18             // TODO Auto-generated catch block
    19             e.printStackTrace();
    20         } finally {
    21             
    22             DBUtil.close(state, conn);
    23         }
    24         if(a>0)
    25             f=true;
    26         return f;
    27         }
    28 }
     1 package DBUtil;
     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 import java.sql.Statement;
     9 
    10 public class DBUtil {
    11 
    12     public static String db_url = "jdbc:mysql://localhost:3306/course?serverTimezone=GMT%2B8&useSSL=false";
    13     public static String db_user = "root";
    14     public static String db_pass = "0424wyhhxx";
    15 
    16     public static Connection getConn () {
    17         Connection conn = null;
    18 
    19         try {
    20             Class.forName("com.mysql.jdbc.Driver");
    21             conn = DriverManager.getConnection(db_url, db_user, db_pass);
    22         } catch (Exception e) {
    23             e.printStackTrace();
    24         }
    25 
    26         return conn;
    27     }//end getConn
    28 
    29     public static void close (Statement state, Connection conn) {
    30         if (state != null) {
    31             try {
    32                 state.close();
    33             } catch (SQLException e) {
    34                 e.printStackTrace();
    35             }
    36         }
    37 
    38         if (conn != null) {
    39             try {
    40                 conn.close();
    41             } catch (SQLException e) {
    42                 e.printStackTrace();
    43             }
    44         }
    45     }
    46 
    47     public static void close (ResultSet rs, Statement state, Connection conn) {
    48         if (rs != null) {
    49             try {
    50                 rs.close();
    51             } catch (SQLException e) {
    52                 e.printStackTrace();
    53             }
    54         }
    55 
    56         if (state != null) {
    57             try {
    58                 state.close();
    59             } catch (SQLException e) {
    60                 e.printStackTrace();
    61             }
    62         }
    63 
    64         if (conn != null) {
    65             try {
    66                 conn.close();
    67             } catch (SQLException e) {
    68                 e.printStackTrace();
    69             }
    70         }
    71     }
    72 
    73     public static void main(String[] args) throws SQLException {
    74         Connection conn = getConn();
    75         PreparedStatement pstmt = null;
    76         ResultSet rs = null;
    77         String sql ="select * from test";
    78         pstmt = conn.prepareStatement(sql);
    79         rs = pstmt.executeQuery();
    80         if(rs.next()){
    81             System.out.println("成功");
    82         }else{
    83             System.out.println("失败");
    84         }
    85     }
    86 }
    package domain;
    
    public class Course {
    
        private String name;
        private String teacher;
        private String classroom;
         
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getTeacher() {
            return teacher;
        }
        public void setTeacher(String teacher) {
            this.teacher = teacher;
        }
        public String getClassroom() {
            return classroom;
        }
        public void setClassroom(String classroom) {
            this.classroom = classroom;
        }
         
        public Course() {}
         
        public Course(String name, String teacher, String classroom) {
            
            this.name = name;
            this.teacher = teacher;
            this.classroom = classroom;
        }
    }
     1 package servlet;
     2 
     3 import java.io.IOException;
     4 import javax.servlet.ServletException;
     5 import javax.servlet.annotation.WebServlet;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 
    10 import dao.CourseDao;
    11 import domain.Course;
    12 
    13 /**
    14  * Servlet implementation class AddServlet
    15  */
    16 @WebServlet("/addServlet")
    17 public class AddServlet extends HttpServlet {
    18     private static final long serialVersionUID = 1L;
    19        
    20     /**
    21      * @see HttpServlet#HttpServlet()
    22      */
    23     public AddServlet() {
    24         super();
    25         // TODO Auto-generated constructor stub
    26     }
    27 
    28     /**
    29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    30      */
    31     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    32         // TODO Auto-generated method stub
    33         request.setCharacterEncoding("utf-8");
    34         String name =request.getParameter("name");
    35         String teacher = request.getParameter("teacher");
    36         String classroom =request.getParameter("classroom");
    37         Course course = new Course(name, teacher, classroom);
    38         CourseDao cd = new CourseDao(); 
    39         
    40         if(!cd.add(course)) {
    41             request.setAttribute("message","添加成功");
    42             request.getRequestDispatcher("add.jsp").forward(request,response);
    43         } else {
    44             request.setAttribute("message", "添加失败,请重新录入");
    45             request.getRequestDispatcher("add.jsp").forward(request,response);
    46         }
    47         
    48     }
    49 
    50     /**
    51      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    52      */
    53     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    54         // TODO Auto-generated method stub
    55         doGet(request, response);
    56     }
    57 
    58 }
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html>
     4 <html>
     5 <head>
     6 <meta charset="UTF-8">
     7 <title>Insert title here</title>
     8 <style>
     9     .a{
    10         margin-top: 20px;
    11     }
    12     .b{
    13         font-size: 20px;
    14          160px;
    15         color: white;
    16         background-color: greenyellow;
    17     }
    18     body{
    19           padding:20px 0px;
    20           background-color:pink;
    21           text-align:center;
    22           }
    23      form button:hover{
    24       background-color:#6666FF;
    25      }
    26 </style>
    27 </head>
    28 <body>
    29    <%
    30           Object message = request.getAttribute("message");
    31           if(message!=null && !"".equals(message)){
    32       
    33      %>
    34           <script type="text/javascript">
    35                alert("<%=request.getAttribute("message")%>");
    36           </script>
    37      <%} %>
    38     <div align="center">
    39         <h1 style="color: red;">课程信息录入</h1>
    40         <form action="${pageContext.request.contextPath }/addServlet" method="post" onsubmit="return check()">
    41             <div class="a">
    42                 课程名称<input type="text" id="name" name="name"/>
    43             </div>
    44             <div class="a">
    45                 任课教师<input type="text" id="teacher" name="teacher" />
    46             </div>
    47             <div class="a">
    48                 上课地点<input type="text" id="classroom" name="classroom" />
    49             </div>
    50             <div class="a">
    51                 <button type="submit" class="b">保   存</button>
    52             </div>
    53         </form>
    54     </div>
    55     <script type="text/javascript">
    56         function check() {
    57             var name = document.getElementById("name");;
    58             var teacher = document.getElementById("teacher");
    59             var classroom = document.getElementById("classroom");
    60              
    61             //非空
    62             if(name.value == '') {
    63                 alert('课程名称为空');
    64                 name.focus();
    65                 return false;
    66             }
    67             if(teacher.value == '') {
    68                 alert('教师为空');
    69                 teacher.focus();
    70                 return false;
    71             }
    72             if(classroom.value == '') {
    73                 alert('上课地点为空');
    74                 classroom.focus();
    75                 return false;
    76             }
    77              
    78             //教师
    79             if(teacher.value != '王建民' && teacher.value != '王辉' && teacher.value != '刘丹' && teacher.value != '刘立嘉' && teacher.value != '杨子光'){
    80                 alert('教师名称错误');
    81                 return false;
    82             }
    83              
    84             //教室
    85             if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) {
    86                 alert('上课地点错误');
    87                 return false;
    88             }
    89         }  
    90         </script type="text/javascript">      
    91 </body>
    92 </html>

     测试截图:

     

  • 相关阅读:
    根据会员权限显示指定字段教程与源码
    关键字替换排除HTML标签属性字符
    C# 图片处理(压缩、剪裁,转换,优化)
    点击按钮后表单自动提交的问题
    浏览器中添加收藏当前网页
    Javascript基础知识整理
    JS中不同类型的值比较问题
    ACM训练场
    sencha/extjs 动态创建grid表格
    sencha 报错问题汇总
  • 原文地址:https://www.cnblogs.com/znjy/p/13972525.html
Copyright © 2011-2022 走看看