• 课堂练习,增加信息


    ---恢复内容开始---

    1.程序设计思想

    用jsp编写登录界面,addput中加入一些java方法

    2

    package com.jaovo.msg.dao;
    
    import java.util.List;
    
    import com.jaovo.msg.model.User;
    
    public interface IUserDao {
        public void add(User user);
        public void delete(int id);
    package com.jaovo.msg.Util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class DBUtil {
        
        public  static  Connection getConnection() {
            try {
                //1 鍔犺浇椹卞姩
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String user = "root";
            String password = "20163558";
            String url = "jdbc:mysql://localhost:3306/jaovo_msg";
            Connection connection = null;
            try {
                //2 鍒涘缓閾炬帴瀵硅薄connection
                 connection = DriverManager.getConnection(url,user,password);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return connection;
        }
        
        //鍏抽棴璧勬簮鐨勬柟娉�
        public static void close(Connection connection ) {
            try {
                if (connection != null) {
                    connection.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(PreparedStatement preparedStatement ) {
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(ResultSet resultSet ) {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    
    
    package com.jaovo.msg.model;
    
    public class User {
        
        private int id;
        private String  username;
        private String  nickname;
        private String  password;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getNickname() {
            return nickname;
        }
        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
    }
    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();
            //鍑嗗�sql璇�彞
            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();
            //鍑嗗�sql璇�彞
            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();
            //鍑嗗�sql璇�彞
            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("password"));
                    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();
            //鍑嗗�sql璇�彞
            String sql = "select * from t_user ";
            //鍒涘缓璇�彞浼犺緭瀵硅薄
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            //闆嗗悎涓�彧鑳芥斁鍏�ser瀵硅薄
            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("password"));
                    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;
        }
    
    }
    public void update(User user);
        public User load(int id);
        public User load(String username);
        public List<User> load();
        
    }
    package com.jaovo.msg.Util;
    
    public class UserException extends RuntimeException{
    
        public UserException() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
            // TODO Auto-generated constructor stub
        }
    
        public UserException(String message, Throwable cause) {
            super(message, cause);
            // TODO Auto-generated constructor stub
        }
    
        public UserException(String message) {
            super(message);
            // TODO Auto-generated constructor stub
        }
    
        public UserException(Throwable cause) {
            super(cause);
            // TODO Auto-generated constructor stub
        }
        
    }

    3.

    文学使思想充满血与肉,他比科学和哲学更能给予思想以巨大的明确性和说明性。
  • 相关阅读:
    【转】Android开发实践:自定义带消息循环(Looper)的工作线程
    【转】 解决IllegalStateException: Can not perform this action after onSaveInstanceState
    【转】Fresco之强大之余的痛楚
    【转】Android 防破解技术简介
    改进版本号的精确数据权限定义和实现
    明天是我的生日,写给24岁的自己
    javascrip cookie
    Servlet -- 跳转到页面后的绝对路径与相对路径的问题
    JAVA訪问URL
    跨浏览器resize事件分析
  • 原文地址:https://www.cnblogs.com/zpsblog/p/7911306.html
Copyright © 2020-2023  润新知