• 02PSP0级及登陆界面开发


    一、PSP0级

    二、程序设计思想

    编写jsp,dao包内加入方法,连接数据库,测试连接。

    三、源代码

    <form action="add.jsp" method="get">
            <table align="center" border="1" width="500">
                <tr>
                    <td> 课程名称: </td>
                    <td>
                        <input type="text" name="username" />
                        <%
    //                         if(errorMsg != null){
    //                             if(errorMsg.get("username") != null){
    //                                 out.println( errorMsg.get("username"));
    //                             }
    //                         }
                            
                        %>
                        
                    </td>
                </tr>
                    <tr>
                    <td>教师名称:</td>
                    <td>
                        <input type="password" name="password" />
                        <%
    //                         if(errorMsg != null){
    //                             if(errorMsg.get("password") != null){
    //                                 out.println( errorMsg.get("password"));
    //                             }
    //                         }
                        
                        %>
                        
                    </td>
                </tr>
                <tr>
                    <td>上课地点:</td>
                    <td>
                        <input type="text" name="nickname" />
                        <%
    //                         if(errorMsg != null){
    //                             if(errorMsg.get("nickname") != null){
    //                                 out.println( errorMsg.get("nickname"));
    //                             }
    //                         }
                        
                        %>
                    
                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="submit" value="提交" />
                        <input type="reset" value="重置" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
    </html>
      <%@page import="com.jaovo.msg.Util.UserException"%>
    <%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
    <%@page import="com.jaovo.msg.model.User"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <%
        //接收客户端传递过来的参数
        String username = request.getParameter("username");
        String password = request.getParameter("passwor");
        String nickname = request.getParameter("nickname");
        if(username == null || "".equals(username.trim())){
            request.setAttribute("error", "用户名不能为空");
        
    %>
        <jsp:forward page="addInput.jsp"></jsp:forward>
    <%
    }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        
        UserDaoImpl userDao = new UserDaoImpl();
        try{
        userDao.add(user);
    %>
    
    
        用户保存成功!!<br>
        <a href="addInput.jsp">继续添加</a><br>
        <a href="#">用户列表</a>
    <%
        }catch(UserException e){
    %>
        <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
        <%
        }
        %>
    </html>
    package com.jaovo.msg.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    
    import com.jaovo.msg.Util.DBUtil;
    import com.jaovo.msg.Util.UserException;
    import com.jaovo.msg.model.User;
    
    import sun.net.www.content.text.plain;
    
    public class UserDaoImpl implements IUserDao {
    
        @Override
        public void add(User user) {
            
            Connection connection = DBUtil.getConnection();
            
            String sql = "select count(*) from t_user where username = ?";
    
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getUsername());
                
                resultSet = preparedStatement.executeQuery();
        
                while(resultSet.next()) {
                    if (resultSet.getInt(1) > 0) {
                        throw new UserException("鐢ㄦ埛宸插瓨鍦�") ;
                    }
                }
                
                sql = "insert into t_user(username,password,nickname) value (?,?,?)";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getUsername());
                preparedStatement.setString(2, user.getPassword());
                preparedStatement.setString(3, user.getNickname());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            
        }
    
        @Override
        public void delete(int id) {
            Connection connection = DBUtil.getConnection();
            String sql = "delete from t_user where id = ?";
            PreparedStatement preparedStatement = null;
            
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1, id);
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            
        }
    
        @Override
        public void update(User user) {
            Connection connection = DBUtil.getConnection();
        
            String sql = "update t_user set password = ? , nickname=? where id = ?";
    
            PreparedStatement preparedStatement = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getPassword());
                preparedStatement.setString(2, user.getNickname());
                preparedStatement.setInt(3, user.getId());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
        }
    
        @Override
        public User load(int id) {
            Connection connection = DBUtil.getConnection();
            
            String sql = "select * from t_user  where id = ?";
            
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            User user = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1, id);
                resultSet = preparedStatement.executeQuery();
                while(resultSet.next()) {
                    user = new User();
                    user.setId(id);
                    user.setUsername(resultSet.getString("username"));
                    user.setPassword(resultSet.getString("passwor"));
                    user.setNickname(resultSet.getString("nickname"));
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            return  user;
        }
    
        @Override
        public User load(String username) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public List<User> load() {
            Connection connection = DBUtil.getConnection();
        
            String sql = "select * from t_user ";
        
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
        
            List<User> users = new ArrayList<User>();
            User user = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                resultSet = preparedStatement.executeQuery();
                while(resultSet.next()) {
                    user = new User();
                    user.setId(resultSet.getInt("id"));
                    user.setUsername(resultSet.getString("username"));
                    user.setPassword(resultSet.getString("passwor"));
                    user.setNickname(resultSet.getString("nickname"));
                    users.add(user);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            return  users;
        }
    
    }
  • 相关阅读:
    杭电1075
    杭电1016深度搜索问题
    杭电1015
    stringstream
    向量的点乘和叉乘
    杭电1010
    FCKEditor2.6.3 配置
    JQuery实现全选 与 批量删除
    JQuery实现下拉框的选择 与当CheckBox为服务器控件时如何获取值的操作,实现全选与删除
    JS 对GridView的一些操作
  • 原文地址:https://www.cnblogs.com/youknownothing/p/7911299.html
Copyright © 2020-2023  润新知