源码(包括sql文件):https://download.csdn.net/download/mistywood/11647743
https://github.com/MistyWood/proJDBC/tree/master
(注意:第一位管理员需手动添加到数据库,记得加载mysql驱动)
管理员的添加和普通用户的注册一样,id 自动分配。添加的新用户权限是管理员
管理员可以根据id修改修改任何人的信息。(注:可以把普通用户修改成管理员)
有三种方式
查询全部人员
按id查询
按姓名查询(支持模糊查询)
根据题意,数据库里的user表中有id(流水号),username(姓名),pwd(密码),email(邮箱),admin(用户权限)几个字段
package com.cyz.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtils {
private static String drive = "org.gjt.mm.mysql.Driver";
private static String url = "jdbc:mysql://localhost/projdbc?&useUnicode=true&characterEncoding=utf-8";
private static String user = "root";
private static String password = "root123";
static {
try {
Class.forName(drive);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private DBUtils() {
}
public static Connection getConn() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void close(ResultSet rs, PreparedStatement pstm, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.cyz.dao;
public class User {
private int id;
private String username;
private String pwd;
private String email;
private String admin;
public User(String username, String pwd, String email) {
super();
this.username = username;
this.pwd = pwd;
this.email = email;
}
public User(int id, String username, String pwd, String email, String admin) {
super();
this.id = id;
this.username = username;
this.pwd = pwd;
this.email = email;
this.admin = admin;
}
public User(String username, String pwd, String email, String admin) {
super();
this.username = username;
this.pwd = pwd;
this.email = email;
this.admin = admin;
}
public User(int id, String username, String pwd, String email) {
super();
this.id = id;
this.username = username;
this.pwd = pwd;
this.email = email;
}
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 getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAdmin() {
return admin;
}
public void setAdmin(String admin) {
this.admin = admin;
}
}
主界面MainView.java
package com.cyz.view;
import java.util.Scanner;
public class MainView {
static Scanner sc = new Scanner(System.in);
public static void run() {
while (true) {
System.out.println("==========================");
System.out.println("欢迎使用用户管理系统");
System.out.println("==========================");
System.out.println("用户登录---------1");
System.out.println("用户注册---------2");
System.out.println("退出程序---------3");
String option = sc.nextLine();
switch (option) {
case "1":
LoginView.show();
break;
case "2":
RegisterView.show();
break;
case "3":
return;
default:
System.out.println("请重新输入");
break;
}
}
}
}
注册界面RegisterView.java
package com.cyz.view;
import java.util.Scanner;
import com.cyz.dao.User;
import com.cyz.service.UserService;
import com.cyz.service.UserServiceImpl;
public class RegisterView {
static Scanner sc = new Scanner(System.in);
public static void show() {
System.out.println("用户注册界面");
System.out.println("==========================");
System.out.println("请输入您的用户名");
String username = sc.nextLine();
System.out.println("请输入您的密码");
String pwd = sc.nextLine();
System.out.println("请输入您的邮箱");
String email = sc.nextLine();
User u = new User(username, pwd, email,"普通用户");
UserService us = new UserServiceImpl();
if (us.register(u))
System.out.println("用户注册成功,请重新登录");
else
System.out.println("用户名已存在或为空,用户注册失败");
return;
}
}
登录界面Login.java
package com.cyz.view;
import java.util.Scanner;
import com.cyz.service.UserService;
import com.cyz.service.UserServiceImpl;
public class LoginView {
static Scanner sc = new Scanner(System.in);
// 登录界面
public static void show() {
System.out.println("==========================");
System.out.println("用户登录界面");
System.out.println("==========================");
System.out.println("请输入您的用户名");
String username = sc.nextLine();
System.out.println("请输入您的密码");
String pwd = sc.nextLine();
UserService us = new UserServiceImpl();
us.login(username, pwd);
return;
}
}
普通用户操作界面UserView.java
package com.cyz.view;
import java.util.Scanner;
import com.cyz.dao.User;
import com.cyz.service.UserService;
import com.cyz.service.UserServiceImpl;
public class UserView {
static Scanner sc = new Scanner(System.in);
// 普通用户登录之后的主界面
public static void show(User u) {
System.out.println("==========================");
System.out.println("欢迎登录主窗体");
System.out.println(u.getUsername() + " 您好,您的权限是:普通用户");
while (true) {
System.out.println("==========================");
System.out.println("修改自己的信息---------1");
System.out.println("查询自己的信息---------2");
System.out.println("返回上一级--------------3");
String option = sc.nextLine();
switch (option) {
case "1":
UserView.userModifyShow(u);
break;
case "2":
UserView.userInfoShow(u);
break;
case "3":
return;
default:
System.out.println("请重新输入");
break;
}
}
}
// 普通用户修改个人信息界面
public static void userModifyShow(User u) {
System.out.println("您现在的信息是:");
System.out.println(
u.getId() + " " + u.getUsername() + " " + u.getPwd() + " " + u.getEmail() + " " + u.getAdmin());
System.out.println("==========================");
System.out.println("请输入要修改的姓名");
String username = sc.nextLine();
u.setUsername(username);
System.out.println("请输入要修改的密码");
String pwd = sc.nextLine();
u.setPwd(pwd);
System.out.println("请输入要修改的邮箱");
String email = sc.nextLine();
u.setEmail(email);
UserService us = new UserServiceImpl();
us.userModify(u);
}
// 用户查询个人信息
public static void userInfoShow(User u) {
UserService us = new UserServiceImpl();
User user = us.userInfo(u.getUsername());
System.out.println(user.getId() + "\t" + user.getUsername() + "\t" + user.getPwd() + "\t" + user.getEmail()
+ "\t" + user.getAdmin());
return;
}
}
管理员操作界面
package com.cyz.view;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import com.cyz.dao.User;
import com.cyz.service.UserService;
import com.cyz.service.UserServiceImpl;
public class AdminView {
static Scanner sc = new Scanner(System.in);
// 管理员登录过后的主界面
public static void show(User u) {
System.out.println("==========================");
System.out.println("欢迎登录主窗体");
System.out.println(u.getUsername() + " 您好,您的权限是:管理员");
while (true) {
System.out.println("==========================");
System.out.println("添加用户--------------1");
System.out.println("删除用户--------------2");
System.out.println("修改用户--------------3");
System.out.println("查询用户--------------4");
System.out.println("返回上一级------------5");
String option = sc.nextLine();
switch (option) {
case "1":
AdminView.adminAddUserShow();
break;
case "2":
AdminView.delUserShow();
break;
case "3":
AdminView.adminModifyShow();
break;
case "4":
AdminView.adminQueryShow();
break;
case "5":
return;
default:
System.out.println("请重新输入");
break;
}
}
}
// 管理员添加用户
public static void adminAddUserShow() {
System.out.println("==========================");
System.out.println("用户添加界面");
System.out.println("==========================");
System.out.println("请输入添加的用户名");
String username = sc.nextLine();
System.out.println("请输入添加的密码");
String pwd = sc.nextLine();
System.out.println("请输入添加的邮箱");
String email = sc.nextLine();
User u = new User(username, pwd, email,"管理员");
UserService us = new UserServiceImpl();
if (us.register(u))
System.out.println("用户添加成功");
else
System.out.println("用户名已存在或为空,用户添加失败");
return;
}
// 管理员删除用户
private static void delUserShow() {
System.out.println("请输入要删除用户的ID号码");
int id=Integer.parseInt(sc.nextLine());
UserService us = new UserServiceImpl();
us.delUser(id);
return;
}
// 管理员修改用户
private static void adminModifyShow() {
System.out.println("请输入要修改的用户的ID号码");
int id = Integer.parseInt(sc.nextLine());
System.out.println("请输入要修改的用户的用户名");
String username = sc.nextLine();
System.out.println("请输入要修改的用户的密码");
String pwd = sc.nextLine();
System.out.println("请输入要修改的用户的邮箱");
String email = sc.nextLine();
System.out.println("请输入要修改的用户的权限(管理员/普通用户)");
String admin = sc.nextLine();
User u = new User(id, username, pwd, email, admin);
UserService us = new UserServiceImpl();
us.adminModifyUser(u);
}
// 管理员查询用户
public static void adminQueryShow() {
while (true) {
System.out.println("查询全部用户--------------1");
System.out.println("根据id查询用户--------------2");
System.out.println("根据姓名查询用户--------------3");
System.out.println("返回上一级--------------4");
String option = sc.nextLine();
switch (option) {
case "1":
AdminView.adminQueryAllShow();
break;
case "2":
AdminView.adminQueryByIdShow();
break;
case "3":
AdminView.adminQueryByNameShow();
break;
case "4":
return;
default:
System.out.println("请重新输入");
break;
}
}
}
// 管理员查询所有用户
public static void adminQueryAllShow() {
UserService us = new UserServiceImpl();
List users = us.queryAll();
if(users.size()==0) System.out.println("查询结果为空\n");
Iterator it = users.iterator();
while(it.hasNext()) {
User user=it.next();
System.out.println(user.getId() + "\t" + user.getUsername() + "\t" + user.getPwd() + "\t" + user.getEmail() + "\t"
+ user.getAdmin());
System.out.println("================================================");
}
return;
}
// 管理员按id查询用户
public static void adminQueryByIdShow() {
System.out.println("请输入要查询的id");
int id=Integer.parseInt(sc.nextLine());
UserService us = new UserServiceImpl();
User u=us.queryById(id);
if(u!=null) {
System.out.println(u);
}else {
System.out.println("用户不存在");
}
System.out.println("================================================");
return;
}
// 管理员按username查询用户(支持模糊查询)
public static void adminQueryByNameShow() {
System.out.println("请输入要查询的名字(支持模糊查询)");
String name=sc.nextLine();
UserService us = new UserServiceImpl();
List users=us.queryByName(name);
if(users.size()==0) System.out.println("查询结果为空\n");
Iterator it = users.iterator();
while(it.hasNext()) {
User user=it.next();
System.out.println(user);
System.out.println("================================================");
}
return;
}
}
写了UserDao接口,用UserDaoImpl实现,此处UserDao略去不写
package com.cyz.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.cyz.db.DBUtils;
public class UserDaoImpl implements UserDao {
@Override
public boolean addUser(User u) {
Connection conn=null;
PreparedStatement pstm=null;
try {
conn=DBUtils.getConn();
String sql=" insert into user(username,pwd,email,admin) value(?,?,?,?) ";
pstm=conn.prepareStatement(sql);
pstm.setString(1, u.getUsername());
pstm.setString(2, u.getPwd());
pstm.setString(3, u.getEmail());
pstm.setString(4, u.getAdmin());
return pstm.executeUpdate()>0;
} catch (SQLException e) {
System.out.println("注册添加失败");
e.printStackTrace();
}finally {
DBUtils.close(null, pstm, conn);
}
return false;
}
@Override
public boolean modifyUser(User u) {
Connection conn=null;
PreparedStatement pstm=null;
try {
conn=DBUtils.getConn();
String sql=" update user set username=?,pwd=?,email=?,admin=? where id=? ";
pstm=conn.prepareStatement(sql);
pstm.setString(1, u.getUsername());
pstm.setString(2, u.getPwd());
pstm.setString(3, u.getEmail());
pstm.setString(4, u.getAdmin());
pstm.setInt(5, u.getId());
return pstm.executeUpdate()>0;
} catch (SQLException e) {
System.out.println("修改信息失败");
e.printStackTrace();
}finally {
DBUtils.close(null, pstm, conn);
}
return false;
}
@Override
public User queryByNameAndPwd(String username, String pwd) {
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
User u=null;
try {
conn=DBUtils.getConn();
String sql=" select id,username,pwd,email,admin from user where username=? and pwd=?";
pstm=conn.prepareStatement(sql);
pstm.setString(1, username);
pstm.setString(2, pwd);
rs=pstm.executeQuery();
while(rs.next()) {
u=new User(rs.getInt("id"),rs.getString("username"),rs.getString("pwd"),rs.getString("email"),rs.getString("admin"));
}
return u;
} catch (SQLException e) {
System.out.println("查询失败");
e.printStackTrace();
}finally {
DBUtils.close(rs, pstm, conn);
}
return null;
}
@Override
public User queryByUsername(String username) {
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
User u=null;
try {
conn=DBUtils.getConn();
String sql=" select id,username,pwd,email,admin from user where username=? ";
pstm=conn.prepareStatement(sql);
pstm.setString(1, username);
rs=pstm.executeQuery();
while(rs.next()) {
u=new User(rs.getInt("id"),rs.getString("username"),rs.getString("pwd"),rs.getString("email"),rs.getString("admin"));
}
return u;
} catch (SQLException e) {
System.out.println("查询失败");
e.printStackTrace();
}finally {
DBUtils.close(rs, pstm, conn);
}
return null;
}
@Override
public boolean delUser(int id) {
Connection conn=null;
PreparedStatement pstm=null;
try {
conn=DBUtils.getConn();
String sql=" delete from user where id=? ";
pstm=conn.prepareStatement(sql);
pstm.setInt(1, id);
return pstm.executeUpdate()>0;
} catch (SQLException e) {
System.out.println("删除失败");
e.printStackTrace();
}finally {
DBUtils.close(null, pstm, conn);
}
return false;
}
@Override
public User queryById(int id) {
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
User u=null;
try {
conn=DBUtils.getConn();
String sql=" select id,username,pwd,email,admin from user where id=? ";
pstm=conn.prepareStatement(sql);
pstm.setInt(1, id);
rs=pstm.executeQuery();
while(rs.next()) {
u=new User(rs.getInt("id"),rs.getString("username"),rs.getString("pwd"),rs.getString("email"),rs.getString("admin"));
}
return u;
} catch (SQLException e) {
System.out.println("查询失败");
e.printStackTrace();
}finally {
DBUtils.close(rs, pstm, conn);
}
return null;
}
@Override
public List queryAll() {
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
User u=null;
List users=new ArrayList<>();
try {
conn=DBUtils.getConn();
String sql=" select id,username,pwd,email,admin from user ";
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();
while(rs.next()) {
u=new User(rs.getInt("id"),rs.getString("username"),rs.getString("pwd"),rs.getString("email"),rs.getString("admin"));
users.add(u);
}
return users;
} catch (SQLException e) {
System.out.println("查询失败");
e.printStackTrace();
}finally {
DBUtils.close(rs, pstm, conn);
}
return null;
}
@Override
public List queryByName(String name) {
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
User u=null;
List users=new ArrayList<>();
try {
conn=DBUtils.getConn();
String sql=" select id,username,pwd,email,admin from user where username like ? ";
pstm=conn.prepareStatement(sql);
pstm.setString(1, "%"+name+"%");
rs=pstm.executeQuery();
while(rs.next()) {
u=new User(rs.getInt("id"),rs.getString("username"),rs.getString("pwd"),rs.getString("email"),rs.getString("admin"));
users.add(u);
}
return users;
} catch (SQLException e) {
System.out.println("查询失败");
e.printStackTrace();
}finally {
DBUtils.close(rs, pstm, conn);
}
return null;
}
}
写了UserService接口,用UserServiceImpl实现,此处UserService略去不写
package com.cyz.service;
import java.util.List;
import com.cyz.dao.User;
import com.cyz.dao.UserDao;
import com.cyz.dao.UserDaoImpl;
import com.cyz.view.AdminView;
import com.cyz.view.UserView;
public class UserServiceImpl implements UserService {
@Override
public boolean register(User u) {
UserDao ud = new UserDaoImpl();
if (!u.getUsername().equals("")&&!u.getPwd().equals("")&&ud.queryByUsername(u.getUsername()) == null && ud.addUser(u)) {
return true;
}
return false;
}
@Override
public boolean login(String username, String pwd) {
UserDao ud = new UserDaoImpl();
User u = null;
if ((u = ud.queryByNameAndPwd(username, pwd)) != null) {
System.out.println("登录成功");
if (u.getAdmin().equals("管理员")) {
AdminView.show(u);
} else {
UserView.show(u);
}
return true;
}
System.out.println("登录失败,用户名或密码错误");
return false;
}
@Override
public User userInfo(String username) {
UserDao ud = new UserDaoImpl();
User u = ud.queryByUsername(username);
return u;
}
@Override
public boolean userModify(User u) {
UserDao ud = new UserDaoImpl();
if (ud.queryByUsername(u.getUsername()) == null && ud.modifyUser(u)) {
System.out.println("修改成功");
return true;
}
System.out.println("用户名已存在,修改失败");
return false;
}
@Override
public boolean delUser(int id) {
UserDao ud = new UserDaoImpl();
if (ud.queryById(id) != null && ud.delUser(id)) {
System.out.println("删除成功");
return true;
}
System.out.println("用户不存在");
return false;
}
@Override
public boolean adminModifyUser(User u) {
UserDao ud = new UserDaoImpl();
if (ud.queryById(u.getId()) != null) {
if (ud.queryByUsername(u.getUsername()) == null && ud.modifyUser(u)) {
System.out.println("修改成功");
return true;
} else {
System.out.println("用户名已存在,修改失败");
}
} else {
System.out.println("该id不存在,修改失败");
}
return false;
}
@Override
public List queryAll() {
UserDao ud = new UserDaoImpl();
List users = ud.queryAll();
return users;
}
@Override
public User queryById(int id) {
UserDao ud = new UserDaoImpl();
User u = ud.queryById(id);
return u;
}
@Override
public List queryByName(String name) {
UserDao ud=new UserDaoImpl();
List users=ud.queryByName(name);
return users;
}
}
主函数调用主界面run()方法,直接运行
package com.cyz.main;
import com.cyz.view.MainView;
public class MainApp {
public static void main(String[] args) {
MainView.run();
return;
}
}