用户管理系统

功能需求
功能名称 备注
注册管理 主要是系统供给对普通用户和管理员的信息注册。
用户信息管理 主要用于对用户信息的查询,修改和删除。
  • 注册管理


  • 用户信息管理


运行时效果图
1.JPG
2-1-查询.JPG
2-1普通用户.JPG
2-1-修改.JPG
2-2-查询.JPG
2-2-查询-根据ID查询.JPG
2-2-查询-根据用户名查询.JPG
2-2-查询-全查.JPG
2-2-删除用户.JPG
2-2-添加用户.JPG
2-2-修改用户.JPG
用户注册.JPG
项目工程
  • 工程目录


  • 案例代码

bean包:

package bean;

public class User {
    private Integer userid;
    private String username;
    private String password;
    private String email;
    private Integer qx;
    
    public User() {
        super();
    }

    public User(Integer userid, String username, String password, String email, Integer qx) {
        super();
        this.userid = userid;
        this.username = username;
        this.password = password;
        this.email = email;
        this.qx = qx;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getQx() {
        return qx;
    }

    public void setQx(Integer qx) {
        this.qx = qx;
    }

    public Integer getUserid() {
        return userid;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((email == null) ? 0 : email.hashCode());
        result = prime * result + ((password == null) ? 0 : password.hashCode());
        result = prime * result + ((qx == null) ? 0 : qx.hashCode());
        result = prime * result + ((userid == null) ? 0 : userid.hashCode());
        result = prime * result + ((username == null) ? 0 : username.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (email == null) {
            if (other.email != null)
                return false;
        } else if (!email.equals(other.email))
            return false;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (qx == null) {
            if (other.qx != null)
                return false;
        } else if (!qx.equals(other.qx))
            return false;
        if (userid == null) {
            if (other.userid != null)
                return false;
        } else if (!userid.equals(other.userid))
            return false;
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return userid + "   " + username + "   " + password + "   " + email
                + "   " + (1==qx?"管理员":"普通用户");
    }
    
    
}

dao包:

package dao;

import service.Menu;

public interface Item {
    Menu executeItem();
}

daoImpl包:

package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.AddMenu;

public class AddItem implements Item {
    User user;
    
    public AddItem() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public AddItem(User user) {
        super();
        this.user = user;
    }

    @Override
    public Menu executeItem() {
        // TODO Auto-generated method stub
        return new AddMenu(user);
    }

}
package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.DeleteMenu;

public class DeleteItem implements Item {
    User user;
    
    public DeleteItem() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public DeleteItem(User user) {
        super();
        this.user = user;
    }

    @Override
    public Menu executeItem() {
        // TODO Auto-generated method stub
        return new DeleteMenu(user);
    }

}
package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.LoginAdminMenu;

public class LoginAdminItem implements Item {
    User user;
    
    public LoginAdminItem() {

    }
    
    public LoginAdminItem(User user) {
        this.user = user;
    }

    @Override
    public Menu executeItem() {
        return new LoginAdminMenu(user);
    }

}
package daoImpl;

import dao.Item;
import service.Menu;
import serviceImpl.LoginMenu;



public class LoginItem implements Item {

    @Override
    public Menu executeItem() {
        
        return new LoginMenu();
    }

}
package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.LoginUserMenu;

public class LoginUserItem implements Item {
    User user;
    public LoginUserItem() {
                
    }
    
    public LoginUserItem(User user) {
        this.user = user;
    }

    @Override
    public Menu executeItem() {
        return new LoginUserMenu(user);
    }

}
package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.QueryAllMenu;

public class QueryAllItem implements Item {
    User user;
    
    public QueryAllItem() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public QueryAllItem(User user) {
        super();
        this.user = user;
    }

    @Override
    public Menu executeItem() {
        // TODO Auto-generated method stub
        return new QueryAllMenu(user);
    }

}
package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.QueryByIdMenu;

public class QueryByIdItem implements Item {
    User user;
    
    public QueryByIdItem() {
        super();
    }
    
    public QueryByIdItem(User user) {
        super();
        this.user = user;
    }

    @Override
    public Menu executeItem() {
        return new QueryByIdMenu(user);
    }

}
package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.QueryByNameMenu;

public class QueryByNameItem implements Item {
    User user;
    
    public QueryByNameItem() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public QueryByNameItem(User user) {
        super();
        this.user = user;
    }

    @Override
    public Menu executeItem() {
        
        return new QueryByNameMenu(user);
    }

}
package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.QueryMenu;

public class QueryItem implements Item {
    User user;
    
    public QueryItem() {
        
    }
    
    public QueryItem(User user) {
        this.user = user;
    }

    @Override
    public Menu executeItem() {
    
        return new QueryMenu(user);
    }

}
package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.QueryUserMenu;

public class QueryUserItem implements Item {
    User user;
    
    public QueryUserItem() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public QueryUserItem(User user) {
        super();
        this.user = user;
    }

    @Override
    public Menu executeItem() {
        return new QueryUserMenu(user);
    }

}
package daoImpl;

import dao.Item;
import service.Menu;
import serviceImpl.RegistMenu;

public class RegistItem implements Item {

    @Override
    public Menu executeItem() {
        return new RegistMenu();
    }

}
package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.UpdateMenu;

public class UpdateItem implements Item {
    User user;
    
    public UpdateItem() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public UpdateItem(User user) {
        super();
        this.user = user;
    }

    @Override
    public Menu executeItem() {
        
        return new UpdateMenu(user);
    }

}
package daoImpl;

import bean.User;
import dao.Item;
import service.Menu;
import serviceImpl.UpdateUserMenu;

public class UpdateUserItem implements Item {
    User user;
    
    public UpdateUserItem() {
        super();
        
    }
    
    public UpdateUserItem(User user) {
        super();
        this.user = user;
    }

    @Override
    public Menu executeItem() {
        
        return new  UpdateUserMenu(user);
    }

}
package daoImpl;

import dao.Item;
import service.Menu;
import serviceImpl.WelcomeMenu;

public class WelcomeItem implements Item {

    @Override
    public Menu executeItem() {
        return new WelcomeMenu();
    }

}

service包:

package service;

import dao.Item;

public interface Menu {
    void show();
    Item selectItem();
}

serviceImpl包:

package serviceImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

import bean.User;
import dao.Item;
import daoImpl.LoginAdminItem;
import service.Menu;
import util.CheckUsername;
import util.CheckUtil;
import util.DButil;

public class AddMenu implements Menu {
    User user;
    
    public AddMenu() {
        super();
        // TODO Auto-generated constructor stub
    }

    public AddMenu(User user) {
        super();
        this.user = user;
    }

    @Override
    public void show() {
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("请输入你的用户名:");
            String username = sc.nextLine();
            Connection conn = null;
            if(CheckUsername.nameSame(username)){
                System.out.println("用户名已被占用");
                continue;
            }
            System.out.println("请输入你的密码:");
            String password = sc.nextLine();
            System.out.println("请输入你的邮箱:");
            String email = sc.nextLine();
            if(!CheckUtil.checkEmail(email)){
                continue;
            }
            PreparedStatement ps;
            try {
                conn = DButil.getConn();
                ps = conn.prepareStatement("insert into user(username,password,email,qx) values(?,?,?,0)");
                ps.setString(1,username);
                ps.setString(2,password);
                ps.setString(3,email);
                int n = ps.executeUpdate();
                if(n>0){
                    System.out.println("添加用户成功");
                    conn.commit();
                }else{
                    System.out.println("添加用户失败");
                    conn.rollback();
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }catch (SQLException e) {
                e.printStackTrace();
            }
            
            break;
        }

    }

    @Override
    public Item selectItem() {
        // TODO Auto-generated method stub
        return new LoginAdminItem(user);
    }

}
package serviceImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

import bean.User;
import dao.Item;
import daoImpl.LoginAdminItem;
import service.Menu;
import util.CheckUsername;
import util.CheckUtil;
import util.DButil;

public class DeleteMenu implements Menu {
    User user;

    public DeleteMenu() {
        super();
        // TODO Auto-generated constructor stub
    }

    public DeleteMenu(User user) {
        super();
        this.user = user;
    }

    @Override
    public void show() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的用户的ID:");
        String userid = sc.nextLine();
        Connection conn = null;
        PreparedStatement ps;
        try {
            conn = DButil.getConn();
            ps = conn.prepareStatement("delete from user where userid = ?");
            ps.setString(1, userid);
            int n = ps.executeUpdate();
            if (n > 0) {
                System.out.println("删除成功");
                conn.commit();
            } else {
                System.out.println("删除失败");
                conn.rollback();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Item selectItem() {
        return new LoginAdminItem(user);
    }

}
package serviceImpl;

import java.util.Scanner;

import bean.User;
import dao.Item;
import daoImpl.AddItem;
import daoImpl.DeleteItem;
import daoImpl.LoginAdminItem;
import daoImpl.LoginUserItem;
import daoImpl.QueryItem;
import daoImpl.QueryUserItem;
import daoImpl.UpdateItem;
import daoImpl.UpdateUserItem;
import service.Menu;

public class LoginAdminMenu implements Menu {
    User user;
    
    public LoginAdminMenu() {
        
    }
    
    public LoginAdminMenu(User user) {
        this.user = user;
    }

    @Override
    public void show() {
        System.out.println("    欢迎登陆主体窗           ");
        System.out.println("=================");
        System.out.println(user.getUsername()+"你好:                 你的权限是:"+
                            (1==user.getQx()?"管理员":"普通用户"));
        System.out.println("=================");
        System.out.println("添加用户-----------1");
        System.out.println("删除用户-----------2");
        System.out.println("修改用户-----------3");
        System.out.println("查询用户-----------4");
        System.out.println("程序退出-----------5");
    }

    @Override
    public Item selectItem() {
        Scanner sc = new Scanner(System.in);
        Item it = null;
        String n = sc.nextLine();
        switch(n){
            case "1":
                it = new AddItem(user);
                break;
            case "2":
                it = new DeleteItem(user);
                break;
            case "3":
                it = new UpdateItem(user);
                break;
            case "4":
                it = new QueryItem(user);
                break;
            case "5":
                break;
            default:
                System.out.println("输入有误");
                it = new LoginAdminItem(user);
                break;
        }
        return it;
    }

}
package serviceImpl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import bean.User;
import dao.Item;
import daoImpl.LoginAdminItem;
import daoImpl.LoginUserItem;
import service.Menu;
import util.DButil;

public class LoginMenu implements Menu {
    User user;
    @Override
    public void show() {
        System.out.println("用户登录界面");
        System.out.println("=================");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的用户名:");
        String username = sc.nextLine();
        System.out.println("请输入你的密码:");
        String password = sc.nextLine();
        PreparedStatement ps =null;
        try {
            ps = DButil.getConn().prepareStatement("select userid,username,password,email,qx "
                    + "from user where username = ? and password = ?");
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                user = new User(rs.getInt("userid"),rs.getString("username"),
                        rs.getString("password"),rs.getString("email"),rs.getInt("qx"));
                System.out.println(user);
                System.out.println("登录成功");
            }else{
                System.out.println("用户名或密码错误");
            }
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Item selectItem() {
        if(1==user.getQx()){
            return new LoginAdminItem(user);
        }else{
            return new LoginUserItem(user);
        }
    }

}
package serviceImpl;

import java.util.Scanner;

import bean.User;
import dao.Item;
import daoImpl.LoginUserItem;
import daoImpl.QueryUserItem;
import daoImpl.UpdateUserItem;
import service.Menu;

public class LoginUserMenu implements Menu {
    User user;
    
    public LoginUserMenu() {
        super();
    }

    public LoginUserMenu(User user) {
        this.user = user;
    }

    @Override
    public void show() {
        System.out.println("    欢迎登陆主体窗           ");
        System.out.println("=================");
        System.out.println(user.getUsername()+"你好: ");
        System.out.println("你的权限是:"+(1==user.getQx()?"管理员":"普通用户"));
        System.out.println("=================");
        System.out.println("修改自己的信息-------1");
        System.out.println("查询自己的信息-------2");
        System.out.println("程序退出-----------3"); 
    }

    @Override
    public Item selectItem() {
        Scanner sc = new Scanner(System.in);
        Item it = null;
        String n = sc.nextLine();
        switch(n){
            case "1":
                it = new UpdateUserItem(user);
                break;
            case "2":
                it = new QueryUserItem(user);
                break;
            case "3":
                break;
            default:
                System.out.println("输入有误");
                it = new LoginUserItem(user);
                break;
        }
        return it;
    }

}
package serviceImpl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import bean.User;
import dao.Item;
import daoImpl.LoginAdminItem;
import daoImpl.QueryItem;
import service.Menu;
import util.DButil;

public class QueryAllMenu implements Menu {
    User user;
    
    public QueryAllMenu() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public QueryAllMenu(User user) {
        super();
        this.user = user;
    }

    @Override
    public void show() {
        PreparedStatement ps =null;
        User users = null;
        try {
            ps = DButil.getConn().prepareStatement("select userid,username,password,email,qx from user");
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                users = new User(rs.getInt("userid"),rs.getString("username"),
                        rs.getString("password"),rs.getString("email"),rs.getInt("qx")); 
                System.out.println(users);
                System.out.println("=================");
            }
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public Item selectItem() {
        return   new LoginAdminItem(user);
    }

}
package serviceImpl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import bean.User;
import dao.Item;
import daoImpl.LoginAdminItem;
import daoImpl.QueryItem;
import service.Menu;
import util.DButil;

public class QueryByIdMenu implements Menu {
    User user;
    
    public QueryByIdMenu() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public QueryByIdMenu(User user) {
        super();
        this.user = user;
    }

    @Override
    public void show() {
        User users = null;
        System.out.println("请输入要查询的ID");
        Scanner sc = new Scanner(System.in);
        PreparedStatement ps =null;
        Item it = null;
        String n = sc.nextLine();
        try {
            ps = DButil.getConn().prepareStatement("select userid,username,password,email,qx from user where userid = ?");
            ps.setString(1, n);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                users = new User(rs.getInt("userid"),rs.getString("username"),
                        rs.getString("password"),rs.getString("email"),rs.getInt("qx")); 
                System.out.println(users);
            }else{
                System.out.println("用户不存在!");
            }
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Item selectItem() {
        // TODO Auto-generated method stub
        return  new LoginAdminItem(user);
    }

}
package serviceImpl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import bean.User;
import dao.Item;
import daoImpl.LoginAdminItem;
import daoImpl.QueryItem;
import service.Menu;
import util.DButil;

public class QueryByNameMenu implements Menu {
    User user;
    
    public QueryByNameMenu() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public QueryByNameMenu(User user) {
        super();
        this.user = user;
    }

    @Override
    public void show() {
        User users = null;
        System.out.println("请输入要查询的用户名");
        Scanner sc = new Scanner(System.in);
        PreparedStatement ps =null;
        Item it = null;
        String n = sc.nextLine();
        String m = "%"+n+"%";
        try {
            ps = DButil.getConn().prepareStatement("select userid,username,password,email,qx from user where username like ?");
            ps.setString(1, m);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                users = new User(rs.getInt("userid"),rs.getString("username"),
                        rs.getString("password"),rs.getString("email"),rs.getInt("qx")); 
                System.out.println(users);
                System.out.println("=================");
            }
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public Item selectItem() {
        return  new LoginAdminItem(user);
    }

}
package serviceImpl;

import java.util.Scanner;

import bean.User;
import dao.Item;
import daoImpl.LoginUserItem;
import daoImpl.QueryAllItem;
import daoImpl.QueryByIdItem;
import daoImpl.QueryByNameItem;
import daoImpl.QueryItem;
import daoImpl.QueryUserItem;
import daoImpl.UpdateUserItem;
import service.Menu;

public class QueryMenu implements Menu {
    User user;
    
    public QueryMenu() {
    }
    
    public QueryMenu(User user) {
        this.user = user;
    }

    @Override
    public void show() {
        System.out.println("查询全部用户-------------1");
        System.out.println("根据ID查询用户-----------2");
        System.out.println("根据姓名查询用户----------3");
        System.out.println("请输入要做的操作:");
    }

    @Override
    public Item selectItem() {
        Scanner sc = new Scanner(System.in);
        Item it = null;
        String n = sc.nextLine();
        switch(n){
            case "1":
                it = new QueryAllItem(user);
                break;
            case "2":
                it = new QueryByIdItem(user);
                break;
            case "3":
                it = new QueryByNameItem(user);
                break;
            default:
                System.out.println("输入有误");
                it = new QueryItem(user);
                break;
        }
        return it;
    }

}
package serviceImpl;

import bean.User;
import dao.Item;
import daoImpl.LoginUserItem;
import service.Menu;

public class QueryUserMenu implements Menu {
    User user;
    
    public QueryUserMenu() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public QueryUserMenu(User user) {
        super();
        this.user = user;
    }

    @Override
    public void show() {
        System.out.println(user);
    }

    @Override
    public Item selectItem() {
        
        return new LoginUserItem(user);
    }

}
package serviceImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
import dao.Item;
import daoImpl.WelcomeItem;
import service.Menu;
import util.CheckUsername;
import util.CheckUtil;
import util.DButil;


public class RegistMenu implements Menu {
    
    @Override
    public void show() {
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("用户注册界面");
            System.out.println("=================");
            System.out.println("请输入你的用户名:");
            String username = sc.nextLine();
            Connection conn = null;
            if(CheckUsername.nameSame(username)){
                System.out.println("用户名已被占用");
                continue;
            }
            System.out.println("请输入你的密码:");
            String password = sc.nextLine();
            System.out.println("请输入你的邮箱:");
            String email = sc.nextLine();
            if(!CheckUtil.checkEmail(email)){
                continue;
            }
            PreparedStatement ps;
            try {
                conn = DButil.getConn();
                ps = conn.prepareStatement("insert into user(username,password,email,qx) values(?,?,?,0)");
                ps.setString(1,username);
                ps.setString(2,password);
                ps.setString(3,email);
                int n = ps.executeUpdate();
                if(n>0){
                    System.out.println("注册成功");
                    conn.commit();
                }else{
                    System.out.println("注册失败");
                    conn.rollback();
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }catch (SQLException e) {
                e.printStackTrace();
            }
            
            break;
        }

    }

    @Override
    public Item selectItem() {
        return new WelcomeItem();
    }

    
}
package serviceImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

import bean.User;
import dao.Item;
import daoImpl.LoginAdminItem;
import service.Menu;
import util.CheckUsername;
import util.CheckUtil;
import util.DButil;

public class UpdateMenu implements Menu {
    User user;
    
    public UpdateMenu() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public UpdateMenu(User user) {
        super();
        this.user = user;
    }

    @Override
    public void show() {
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("请输入要修改的用户的ID:");
            String userid = sc.nextLine();
            System.out.println("请输入要修改的用户的用户名:");
            String username = sc.nextLine();
            Connection conn = null;
            if(CheckUsername.nameSame(username)){
                System.out.println("用户名已存在,请重新输入");
                continue;
            }
            System.out.println("请输入要修改的用户的密码:");
            String password = sc.nextLine();
            System.out.println("请输入要修改的用户的的邮箱:");
            String email = sc.nextLine();
            if(!CheckUtil.checkEmail(email)){
                continue;
            }
            System.out.println("请输入要修改的用户的的权限:");
            String qx = sc.nextLine();
            PreparedStatement ps;
            try {
                conn = DButil.getConn();
                ps = conn.prepareStatement("update user set username = ?, password=?, email=?, qx=? where userid = ?");
                ps.setString(1,username);
                ps.setString(2,password);
                ps.setString(3,email);
                ps.setString(4,qx);
                ps.setString(5,userid);
                int n = ps.executeUpdate();
                if(n>0){
                    System.out.println("修改成功");
                    conn.commit();
                }else{
                    System.out.println("修改失败");
                    conn.rollback();
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }catch (SQLException e) {
                e.printStackTrace();
            }
            
            break;
        }

    }

    @Override
    public Item selectItem() {
        // TODO Auto-generated method stub
        return new LoginAdminItem(user);
    }

}
package serviceImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

import bean.User;
import dao.Item;
import daoImpl.LoginUserItem;
import service.Menu;
import util.CheckUsername;
import util.CheckUtil;
import util.DButil;

public class UpdateUserMenu implements Menu {
    User user;
    
    public UpdateUserMenu() {
        super();
        
    }
    
    public UpdateUserMenu(User user) {
        super();
        this.user = user;
    }

    @Override
    public void show() {
        Scanner sc = new Scanner(System.in);
        while(true){
            Connection conn = null;
            System.out.println("你现在的信息是:");
            System.out.println(user);
            System.out.println("=================");
            System.out.println("请输入要修改的用户名:");
            String username = sc.nextLine();
            if((!user.getUsername().equals(username)) && CheckUsername.nameSame(username)){
                System.out.println("用户名已存在,请重新输入");
                continue;
            }
            System.out.println("请输入要修改的密码:");
            String password = sc.nextLine();
            System.out.println("请输入要修改的邮箱:");
            String email = sc.nextLine();
            if(!CheckUtil.checkEmail(email)){
                continue;
            }
            PreparedStatement ps;
            try {
                conn = DButil.getConn();
                ps = conn.prepareStatement("update user set username = ?, password=?, email=? where userid = ?");
                ps.setString(1,username);
                ps.setString(2,password);
                ps.setString(3,email);
                ps.setInt(4, user.getUserid()); 
                int n = ps.executeUpdate();
                if(n>0){
                    System.out.println("修改成功");
                    conn.commit();
                }else{
                    System.out.println("修改失败");
                    conn.rollback();
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }catch (SQLException e) {
                e.printStackTrace();
            }
            break;
        }

    }

    @Override
    public Item selectItem() {
        return new LoginUserItem();
    }

}
package serviceImpl;

import java.util.Scanner;

import dao.Item;
import daoImpl.LoginItem;
import daoImpl.RegistItem;
import daoImpl.WelcomeItem;
import service.Menu;

public class WelcomeMenu implements Menu{

    @Override
    public void show() {
        System.out.println("欢迎使用用户管理系统");
        System.out.println("=================");
        System.out.println("用户登录-----------1");
        System.out.println("用户注册-----------2");
        System.out.println("用户退出-----------3");
    }

    @Override
    public Item selectItem() {
        Scanner sc = new Scanner(System.in);
        Item it = null;
        String n = sc.nextLine();
        switch(n){
            case "1":
                it = new LoginItem();
                break;
            case "2":
                it = new RegistItem();
                break;
            case "3":
                break;
            default:
                System.out.println("输入有误");
                it = new WelcomeItem();
                break;
        }
        return it;
    }

}

test包:

package test;

import dao.Item;
import service.Menu;
import serviceImpl.WelcomeMenu;

public class Test {
    public static void main(String[] args) {
        Menu m = new WelcomeMenu();
        while(m!=null){
            m.show();
            Item it = m.selectItem();
            
            if(it != null) {
                m = it.executeItem();
            }else{
                m = null;
            }
        }
    }
}

util包:

package util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CheckUsername {
    //判断用户名是否重复,如果重复,返回true.
            public static  boolean nameSame(String username){
                boolean flag = true;
                PreparedStatement ps;
                Connection conn = null;
                try {
                    conn = DButil.getConn();
                    ps = conn.prepareStatement("select userid,username,password,email,qx from user where username = ?");
                    ps.setString(1,username);
                    ResultSet rs = ps.executeQuery();
                    if(rs.next()){
                        flag = true;
                    }else{
                        flag = false;
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return flag;
            }
}
package util;

public class CheckUtil {
    public static boolean checkEmail(String email){
        boolean flagEmail = true;
        String []mailarr = email.split("@");
        char arr[] = mailarr[0].toCharArray();
        char c = email.charAt(0);
        if((c>=97&&c<=122) ||(c>=65&&c<=90)){
            if((email.endsWith(".com")||email.endsWith(".cn"))){                
                if(mailarr.length==2){
                    if(mailarr[1].indexOf(".")==mailarr[1].lastIndexOf(".")){
                        for(int i =1 ;i<arr.length;i++){
                            if(!((arr[i]>=97&&arr[i]<=122) ||(arr[i]>=65&&arr[i]<=90)||Character.isDigit(arr[i]))){
                                flagEmail = false;
                                System.out.println("@前只能由数字或者字母组成");
                              break;    
                            }
                        }
                    }else{
                        flagEmail = false;
                        System.out.println(".符号在@之后有且仅能有一个");
                    }
                }else{
                    flagEmail = false;
                    System.out.println("@规定只能有一个");
                }               
            }else{
                flagEmail = false;
                System.out.println("结尾只能是.com或者.cn");
            }
            
        }else{
            flagEmail = false;
            System.out.println("首字符应该是字母");
        }
        return flagEmail;
    }
}
package util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DButil {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;
    static{
        try {
            Properties pop = new Properties();
            InputStream is  =  null;
            is = DButil.class.getClassLoader().getResourceAsStream("db.properties");
            try {
                pop.load(is);
            } catch (IOException e) {
                
                e.printStackTrace();
            }
            driver = pop.getProperty("driver");
            url = pop.getProperty("url");
            username = pop.getProperty("username");
            password = pop.getProperty("password");
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            System.out.println("系统错误");
        }
    }
    public static Connection getConn() throws ClassNotFoundException, SQLException{     
        //加载驱动
        Connection conn = DriverManager.getConnection(url, username, password);
        conn.setAutoCommit(false);
        return conn;
    } 
}

db.properties文件:

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost/user?useSSL=FALSE&serverTimezone=UTC
username=root
password=root
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,193评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,306评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,130评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,110评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,118评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,085评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,007评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,844评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,283评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,508评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,667评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,395评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,985评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,630评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,797评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,653评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,553评论 2 352

推荐阅读更多精彩内容