软件工程概论课堂测试一(设计添加新课程界面)

设计思想:

源代码:

  1 package com.jaovo.msg.dao;
  2 import java.sql.Connection;
  3 import java.sql.PreparedStatement;
  4 import java.sql.ResultSet;
  5 import java.sql.SQLException;
  6 import java.util.List;
  7 import java.util.ArrayList;
  8
  9
 10 import com.jaovo.msg.Util.DBUtil;
 11 import com.jaovo.msg.model.User;
 12 import com.jaovo.msg.Util.UserException;
 13
 14
 15 public class UserDaoImpl implements IUserDao{
 16
 17     @Override
 18     public void add(User user) {
 19         // TODO Auto-generated method stub
 20         Connection connection = DBUtil.getConnection();
 21
 22         String sql = "select count(*) from t_user where username = ?";
 23
 24         PreparedStatement preparedStatement = null;
 25         ResultSet resultSet = null;
 26         try {
 27             preparedStatement =  connection.prepareStatement(sql);
 28             preparedStatement.setString(1, user.getUsername());
 29
 30             resultSet = preparedStatement.executeQuery();
 31
 32 //            while(resultSet.next()) {
 33 //                if (resultSet.getInt(1) > 0) {
 34 //                    throw new UserException("课程已存在") ;
 35 //                }
 36 //            }
 37 //
 38             sql = "insert into t_user(username,password,nickname) value (?,?,?)";
 39             preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
 40             preparedStatement.setString(1, user.getUsername());
 41             preparedStatement.setString(2, user.getPassword());
 42             preparedStatement.setString(3, user.getNickname());
 43             preparedStatement.executeUpdate();
 44         } catch (SQLException e) {
 45             // TODO Auto-generated catch block
 46             e.printStackTrace();
 47         }finally {
 48
 49             DBUtil.close(resultSet);
 50             DBUtil.close(preparedStatement);
 51             DBUtil.close(connection);
 52         }
 53
 54     }
 55
 56
 57     @Override
 58     public void delete(int id) {
 59         // TODO Auto-generated method stub
 60
 61         Connection connection = DBUtil.getConnection();
 62         String sql = "delete from t_user where id = ?";
 63         PreparedStatement preparedStatement = null;
 64
 65         try {
 66             preparedStatement =  connection.prepareStatement(sql);
 67             preparedStatement.setInt(1, id);
 68             preparedStatement.executeUpdate();
 69         } catch (SQLException e) {
 70             // TODO Auto-generated catch block
 71             e.printStackTrace();
 72         }finally {
 73             DBUtil.close(preparedStatement);
 74             DBUtil.close(connection);
 75         }
 76
 77     }
 78
 79     @Override
 80     public void update(User user) {
 81         Connection connection = DBUtil.getConnection();
 82
 83         String sql = "update t_user set password = ? , nickname=? where id = ?";
 84
 85         PreparedStatement preparedStatement = null;
 86         try {
 87             preparedStatement = connection.prepareStatement(sql);
 88             preparedStatement.setString(1, user.getPassword());
 89             preparedStatement.setString(2, user.getNickname());
 90             preparedStatement.setInt(3, user.getId());
 91             preparedStatement.executeUpdate();
 92         } catch (SQLException e) {
 93             // TODO Auto-generated catch block
 94             e.printStackTrace();
 95         }finally {
 96             DBUtil.close(preparedStatement);
 97             DBUtil.close(connection);
 98         }
 99     }
100
101
102
103
104     @Override
105     public User load(int id) {
106         Connection connection = DBUtil.getConnection();
107
108         String sql = "select * from t_user  where id = ?";
109
110         PreparedStatement preparedStatement = null;
111         ResultSet resultSet = null;
112         User user = null;
113         try {
114             preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
115             preparedStatement.setInt(1, id);
116             resultSet = preparedStatement.executeQuery();
117             while(resultSet.next()) {
118                 user = new User();
119                 user.setId(id);
120                 user.setUsername(resultSet.getString("username"));
121                 user.setPassword(resultSet.getString("password"));
122                 user.setNickname(resultSet.getString("nickname"));
123             }
124         } catch (SQLException e) {
125             // TODO Auto-generated catch block
126             e.printStackTrace();
127         }finally {
128             DBUtil.close(resultSet);
129             DBUtil.close(preparedStatement);
130             DBUtil.close(connection);
131         }
132         return  user;
133     }
134
135
136     @Override
137     public User load(String username) {
138         // TODO Auto-generated method stub
139         return null;
140     }
141
142     @Override
143     public List<User> load() {
144         Connection connection = DBUtil.getConnection();
145
146         String sql = "select * from t_user ";
147
148         PreparedStatement preparedStatement = null;
149         ResultSet resultSet = null;
150
151         List<User> users = new ArrayList<User>();
152         User user = null;
153         try {
154             preparedStatement = connection.prepareStatement(sql);
155             resultSet = preparedStatement.executeQuery();
156             while(resultSet.next()) {
157                 user = new User();
158                 user.setId(resultSet.getInt("id"));
159                 user.setUsername(resultSet.getString("username"));
160                 user.setPassword(resultSet.getString("password"));
161                 user.setNickname(resultSet.getString("nickname"));
162                 users.add(user);
163             }
164         } catch (SQLException e) {
165             // TODO Auto-generated catch block
166             e.printStackTrace();
167         }finally {
168             DBUtil.close(resultSet);
169             DBUtil.close(preparedStatement);
170             DBUtil.close(connection);
171         }
172         return  users;
173     }
174
175 }
 1 package com.jaovo.msg.model;
 2
 3 public class User {
 4     private int id;
 5     private String  username;
 6     private String  nickname;
 7     private String  password;
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getUsername() {
15         return username;
16     }
17     public void setUsername(String username) {
18         this.username = username;
19     }
20     public String getNickname() {
21         return nickname;
22     }
23     public void setNickname(String nickname) {
24         this.nickname = nickname;
25     }
26     public String getPassword() {
27         return password;
28     }
29     public void setPassword(String password) {
30         this.password = password;
31     }
32
33
34 }
 1 package com.jaovo.msg.Util;
 2
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7
 8 import java.sql.PreparedStatement;
 9
10 public class DBUtil {
11     public  static  Connection getConnection() {
12
13         try {
14
15             Class.forName("com.mysql.jdbc.Driver").newInstance();
16         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
17             // TODO Auto-generated catch block
18             e.printStackTrace();
19         }
20         String user = "root";
21         String password = "root";
22         String url = "jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8";
23         Connection connection = null;
24         try {
25
26              connection = DriverManager.getConnection(url,user,password);
27         } catch (SQLException e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30         }
31         return connection;
32     }
33
34
35     public static void close(Connection connection ) {
36         try {
37             if (connection != null) {
38                 connection.close();
39             }
40
41         } catch (SQLException e) {
42             // TODO Auto-generated catch block
43             e.printStackTrace();
44         }
45     }
46     public static void close(PreparedStatement preparedStatement ) {
47         try {
48             if (preparedStatement != null) {
49                 preparedStatement.close();
50             }
51
52         } catch (SQLException e) {
53             // TODO Auto-generated catch block
54             e.printStackTrace();
55         }
56     }
57     public static void close(ResultSet resultSet ) {
58         try {
59             if (resultSet != null) {
60                 resultSet.close();
61             }
62
63         } catch (SQLException e) {
64             // TODO Auto-generated catch block
65             e.printStackTrace();
66         }
67     }
68
69
70
71
72 }
 1 package com.jaovo.msg.Util;
 2
 3 public class UserException  extends Exception{
 4
 5     public UserException() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9
10     public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
11         super(message, cause, enableSuppression, writableStackTrace);
12         // TODO Auto-generated constructor stub
13     }
14
15     public UserException(String message, Throwable cause) {
16         super(message, cause);
17         // TODO Auto-generated constructor stub
18     }
19
20     public UserException(String message) {
21         super(message);
22         // TODO Auto-generated constructor stub
23     }
24
25     public UserException(Throwable cause) {
26         super(cause);
27         // TODO Auto-generated constructor stub
28     }
29
30
31
32
33
34 }
 1 package com.jaovo.msg.Util;
 2
 3 public class UserException1   extends Exception{
 4
 5     public UserException1() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9
10
11
12     public UserException1(String message) {
13         super(message);
14         // TODO Auto-generated constructor stub
15     }
16
17
18
19 } 
 1 -- mysal -u root -p root;
 2 -- show databases;
 3 -- drop database jaovo_shop;
 4
 5 create database jaovo_msg;
 6 use jaovo_msg;
 7 GRANT ALL ON jaovo_msg.* to "jaovo"@"localhost" IDENTIFIED BY "root";
 8 create table t_user(
 9      id int(10) primary key auto_increment,
10      username varchar(255),
11      password varchar(255),
12      nickname varchar(255),
13      type int(2),
14      status int(2)
15 );
16
17 create table t_message(
18    id          int(10) primary key auto_increment,
19    title                varchar(254),
20    content              text,
21    post_date             datetime,
22    user_id               int(10),
23    CONSTRAINT FOREIGN KEY(user_id) REFERENCES t_user(id)
24 );
25
26 create table t_comment(
27    id        int(10) primary key auto_increment,
28    content       text,
29    post_date     datetime,
30    user_id       int(10),
31    msg_id        int(10),
32    CONSTRAINT FOREIGN KEY(user_id) REFERENCES t_user(id),
33    CONSTRAINT FOREIGN KEY(msg_id) REFERENCES t_message(id)
34 );
 1 <%@page import="com.jaovo.msg.Util.UserException"%>
 2 <%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
 3 <%@page import="com.jaovo.msg.Util.UserException"%>
 4 <%@page import="com.jaovo.msg.model.User"%>
 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     //接收客户端传递过来的参数
12
13     String username = request.getParameter("username");
14     String password = request.getParameter("password");
15     String nickname = request.getParameter("nickname");
16
17
18
19     try{
20     if(!password.equals("王建民")&&!password.equals("刘丹")&&!password.equals("刘立嘉"))
21     {
22         throw new UserException( "教师名不存在");
23
24     }
25     if(!nickname.startsWith("基教")&&!nickname.startsWith("一教")&&!nickname.startsWith("二教")&&!nickname.startsWith("三教"))
26     {
27         throw new UserException( "教室不存在");
28     }
29     User user = new User();
30     user.setUsername(username);
31     user.setPassword(password);
32     user.setNickname(nickname);
33
34     UserDaoImpl userDao = new UserDaoImpl();
35
36         userDao.add(user);
37 %>
38
39
40     课程保存成功!!<br>
41     <a href="addInput.jsp">继续添加</a><br>
42
43 <%
44     }catch(UserException e){
45 %>
46     <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
47     <%
48     }
49     %>
50 </html>
 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
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="username" />
16
17                 </td>
18             </tr>
19                 <tr>
20                 <td>任课教师:</td>
21                 <td>
22                     <input type="text" name="password" />
23                 </td>
24             </tr>
25             <tr>
26                 <td>上课地点:</td>
27                 <td>
28                     <input type="text" name="nickname" />
29                 </td>
30             </tr>
31             <tr align="center">
32                 <td colspan="2">
33                     <input type="submit" value="保存" />
34
35                 </td>
36             </tr>
37         </table>
38     </form>
39 </body>
40 </html>
 1 package com.jaovo.msg.dao;
 2
 3
 4
 5 import java.util.List;
 6
 7 import com.jaovo.msg.model.User;
 8
 9 public interface IUserDao {
10     public void add(User user);
11     public void delete(int id);
12     public void update(User user);
13     public User load(int id);
14     public User load(String username);
15     public List<User> load();
16
17 }

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>User_Message1</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12 </web-app>

运行截图:

时间: 2024-10-29 07:54:27

软件工程概论课堂测试一(设计添加新课程界面)的相关文章

软件工程概论课堂测试一————添加新课程(web)

设计思想 三个文件Class_add.java  add.jsp  addInput.jsp Class_add.java : 内封装方法:连接数据库.向数据库添加课程信息.判断非合理的输入情况.判断添加的课程是否重复. addInput.jsp      :  完成显示输入界面,点击保存后跳转到addInput.jsp. add.jsp              :  处理输入的数据,并给出相应的交互提示. 源程序代码 Class_add.java : import java.sql.Conn

软件工程概论——课堂测试1

设计思想:1.用1个页面,实现课程录入,提交后直接返回课程界面.2.应用html表单属性进行数据的提交.3.用servlet进行写入数据库和验证输入. 源代码: <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><%String path = request.getContextPath();String b

软件工程概论课堂作业1

1.网站系统开发需要掌握的技术 (1)数据库链接技术 (2)JavaBean技术 (3)Servlet技术 (4)流行框架与流行XML技术 2.本次课堂测试的源程序代码 <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%><!DOCTYPE html PUBLIC "-//W3C//DTD H

软件工程概论第八章--面向对象设计

面向对象设计主要讲设计的概念.软件体系结构.系统设计.详细设计.应用设计模式.用户界面设计和设计文档等方面知识,面向对象的设计是面向对象技术中比较重要的阶段. 设计的概念中讲了设计活动和设计原则,设计是一个建模活动,此活动能实现从需求分析到软件实现间的跨越.设计原则主要有模块化.耦合度和内聚性和复用性,模块化可以使复杂的系统简化,耦合度和内聚性分别是子系统间的关联程度和系统内部的相关程度,降低耦合性提高内聚性.复用性利用以开发的软件元素生成新的软件系统. 软件体系结构主要有仓库体系结构,分层体系

新课程界面

1.程序设计思想 (1)新建数据库,名称为dbCourse,新建表,表名为t_user. (2)创建包DBBean,将sqljdbc_4.0导入到lib中和Referenced Libraries,在DBBean中下,创建AddCourse.class文件,在里面有数据库的操作,通过sa用户连接.创建AddCourse类,在类中有异常处理,创建类addCourse,形参有3个,分别为name,teacher,point,均为String类型.在类中输入代码进行提示,判断课程,教师,上课地点是否输

软件工程概论课堂作业3

题目:返回一个整数数组中最大子数组的和 要求: 输入一个一维整形数组,数组里有正数也有负数. 一维数组首尾相接,象个一条首尾相接带子一样. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值. 设计思想: 用户自定义数组长度并依次输入数组元素,设一个全局变量初始化为零的数组a[N],N=10000: 1.因为该数组首尾相接可视作一个环,那么我们需要在一个合适的位置断开,把数组元素展成一条笔直的带子. (1).设用户自定义数组长度为m,输入数组各元素值a[1

基于spring-boot的测试桩设计-添加配置文件(properties)

编写测试时,有些内容可以放到配置文件中. 第一步:新增配置文件 conf.properties 第二步:编写配置文件类 MockConf 1 package mock.mockdemo.conf; 2 3 import lombok.Data; 4 import org.springframework.beans.factory.annotation.Value; 5 import org.springframework.context.annotation.PropertySource; 6

软件概论课堂测试

课题: 编写教师开设课程的网页 代码: package pers.sun.DataBase; import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException; public class Data {     public static Connection getConn

软件工程开学课堂测试

这个测试给我的总体感受挺头大的,对于上学期本来就没怎么学好的我,无疑是一个艰难的过程,这次与以往测试不同的是这次给了一个模板,要求也算是利用这个模板的前提下,进行相应的编写,编写的过程也是遇到各种问题,如登录界面验证码的问题,验证码实现不了验证的功能,是不是听起来很滑稽[/哭笑],后来又弄了好一会呢,然后用的模板吗,好多东西看着难,也不好找,JavaScript没怎么学过,然后就不会...  还需要很大的努力呀,我没觉得自己厉害,然后现实自己的菜比我想象的菜还要菜,加油吧 原文地址:https: