2019.1.3 JDBC项目,项目名称 :用户管理系统

项目名称 :用户管理系统
开发环境 : 1. JDK 6.0
2. Eclipse 3.3或MyEclipse6.0
3. Oracle 10g
软件功能 :

  1. 注册功能: 用户可以注册成本软件的用户。(注:权限是 普通用户)
    如下图:
    2019.1.3 JDBC项目,项目名称 :用户管理系统_第1张图片
    . 登录功能: 用户输入用户名和密码。断用户是否存在
    存在:提取用户的权限
    权限如果是:管理员的话则显示如下图
    2019.1.3 JDBC项目,项目名称 :用户管理系统_第2张图片
    权限如果是普通用户的话则显示如下图

2019.1.3 JDBC项目,项目名称 :用户管理系统_第3张图片
3. 普通用户的修改功能
普通用户可以修改自己的name ,pass ,mail
不能修改权限和ID。
运行效果如下图:
2019.1.3 JDBC项目,项目名称 :用户管理系统_第4张图片
4. 普通用户的查询功能
普通用户可以查询自己的详细信息
运行效果如下图:

5.管理员的添加功能
管理员的添加和普通用户的注册一样。Id自动分配。权限是普通用户:
运行效果如图:
2019.1.3 JDBC项目,项目名称 :用户管理系统_第5张图片

  1. 管理员的修改功能:
    管理员可以修改任何人的信息。(注:可以把普通用户修改成管理员)根据id 修改某个用户信息
    运行效果如图:

  2. 管理员的查询功能:
    管理员可以有三个查询方式:
    运行效果如图:

  3. 全部查询:
    运行效果如图:

  4. 根据id查询某一个用户:
    运行效果如图:

  5. 根据用户名查询:支持模糊查询
    运行效果如图:

  6. 管理员的删除功能:
    运行效果如图:

User类

package com.neu.entity;

public class User {

	private Integer id;
	private String username;
	private String password;
	private String email;
	private Integer role;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(Integer id, String username, String password, String email, Integer role) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.email = email;
		this.role = role;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", role="
				+ role + "]";
	}
	@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 + ((role == null) ? 0 : role.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 (role == null) {
			if (other.role != null)
				return false;
		} else if (!role.equals(other.role))
			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 Integer getRole() {
		return role;
	}
	public void setRole(Integer role) {
		this.role = role;
	}
	public User(Integer id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}
	public User(String username, String password, String email) {
		super();
		this.username = username;
		this.password = password;
		this.email = email;
	}
}

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 com.neu.entity.User;




public class UserDaoImpl {
    //检验用户名密码是否正确,若正确,返回用户对象,不正确,返回null
	public User login(String username,String password) throws SQLException {
		
		String sql = "select * from myuser where username = ? and password = ?  ";
	
	    Connection connection = JDBCUtil.getConnertion();
	    
	    Object[]  params = {username,password};
	    
	    ResultSet rs = JDBCUtil.executeQuery(connection, sql, params);
	    
	    User user = null;
	    
	    if(rs.next()) {
	    	int id  = rs.getInt("id");
	    	
	    	String  email = rs.getString("email");
            int role = rs.getInt("role");
	    	user = new User(id,username,password,email,role);
	    }
	    rs.close();
	    JDBCUtil.closeConnection(connection);
	   
	    return user;
	}
	//用户注册
	public int insert(User user) throws SQLException {
		String sql = "insert into myuser values(null,?,?,?,default)";
		Object[] params = new Object[] {
				user.getUsername(),
				user.getPassword(),
				user.getEmail()	
		};

		int n = JDBCUtil.executeUpdate(sql, params);
		
		return n;
		
	}
	//管理员查询全部用户
	public ArrayList<User> getall() throws SQLException{
		String sql = "select * from myuser order by id";
		
		Connection connection = JDBCUtil.getConnertion();
		
        ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
		
		
		 ArrayList<User> list = new ArrayList<>();
		   User user = null;
		   Integer id;
		   String username;
		   String password;
		   String email;
		   Integer role;
		while(rs.next()) {
			 id = rs.getInt("id");
			 username = rs.getString("username");
			 password = rs.getString("password");
			 email = rs.getString("email");
			 role = rs.getInt("role");
			 user = new User(id,username,password,email,role);
			list.add(user);
		}
		rs.close();
		JDBCUtil.closeConnection(connection);
		return list;
	}
	//按id查询
    public User getById(int id) throws SQLException {
    	String sql  = "select * from myuser where id = ?";
    	
    	Connection connection = JDBCUtil.getConnertion();
    	
    	ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {id});
    	
    	User user = null;
    	Integer id2;
		String username;
		String password;
		String email;
		Integer role;
		   while(rs.next()) {
			   id2 = rs.getInt("id");
			   username = rs.getString("username");
			   password = rs.getString("password");
			   email = rs.getString("email");
			   role = rs.getInt("role");
			   
			 user = new User(id2,username,password,email,role);
		   }
    	rs.close();
    	JDBCUtil.closeConnection(connection);
    	return user;
      }
	//根据name查询
    public User getName(String na) throws SQLException {
    	String sql  = "select * from myuser where username like ?";
    	
    	Connection connection = JDBCUtil.getConnertion();
    	
    	ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {"%"+na+"%"});
    	
    	User user = null;
    	Integer id;
		String username;
		String password;
		String email;
		Integer role;
		   while(rs.next()) {
			   id = rs.getInt("id");
			   username = rs.getString("username");
			   password = rs.getString("password");
			   email = rs.getString("email");
			   role = rs.getInt("role");
			   
			 user = new User(id,username,password,email,role);
		   }
    	rs.close();
    	JDBCUtil.closeConnection(connection);
    	return user;
      }
	
	//管理员修改用户
    public int update(User user) throws SQLException {
    	String sql = "update myuser set username = ?,password = ?,email = ?,role = ? where id = ?";
    	
    	Object[] params = new Object[] {
    			
    			user.getUsername(),
				user.getPassword(),
				user.getEmail(),
				user.getRole(),
				user.getId()
		};
    	
    	int n = JDBCUtil.executeUpdate(sql, params);
    	return n; 
    }
	//管理员删除用户
    public int delete(int id) throws SQLException {
    	String sql = "delete from myuser where id = ?";
 
    	int n = JDBCUtil.executeUpdate(sql, new Object[] {id});
    	return n; 	
    }
	
	//普通用户修改
    public int update2(User user2) throws SQLException {
    	String sql = "update myuser set username = ?,password = ?,email = ? where id = ?";
    	
    	Object[] params = new Object[] {
    			
    			user2.getUsername(),
				user2.getPassword(),
				user2.getEmail(),
				user2.getId()
		};
    	
    	int n = JDBCUtil.executeUpdate(sql, params);
    	return n; 
    }
	
	
}

主界面类

package com.neu.ui;

import java.sql.SQLException;
import java.util.ArrayList;

import java.util.Scanner;

import javax.swing.plaf.synth.SynthOptionPaneUI;

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

public class StartSystem {
	 static UserDaoImpl userDao = new UserDaoImpl();
	  static Scanner input = new Scanner(System.in);
	public static void main(String[] args) throws SQLException {
		boolean xx=true;
		while(xx) {
			System.out.println("欢迎使用neusoft的用户管理系统");
			System.out.println("======================");
			System.out.println("用户登录----------------1");
			System.out.println("用户注册----------------2");
			System.out.println("退出程序----------------3");
			Scanner input=null;
			int n=0;
			try {
				input = new Scanner(System.in);
				n = input.nextInt();
			} catch (Exception e) {
				System.out.println("输入的值不是数字");
			}
				
			switch(n) {
			case 1:
				//如果方法写在这里不美观,代码量太大,用个方法写在下面
				//用户登录
				login();
				continue;
			case 2:
				//用户注册
				register();
				continue;
			case 3:
				System.out.println("程序退出!");
				xx=false;
				break;
			
			}			
		}
	
	}
     //用户登录
	 private static void login() throws SQLException {
		System.out.println("请输入您的用户名:");
		String username = input.next();
		System.out.println("请输入您的密码:");
		String password = input.next();
		UserDaoImpl userDao = new UserDaoImpl();
		User user = userDao.login(username, password);//判断用户是否正确
		if(user != null) {
			System.out.println("登录成功。。。");
			System.out.println("======================");
			System.out.println("欢迎登录主窗体");
			System.out.println(username+"您好         "+"您的权限是:"+(user.getRole()==2?"管理员":"普通用户"));
			System.out.println("======================");
			if(user.getRole().equals(2)) {
				 boolean nn =true;
				 while(nn) {
				 System.out.println("添加用户---------------1");   
				 System.out.println("删除用户---------------2");   
				 System.out.println("修改用户---------------3");
				 System.out.println("查询用户---------------4"); 
				 System.out.println("程序退出---------------5"); 
				 int n = input.nextInt();
				 switch(n) {
				 case 1:
					    System.out.println("请输入用户名:");
						String username1 = input.next();
						System.out.println("请输入密码:");
						String password1 = input.next();
						System.out.println("请输入您的邮箱:");
						String email = input.next();
						
					    User u = new User(null,username1,password1,email,null);
						int n1 = userDao.insert(u);
						if(n1 ==1) {
							System.out.println("添加成功");
						}else{
							System.out.println("添加失败,用户已存在");
						}
				        break;
				 case 2:
					    System.out.println("请输入需要删除的用户的ID号码:");
					    int n2 = input.nextInt();
					    userDao.delete(n2);
						System.out.println("删除用户成功");

					 break;
				 case 3:
					    System.out.println("请输入要修改的用户的ID号码:");
					    int id = input.nextInt();
					    System.out.println("请输入要修改的用户的用户名:");
					    String username2 = input.next();
					    System.out.println("请输入要修改的用户的密码:");
					    String password2 = input.next();
					    System.out.println("请输入要修改的用户的邮箱:");
					    String email1 = input.next();
					    System.out.println("请输入要修改的用户的权限(管理员/普通用户):");
					    int role = input.nextInt();
					    User user1 = new User(id,username2,password2,email1,role);

						int s =userDao.update(user1);
						if(s == 1) {
							System.out.println("修改成功");
						}else{
							System.out.println("修改失败");
						}
					 break;
					 
				 case 4:
					 
					 System.out.println("查询全部用户------------1");
					 System.out.println("根据ID查询用户-----------2");
					 System.out.println("根据姓名查询用户-------------3");
					 System.out.println("请输入要做的操作");
					 int n3 = input.nextInt();
					 switch(n3) {
					 case 1:
					     ArrayList<User> list = new ArrayList<>();
							list = userDao.getall();
							for(User x :list) {
								System.out.println(x.getId()+"  "+x.getUsername()+"  "+x.getPassword()+"  "+x.getEmail()+"  "+(x.getRole()==2?"管理员":"普通用户"));
							    System.out.println("======================");
							}
						 
						 break;
					 case 2:
						 System.out.println("请输入要查询的ID");
						 int n4 = input.nextInt();
						   User user2 =userDao.getById(n4);
					            System.out.println(user2.getId()+"  "+user2.getUsername()+"  "+user2.getPassword()+"  "+user2.getEmail()+"  "+(user2.getRole()==2?"管理员":"普通用户"));	 
						        System.out.println("======================");
						  
						  break;
					 case 3:
						 System.out.println("请输入要查询的用户名(支持模糊查询)");
						 String  n5 = input.next();
						   User user3 =userDao.getName(n5);
						   if(user3 != null) {
						        System.out.println(user3.getId()+"  "+user3.getUsername()+"  "+user3.getPassword()+"  "+user3.getEmail()+"  "+(user3.getRole()==2?"管理员":"普通用户"));	 
					            System.out.println("======================"); 
						   }else {System.out.println("此用户不存在");}
					     break;
					 }
					 break;
				 case 5:
					 System.out.println("程序退出");
					 nn = false;
					 break;
				 }	
			 } //循环括号
			
			}else {
				boolean mm = true;
				while(mm) {
				System.out.println("======================"); 
			    System.out.println("修改自己的信息------------1");
			    System.out.println("查询自己的信息------------2");
			    System.out.println("程序退出----------------3");
			    int n = input.nextInt();
			    switch(n) {
			    case 1:
			        System.out.println("您现在的信息是:");
			        User user1 =userDao.getById(user.getId());
		            System.out.println(user1.getId()+"  "+user1.getUsername()+"  "+user1.getPassword()+"  "+user1.getEmail()+"  "+"普通用户");	 
	                System.out.println("======================");
	                System.out.println("请输入要修改的姓名");
			        String username1 = input.next();
			        System.out.println("请输入要修改的密码");
			        String password1 = input.next();
			        System.out.println("请输入要修改的邮箱");
			        String email = input.next();
			        User user2 = new User(user.getId(),username1,password1,email,null);

				    int s1 =userDao.update2(user2);
				    if(s1 == 1) {
					   System.out.println("修改成功");
				    }else{
					   System.out.println("修改失败");
				    }   
			    break;
			    case 2:
				    User user3 =userDao.getById(user.getId());
			        System.out.println(user3.getId()+"  "+user3.getUsername()+"  "+user3.getPassword()+"  "+user3.getEmail()+"  "+(user3.getRole()==2?"管理员":"普通用户"));	//多此一举 
		            break;
			    case 3:
			    	mm = false;
			    	break;
			    }			    
			  }//循环括号
			    
			    
	    
			}			 			
		 }
      }
	  //用户注册
	  private  static void register() throws SQLException{
			System.out.println("请输入您的用户名:");
			String username = input.next();
			System.out.println("请输入您的密码:");
			String password = input.next();
			System.out.println("请输入您的邮箱:");
			String email = input.next();
			
		    User u = new User(username,password,email);
			int n = userDao.insert(u);
			if(n == 1) {
				System.out.println("注册成功");
			}else {
				System.out.println("失败");
			}
			
	  }
	
}
	 

UserDaoImpl类方法测试

package com.neu.test;

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

import org.junit.Test;

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

public class UserDaoImplTest {
	UserDaoImpl userDao = new UserDaoImpl();
	@Test
	public void testLogin() throws SQLException {
		
	   User user = userDao.login("tom","111");
	   System.out.println(user);
	}
	
	@Test
	public void testinsert() throws SQLException {
		 
       User  user= new User("marry","123","[email protected]");
			
		 int n =userDao.insert(user);
		 System.out.println(n);	
	}
	@Test
	public void testgetall() throws SQLException {
		
	ArrayList<User> list = new ArrayList<>();
		
		list = userDao.getall();
		System.out.println(list);
	}
	
	
	@Test
	public void testgetbyid() throws SQLException {
		User user = new User();
		
		 user = userDao.getById(1);
		System.out.println(user);
	}
	@Test
	public void testgetname() throws SQLException {
		
		User user = userDao.getName("tom");
		System.out.println(user);
	}
	
	@Test
	public void testupdate() throws SQLException {
		
		
		User user = new User(4,"marry","456","321qq.com",2);
		
		
		int n =userDao.update(user);
		
		System.out.println(n);		
	}
	@Test
	public void testdelete() throws SQLException {
		
		int n = userDao.delete(5);
		System.out.println(n);
	}
JDBCUitl类

    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 getConnertion() 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 = getConnertion();
    	
    	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 {
    	
    	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;	
    }   
}


你可能感兴趣的:(2019.1.3 JDBC项目,项目名称 :用户管理系统)