项目名称:用户管理系统

User

package com.neu.entity;

public class User {
     private Integer id;
     private String username;
     private String password;
     private String email;
     private String power;
     
	public User() {
		super();
		
	}
	public User(Integer id, String username, String password, String email, String power) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.email = email;
		this.power = power;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", power="
				+ power + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((power == null) ? 0 : power.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 (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (power == null) {
			if (other.power != null)
				return false;
		} else if (!power.equals(other.power))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	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 String getPower() {
		return power;
	}
	public void setPower(String power) {
		this.power = power;
	}
	
     
}


JDBCUtil

package com.neu.dao;

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

public class JDBCUtil {
    private static String driverClassName = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/mydb";
    private static String username = "root";
    private static String password = "root";    
    //加载驱动
    static {
    	try {
			Class.forName(driverClassName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    }
    //得到连接
    public static Connection getConnection() throws SQLException{
	   Connection connection = DriverManager.getConnection(url,username,password);
	   
	   return connection;
   }
    //关闭连接
    public static void closeConnection(Connection connection) throws SQLException{
	   connection.close();
   }
    
   //执行增删改操作
    public static int executeUpdate(String sql,Object[] params) throws SQLException{
    	Connection connection = getConnection(); 
    	
    	PreparedStatement s = connection.prepareStatement(sql);    	
    	
        if(params!= null){
    	for(int i = 0;i<params.length;i++){
    		s.setObject(i+1, params[i]);    		
    	}
   }
    
    int n = s.executeUpdate();
    
    s.close();
    connection.close();
    
    return n ;
    
   }   

     //执行查询操作
    public static ResultSet executeQuery(Connection connection,String sql,Object[] params) throws SQLException{
     //Connection connection = getConnection(); 	
    	PreparedStatement s = connection.prepareStatement(sql);    	
    	
        if(params!= null){
    	for(int i = 0;i<params.length;i++){
    		s.setObject(i+1, params[i]);    		
    	}
   }    
        ResultSet rs = s.executeQuery();
 
        return rs ;
    
   }   
}

UserDaoImpl

package com.neu.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.neu.entity.User;

public class UserDaoImpl {
	
	public User login(String username, String password) throws SQLException {
		String sql = "select * from myuser2 where username = ? and password = ? ";
		Connection connection = JDBCUtil.getConnection();
		Object[] params = {username,password};
		ResultSet rs = JDBCUtil.executeQuery(connection, sql, params);
		User user = null;
		
		if(rs.next()){
			int id = rs.getInt("id");			
			String username1 = rs.getString("username");						
			String password1 = rs.getString("password");			
			String email1 = rs.getString("email");			
			int role1 = rs.getInt("role");
			user = new User(id,username,password, null, null);
		}
		rs.close();
		JDBCUtil.closeConnection(connection);
		
		
		return user;
	}
	//
	public User getbyid(int id) throws SQLException {
		String sql="select * from myuser2 where id=?";
		Connection connection = JDBCUtil.getConnection();
		ResultSet rs = JDBCUtil.executeQuery(connection, sql,new Object[]{id});
		User user=null;
		while(rs.next()) {
			int id1=rs.getInt("id");
			String username =rs.getString("username");
			String password=rs.getString("password");
			String email=rs.getString("email");
			String power =rs.getString("power");
			user= new User(id1, username, password, email, power);
			
		}
		rs.close();
		JDBCUtil.closeConnection(connection);
		return user;
			
		}

	//增加
     public int insert(User user) throws SQLException{
    	String sql = "insert into myuser2 values(null,?,?,?,defult)";
//    	Connection connection = JDBCUtil.getConnection();
    	Object[] params= new Object[]{
    		user.getUsername(),
    		user.getPassword(),
    		user.getPower()    		
    	};
    	int n = JDBCUtil.executeUpdate(sql, params);
		return n;
     }
	//删除
     public int delete(int id) throws SQLException {
			String sql="delete from myuser2 WHERE id=?;";
			int n = JDBCUtil.executeUpdate(sql, new Object[]{id});
			return n;
			
		}
     //修改
	 public int update(User user) throws SQLException {
			String sql="update myuser2 set username=?,password=?,email=?,power=? and where id=? ";
			Object[] params= new Object[]{
		    		user.getUsername(),
		    		user.getPassword(),
		    		user.getPower()    		
		    	};
			int n = JDBCUtil.executeUpdate(sql, params);
			return n;
			
		}

	 public boolean  login4(String username,String password) throws SQLException {
			String sql="select * from myuser2 where username=? and password = ?";
			Connection connection = JDBCUtil.getConnection();
		    System.out.println("xxx");
			ResultSet rs = JDBCUtil.executeQuery(connection, sql,new Object[]{username,password});
			User user=null;
			boolean x=false;
			while(rs.next()) {
				int id=rs.getInt("id");
				String username1 =rs.getString("username");
				String password1=rs.getString("password");
				if(username1!=(" ")|password1!=(" ")){
					System.out.println(username1+password1);
					x=true;
					}else{
						x=false;
					}
				}
				//user= new User(id, name, password, email, power);
			
				return x;
			}
	 //全部查询(遍历)
	 public List<User> getAll() throws SQLException{
			String sql="select * from myuser2 order by id";
			Connection connection = JDBCUtil.getConnection();
			ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
			List<User> list=new ArrayList<>();
			User user=null;
			while(rs.next()) {
				int id=rs.getInt("id");
				String username =rs.getString("username");
				String password=rs.getString("password");
				String email=rs.getString("email");
				String power =rs.getString("power");
				user = new User(id,username,password,email, power);
				list.add(user);
			}
			rs.close();
			JDBCUtil.closeConnection(connection);
			return list;
		}


	 //模糊查询
	 public User getusername(String na) throws SQLException {
			String sql="select * from myuser2 where username like ?";
			Connection connection = JDBCUtil.getConnection();
			ResultSet rs = JDBCUtil.executeQuery(connection, sql,new Object[]{na});
			User user=null;
			while(rs.next()) {
				int id=rs.getInt("id");
				String username =rs.getString("username");
				String password=rs.getString("password");
				String email=rs.getString("email");
				String power =rs.getString("power");
				user= new User(id, username, password, email, power);
				
			}
			rs.close();
			JDBCUtil.closeConnection(connection);
			return user;
		}
	 
	 public User getpower(int id) throws SQLException {
			String sql="select *  from myuser2 where id=?";
			Connection connection = JDBCUtil.getConnection();
			ResultSet rs = JDBCUtil.executeQuery(connection, sql,new Object[]{id});
			User user=null;
			while(rs.next()) {
				int id1=rs.getInt("id");
				String username =rs.getString("username");
				String password=rs.getString("password");
				String email=rs.getString("email");
				String power =rs.getString("power");
				user= new User(id1, username, password, email, power);
				
			}
			rs.close();
			JDBCUtil.closeConnection(connection);
			return user;
				
			}
}

StartSystem

package com.neu.ui;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

import com.neu.dao.UserDaoImpl;
import com.neu.entity.User;

public class StartSystem {
	public static void main(String[] args) throws SQLException {
		String name;
		String password;
		String email;
		User user=null;
		UserDaoImpl neus=new UserDaoImpl();
		boolean b = true;
		while(b){
		System.out.println("欢迎使用neusoft系统");
		System.out.println("================");
		System.out.println("登陆系统=========1");
		System.out.println("注册系统=========2");
		System.out.println("退出系统=========3");
		Scanner s=null;
		int x=0;
		try {
			s = new Scanner(System.in);
			x = s.nextInt();
		} catch (Exception e) {
			System.out.println("输入的值不是数字");
		}
		switch(x){
		case 1:
			System.out.print("请输入账号");
			name=s.next();
			System.out.print("请输入密码");
			password=s.next();
			User p=neus.login(name, password);
			boolean p1 = true;
			if(p1==true){
				System.out.println("登陆成功");
				user=neus.login(name,password);
				System.out.println("欢迎登陆主窗体");
				System.out.println("尊敬的"+user.getUsername()+"你好\t\t您的权限为"+user.getId());
			if(user.getId().equals("管理员")){
				System.out.println("管理员");
				boolean g=true;
				while(g){
					System.out.println("=============");
					System.out.println("添加用户======1");
					System.out.println("删除用户======2");
					System.out.println("修改用户======3");
					System.out.println("查询用户======4");
					System.out.println("退出程序======5");
					int f=s.nextInt();
					switch(f){
					case 1:
						System.out.print("请输入账号");
						name=s.next();
						System.out.print("请输入密码");
						password=s.next();
						System.out.println("请输入邮箱");
						email=s.next();
						User neu= new User(null, name, password, email, null);
						int z=neus.insert(neu);
						if(z==1){
							System.out.println("添加用户成功,请牢记相关信息");
							
						}else{
							System.out.println("发生未知错误,注册失败");
						}
						continue;
					case 2:
						System.out.println("输入需要删除的id");
						int sc=s.nextInt();
						int c=neus.delete(sc);
						if(c==1){
							System.out.println("删除成功");
						}else{
							System.out.println("删除失败");
						}
						break;
					case 3:
						
						
						System.out.println("请输入要修改id");
						int  u=s.nextInt();
						User nue=neus.getbyid(u);
						if(nue.getId().equals("管理员")){
							System.out.println("若想修改管理员的权限请联系数据库管理员");
							break;
						}
						System.out.println("请输入要修改账户");
						String  u1=s.next();
						System.out.println("请输入要修改密码");
						String  u2=s.next();
						System.out.println("请输入要修改邮箱");
						String  u3=s.next();
						System.out.println("请输入要修改权限");
						String  u4=s.next();
				if(u4.equals("管理员")){
						System.out.println("管理员具有修改用户信息的权限确定保存修改?");
						System.out.println("是======1");
						System.out.println("否======2");
						int pd=s.nextInt();
					switch(pd){
					case 1:
						System.out.println("正在保存修改");
						User neu1 = new User(u, u1, u2, u3, u4);
								int up=neus.update(neu1);
								if(up==1){
									System.out.println("更改已被保存");
									}
								else{
									System.out.println("发生未知错误请联系管理员");
								}
							break;	
					case 2:
					System.out.println("已舍弃该修改");
					break;
					}
					
					}
					break;
				case 4:
					System.out.println("请输入查询的方式");
					System.out.println("查询所有用户==1");
					System.out.println("根据id查询==2");
					System.out.println("根据姓名查询==3");
					int cx=s.nextInt();
					switch(cx){
					case 1:
						List<User> li=neus.getAll();
						for(User j:li){
						System.out.println(j);
						}
						break;
					case 2:
						System.out.println("请输入需要查询的id");
						int x1=s.nextInt();
					User n=neus.getbyid(x1);
					System.out.println("id为"+n.getId()+"账号为"+n.getUsername()+"密码为"+n.getPassword()+"邮箱为"+n.getEmail()+"权限为"+n.getPower());
					break;
					case 3:
						System.out.println("请输入要查询的账户(支持模糊查询)");
						String na=s.next();
						User n1=neus.getusername("%"+na+"%");
						System.out.println("id为"+n1.getId()+"账号为"+n1.getUsername()+"密码为"+n1.getPassword()+"邮箱为"+n1.getEmail()+"权限为"+n1.getPower());
						break;
					}
					case 5:
					g=false;
					
					}
				}
				}else{
					User neu3= neus.getusername(null);
					System.out.println("普通用户");
					System.out.println("=============");
					System.out.println("修改个人信息======1");
					System.out.println("查询个人信息======2");
					System.out.println("退出程序======3");
					int g=s.nextInt();
					switch(g){
					case 1:
						System.out.println("请输入修改后的账户名");
						String  u1=s.next();
						System.out.println("请输入要修改密码");
						String  u2=s.next();
						System.out.println("请输入要修改邮箱");
						String  u3=s.next();
						User neu4 = new User(null, u1, u2, u3, null);
					int jg=neus.update(neu4);
					if(jg==1){
					System.out.println("修改个人信息成功");
					}else{
					System.out.println("发生未知错误请联系管理员");
					}
					break;
					case 2:
					User n2=neus.getbyid(neu3.getId());
					System.out.println("id为"+n2.getId()+"账号为"+n2.getUsername()+"密码为"+n2.getPassword()+"邮箱为"+n2.getEmail()+"权限为"+n2.getPower());
					break;
					case 3:
						break;
					}
				}
				break;
			}else{
				System.out.println("未搜索到相关信息,请注册后尝试");
				continue;
			}
			
		case 2:
			System.out.print("请输入账号");
			name=s.next();
			System.out.print("请输入密码");
			password=s.next();
			System.out.println("请输入邮箱");
			email=s.next();
			User neu= new User(null, name, password, email, null);
			int z=neus.insert(neu);
			if(z==1){
				System.out.println("注册成功,请牢记账号密码");
				
			}else{
				System.out.println("发生未知错误,注册失败");
			}
			continue;
		case 3:
			System.out.println("正在退出系统");
			break;
		default:
			System.out.println("输入有误");
			break;
		}
		
		b=false;
		
	    }
	  }
	}

            

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值