zoukankan      html  css  js  c++  java
  • 软件工程概论课堂测试一(设计添加新课程界面)

    设计思想:首先实现界面功能,(包含三个输入框分别是课程名称,老师,上课地点),再添加一个保存按钮。之后建立与数据的连接,将信息保存在数据库中的表中

    。之后定义异常处理机制,如果输入与要求不符抛出相应的异常信息,并输出。

    源代码:

      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>

    运行截图:

    计划总结:

     

  • 相关阅读:
    机器学习规则:ML工程最佳实践----rules_of_ml section 2【翻译】
    机器学习规则:ML工程最佳实践----rule_of_ml section 3【翻译】
    知识图谱技术分享会----有关知识图谱构建的部分关键技术简介及思考
    【e2Open】
    【2B】企业供应链管理软件
    【交互】复杂逻辑配置的一个不错的方法(神策数据)
    【视觉】交易数据展示
    【视觉】数据平台
    【设计复盘】APP设计复盘
    【设计规范】腾讯课堂
  • 原文地址:https://www.cnblogs.com/zyt-bg/p/7911532.html
Copyright © 2011-2022 走看看