功能需求
功能名称 | 备注 |
---|---|
注册管理 | 主要是系统供给对普通用户和管理员的信息注册。 |
用户信息管理 | 主要用于对用户信息的查询,修改和删除。 |
-
注册管理
-
用户信息管理
运行时效果图
项目工程
-
工程目录
案例代码
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