课堂作业(新课程添加)

  • 创建Maven项目

  • 导入依赖

 1     <dependency>
 2       <groupId>javax.servlet</groupId>
 3       <artifactId>javax.servlet-api</artifactId>
 4       <version>4.0.1</version>
 5       <scope>provided</scope>
 6     </dependency>
 7     <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
 8     <dependency>
 9       <groupId>mysql</groupId>
10       <artifactId>mysql-connector-java</artifactId>
11       <version>8.0.26</version>
12     </dependency>
  • 建连接数据库及释放的工具类

 1 import java.io.InputStream;
 2 import java.sql.*;
 3 import java.util.Properties;
 4 
 5 public class JDBCUtils {
 6     public static String url = null;
 7     public static String user = null;
 8     public static String driver = null;
 9     public static String password = null;
10 
11     static {
12         try {
13             InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
14             Properties properties = new Properties();
15             properties.load(in);
16 
17             driver = properties.getProperty("driver");
18             user = properties.getProperty("user");
19             password = properties.getProperty("password");
20             url = properties.getProperty("url");
21 
22             Class.forName(driver);
23 
24         } catch (Exception e) {
25             e.printStackTrace();
26         }
27     }
28 
29     public static Connection getConnection() throws SQLException {
30         return DriverManager.getConnection(url, user, password);
31     }
32 
33     public static void release(Connection conn, PreparedStatement st, ResultSet rs, Statement stt) {
34         if (rs != null) {
35             try {
36                 rs.close();
37             } catch (SQLException e) {
38                 e.printStackTrace();
39             }
40         }
41         if (st != null) {
42             try {
43                 st.close();
44             } catch (SQLException e) {
45                 e.printStackTrace();
46             }
47         }
48         if (stt != null) {
49             try {
50                 stt.close();
51             } catch (SQLException e) {
52                 e.printStackTrace();
53             }
54         }
55         if (conn != null) {
56             try {
57                 conn.close();
58             } catch (SQLException e) {
59                 e.printStackTrace();
60             }
61         }
62     }
63 }
  • 写properties文件

url=jdbc:mysql://localhost:3306/jdbcstudy?&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=true
driver=com.mysql.cj.jdbc.Driver
user=root
password=123456
  • 编写servlet文件

 1 import javax.servlet.ServletException;
 2 import javax.servlet.http.HttpServlet;
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 import java.io.IOException;
 6 import java.sql.*;
 7 import java.util.HashSet;
 8 import java.util.Set;
 9 
10 public class servlet extends HttpServlet {
11     @Override
12     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13         req.setCharacterEncoding("UTF-8");
14         resp.setCharacterEncoding("UTF-8");
15 
16         String classname = req.getParameter("classname");
17         String teachername = req.getParameter("teachername");
18         String place = req.getParameter("place");
19 
20         Set<String> hashSet = new HashSet<String>(8);
21         hashSet.add("王建民");
22         hashSet.add("刘立嘉");
23         hashSet.add("刘丹");
24         hashSet.add("杨子光");
25         hashSet.add("张云霞");
26         hashSet.add("武勇亮");
27         hashSet.add("高飞");
28         if (!hashSet.contains(teachername)) {
29             resp.getWriter().print("<script> alert(\"Teacher name input error\") </script>");
30             return;
31         }
32 
33         Set<String> hashSet2 = new HashSet<String>(5);
34         hashSet2.add("一教");
35         hashSet2.add("二教");
36         hashSet2.add("三教");
37         hashSet2.add("四教");
38         //System.out.println(classname);
39         if (!hashSet2.contains(place)) {
40             resp.getWriter().print("<script> alert(\"Classroom location input error\") </script>");
41             return;
42         }
43 
44         Connection conn = null;
45         ResultSet rs = null;
46         Statement stt = null;
47         try {
48             conn = JDBCUtils.getConnection();
49             stt = conn.createStatement();
50             String sql = "select * from lesson";
51             rs = stt.executeQuery(sql);
52 
53             while (rs.next()) {
54                 if (rs.getString("ipp").equals(classname)) {
55                     resp.getWriter().print("<script> alert(\"Duplicate course name, please re-enter\") </script>");
56                     return;
57                 }
58             }
59         } catch (SQLException e) {
60             e.printStackTrace();
61         }
62 
63         try {
64 
65             conn = JDBCUtils.getConnection();
66             String sql = "insert into `lesson`(`ipp`,`teacher`,`position`) values(?,?,?)";
67             PreparedStatement st = null;
68 
69             try {
70                 //创建语句传输对象
71                 st = conn.prepareStatement(sql);
72 
73                 st = conn.prepareStatement(sql);
74                 st.setString(1, classname);
75                 st.setString(2, teachername);
76                 st.setString(3, place);
77                 st.executeUpdate();
78                 int i = st.executeUpdate(sql);
79                 if (i > 0) {
80                     System.out.println("插入成功");
81                 }
82             } catch (SQLException e) {
83                 // TODO Auto-generated catch block
84                 e.printStackTrace();
85             } finally {
86                 //关闭资源
87                 JDBCUtils.release(conn, st, rs, stt);
88             }
89         } catch (SQLException e) {
90             e.printStackTrace();
91         }
92     }
93 }
 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 <form action="${pageContext.request.contextPath}\p" method="get">
10     <table align="center" border="1" width="500">
11         <tr>
12             <td>课程名称 : </td>
13             <td>
14                 <input type="text" name="classname" />
15             </td>
16         </tr>
17         <tr>
18             <td>任课教师:</td>
19             <td>
20                 <input type="text" name="teachername" />
21             </td>
22         </tr>
23         <tr>
24             <td>上课地点:</td>
25             <td>
26                 <input type="text" name="place" />
27             </td>
28         </tr>
29         <tr align="center">
30             <td colspan="2">
31                 <input type="submit" value="提交" />
32                 <input type="reset" value="重置" />
33             </td>
34         </tr>
35     </table>
36 </form>
37 </body>
38 </html>
  • 在web.xml下注册

1 <servlet>
2     <servlet-name>test</servlet-name>
3     <servlet-class>servlet</servlet-class>
4   </servlet>
5   <servlet-mapping>
6     <servlet-name>test</servlet-name>
7     <url-pattern>/p</url-pattern>
8   </servlet-mapping>
  • 项目总体结构

 

  •  效果

 

posted @ 2021-10-15 19:17  Cra2iTeT  阅读(257)  评论(0编辑  收藏  举报