学习完java基础后,自己练了练用MySql作为数据存储的简单用户管理系统,这是一个没有界面的管理系统,看起来比较枯燥,先给出几张截图吧。
首先预览一下包结构吧
主类是Start.java
import com.menu.MainMenu;
import com.nensoft.bean.Item;
import com.nensoft.bean.Menu;
import com.nensoft.bean.Userinfo;
import com.utils.DBUtil2;
public class Start {
public static Userinfo loginuser=new Userinfo();
public static DBUtil2 db=DBUtil2.getInstance();
public static void main(String[] args) {
// TODO Auto-generated method stub
Menu menu=new MainMenu();
while(menu != null){
//显示内容
menu.showInfro();
Item item=menu.pickItem();
menu=item.excute();
}
System.out.println("谢谢使用,再见,拜拜!");
}
}
用两个接口来规范
第一个是菜单接口,抽象了两个抽象方法,
package com.nensoft.bean;
public interface Menu {
//显示菜单
void showInfro();
//提供选项(对象)
Item pickItem();
}
第二个是,菜单项接口,抽象的是菜单执行的方法
public interface Item {
//执行功能
Menu excute();
}
UserInfo是用来记录当前用户信息的
package com.nensoft.bean;
//实体类entity
public class Userinfo {
private int id;
private String username;
private String password;
private String email;
private int power;
//无参构造器与框架有关
public Userinfo(){
}
public Userinfo(int id, String username, String password, String email, int power) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.power = power;
}
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 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 int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
}
com.Menu包
权限分为普通用户菜单(NormalMenu)和管理员菜单(AdminMenu)
import com.adminmenuitem.AddUser;
import com.adminmenuitem.DeleteUser;
import com.adminmenuitem.ModifyUser;
import com.adminmenuitem.SerchUser;
import com.mainmenuitem.Exit;
import com.nensoft.bean.Item;
import com.nensoft.bean.Menu;
import com.nensoft.start.Start;
import com.normalmenu.ModifySelfInfo;
import com.normalmenu.SerchSelfInfo;
import com.utils.ConsoleUtil;
public class AdminMenu implements Menu{
public void showInfro() {
// TODO Auto-generated method stub
System.out.println("==============");
System.out.println("欢迎登录主窗体!");
System.out.println(Start.loginuser.getUsername()+" 您好! "+"您的权限是:管理员");
System.out.println("添加用户---------1");
System.out.println("删除用户---------2");
System.out.println("修改用户---------3");
System.out.println("查询用户---------4");
System.out.println("退出程序---------5");
}
public Item pickItem() {
// TODO Auto-generated method stub
Item item=null;
int option=ConsoleUtil.getOption(1, 5);
switch(option){
case 1:
item=new AddUser();
break;
case 2:
item=new DeleteUser();
break;
case 3:
item=new ModifyUser();
break;
case 4:
item=new SerchUser();
break;
case 5:
item=new Exit();
break;
}
return item;
}
}
import com.mainmenuitem.Exit;
import com.nensoft.bean.Item;
import com.nensoft.bean.Menu;
import com.nensoft.start.Start;
import com.normalmenu.ModifySelfInfo;
import com.normalmenu.SerchSelfInfo;
import com.utils.ConsoleUtil;
public class NormalMenu implements Menu{
public void showInfro() {
// TODO Auto-generated method stub
System.out.println("==============");
System.out.println("欢迎登录主窗体!");
System.out.println(Start.loginuser.getUsername()+" 您好! "+"您的权限是:普通用户");
System.out.println("修改自己的信息---------1");
System.out.println("查询自己的信息---------2");
System.out.println("退出程序--------------3");
}
public Item pickItem() {
// TODO Auto-generated method stub
Item item=null;
int option=ConsoleUtil.getOption(1, 3);
switch(option){
case 1:
item=new ModifySelfInfo();
break;
case 2:
item=new SerchSelfInfo();
break;
case 3:
item=new Exit();
}
return item;
}
}
还有一个主菜单,用于显示登录界面的
import com.mainmenuitem.Exit;
import com.mainmenuitem.LoginItem;
import com.mainmenuitem.RegItem;
import com.nensoft.bean.Item;
import com.nensoft.bean.Menu;
import com.utils.ConsoleUtil;
public class MainMenu implements Menu{
public void showInfro() {
// TODO Auto-generated method stub
System.out.println("欢迎使用neusoft用户管理系统");
System.out.println("===================");
System.out.println("用户登录===========1");
System.out.println("用户注册===========2");
System.out.println("用户退出===========3");
}
public Item pickItem() {
// TODO Auto-generated method stub
Item item=null;
//
int option =ConsoleUtil.getOption(1, 3);
switch(option){
case 1:
item=new LoginItem();
break;
case 2:
item =new RegItem();
break;
case 3:
item=new Exit();
}
return item;
}
}
另外一个是管理员的查询菜单
import com.adminmenuitem.ReturnMenu;
import com.adminmenuitem.SerchUser;
import com.mainmenuitem.Exit;
import com.mainmenuitem.LoginItem;
import com.mainmenuitem.RegItem;
import com.nensoft.bean.Item;
import com.nensoft.bean.Menu;
import com.serchuser_item.SerchAllUser;
import com.serchuser_item.SerchOneUser;
import com.utils.ConsoleUtil;
public class SerchUserMenu implements Menu{
public void showInfro() {
// TODO Auto-generated method stub
System.out.println("============");
System.out.println("查询所有用户--------1");
System.out.println("查询单个用户--------2");
System.out.println("返回--------------3");
}
public Item pickItem() {
// TODO Auto-generated method stub
Item item=null;
//
int option =ConsoleUtil.getOption(1, 3);
switch(option){
case 1:
item=new SerchAllUser();
break;
case 2:
item =new SerchOneUser();
break;
case 3:
item=new ReturnMenu();
}
return item;
}
}
有一个工具类,包括数据库的连接,以及对用户输入数据的检测是否规范,com.util包
package com.utils;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.Format;
import java.util.Properties;
public class DBUtil2 {
private static DBUtil2 db;
private String url;
private String username;
private String password;
private String driver;
private DBUtil2(){
Properties properties=new Properties();
try {
properties.load(this.getClass().getClassLoader().getResourceAsStream("db.properties"));
driver=properties.getProperty("driver");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static DBUtil2 getInstance(){
if(db==null){
db = new DBUtil2();
}
return db;
}
public Connection getConnect() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn=null;
try {
conn = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void close(Connection item){
if(item!=null){
try {
item.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void close(ResultSet item){
if(item!=null){
try {
item.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void close(PreparedStatement item){
if(item!=null){
try {
item.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
工具类
package com.utils;
import java.util.Scanner;
public class ConsoleUtil {
public static int getOption(int low,int high){
while(true){
Scanner scan=new Scanner(System.in);
System.out.println("请选择");
try{
int num=scan.nextInt();
if(num<low || num> high){
System.out.println("数值非法,重新选择");
}else{
return num;
}
}catch(Exception e){
}
}
}
public static String getString(){
while(true){
Scanner scan=new Scanner(System.in);
//System.out.println("请选择");
String str=scan.nextLine();
if(str !=null && str.trim()!=""){
return str;
}else{
System.out.println("错误");
}
}
}
public static String getEmail(){
while(true){
Scanner scan=new Scanner(System.in);
String email=scan.nextLine();
/*
* 必须有@
* 不能有多个@和。
* @不能在.前面
* @不能开头,。不能结尾
*/
if(email.indexOf("@") ==-1|| email.indexOf(".")==-1){
System.out.println("邮箱中必须有@和.");
}else if(email.indexOf("@") != email.lastIndexOf("@") || email.indexOf(".")!=
email.lastIndexOf(".")){
System.out.println("邮箱中不能有多个@和.");
}else if(email.startsWith("@") || email.endsWith(".")){
System.out.println("邮箱中不能@开头和.结尾");
}else{
return email;
}
}
}
}
主菜单包下包括用户登录类,注册类,
package com.mainmenuitem;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.menu.AdminMenu;
import com.menu.MainMenu;
import com.menu.NormalMenu;
import com.nensoft.bean.Item;
import com.nensoft.bean.Menu;
import com.nensoft.bean.Userinfo;
import com.nensoft.start.Start;
import com.utils.ConsoleUtil;
import com.utils.DBUtil2;
public class LoginItem implements Item{
public Menu excute() {
// TODO Auto-generated method stub
System.out.println("用户登录界面");
System.out.println("==============");
System.out.println("请输入你的用户名:");
String username= ConsoleUtil.getString();
System.out.println("请输入你的密码:");
String password= ConsoleUtil.getString();
Connection con=Start.db.getConnect();
PreparedStatement ps=null;
try {
ps=con.prepareStatement("select * from userinfo where username=?");
ps.setString(1, username);
ResultSet st=ps.executeQuery();
if(st.next()){
Userinfo userinfo=new Userinfo();
userinfo.setId(st.getInt("id"));
userinfo.setUsername(st.getString(2));
userinfo.setPassword(st.getString(3));
userinfo.setEmail(st.getString(4));
userinfo.setPower(st.getInt(5));
System.out.println("登录成功");
Start.loginuser=userinfo;
if(userinfo.getPower()== 0){
return (Menu) new NormalMenu();
}else{
return (Menu) new AdminMenu();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (Menu) new MainMenu();
}
}
注册类
package com.mainmenuitem;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.menu.MainMenu;
import com.nensoft.bean.Item;
import com.nensoft.bean.Menu;
import com.nensoft.start.Start;
import com.utils.ConsoleUtil;
import com.utils.DBUtil;
import com.utils.DBUtil2;
public class RegItem implements Item{
public Menu excute() {
// TODO Auto-generated method stub
System.out.println("用户注册界面");
System.out.println("==========");
System.out.println("请输入你的用户名");
String username=ConsoleUtil.getString();
System.out.println("请输入你的密码:");
String password = ConsoleUtil.getString();
System.out.println("请输入你的邮箱:");
String email= ConsoleUtil.getEmail();
Connection con=Start.db.getConnect();
PreparedStatement ps=null;
try {
ps=con.prepareStatement("select * from userinfo where username=?");
ps.setString(1, username);
ResultSet st=ps.executeQuery();
if(st.next()){
System.out.println(st);
System.out.println("此用户已被占用");
}else{
ps=con.prepareStatement("insert into userinfo(username,password,email) values(?,?,?)");
ps.setString(1, username);
ps.setString(2, password);
ps.setString(3, email);
ps.executeUpdate();
con.setAutoCommit(true);
System.out.println("注册成功!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new MainMenu();
}
}
普通用户菜单项包com.normalusermenu
修改本人的信息
package com.normalmenu;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.menu.NormalMenu;
import com.nensoft.bean.Item;
import com.nensoft.bean.Menu;
import com.nensoft.start.Start;
import com.utils.ConsoleUtil;
import com.utils.DBUtil2;
public class ModifySelfInfo implements Item{
public Menu excute() {
// TODO Auto-generated method stub
System.out.println("================");
System.out.println("您现在的信息是:");
System.out.println(Start.loginuser.getId()+" | "+Start.loginuser.getUsername()+" | " +
Start.loginuser.getPassword()+" | "+Start.loginuser.getEmail()+" | "+
(Start.loginuser.getPower()==0?"普通用户":"管理员"));
System.out.println("================");
System.out.println("请输入您要修改的姓名:");
String username=ConsoleUtil.getString();
System.out.println("请输入您要修改的密码:");
String password=ConsoleUtil.getString();
System.out.println("请输入您要修改的邮箱:");
String email=ConsoleUtil.getString();
Connection conn=Start.db.getConnect();
PreparedStatement ps=null;
ResultSet rs=null;
try {
ps=conn.prepareStatement("select * from userinfo where username=? and id!=?");
ps.setString(1, username);
ps.setInt(2,Start.loginuser.getId());
rs=ps.executeQuery();
if(rs.next()){
System.out.println("用户名已被占用! 请重新输入...");
username=ConsoleUtil.getString();
}else{
conn.setAutoCommit(false);
ps=conn.prepareStatement("update userinfo set username=?,password=?,email=? where id="+Start.loginuser.getId());
ps.setString(1,username);
ps.setString(2, password);
ps.setString(3, email);
ps.executeUpdate();
conn.commit();
Start.loginuser.setUsername(username); //修改完数据库的信息后还要同时修改用户类(UserInfo)的属性
Start.loginuser.setPassword(password);
Start.loginuser.setEmail(email);
System.out.println("修改成功!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Start.db.close(rs);
Start.db.close(ps);
Start.db.close(conn);
return new NormalMenu();
}
}
查询个人信息
package com.normalmenu;
import com.menu.NormalMenu;
import com.nensoft.bean.Item;
import com.nensoft.bean.Menu;
import com.nensoft.start.Start;
public class SerchSelfInfo implements Item{
public Menu excute() {
// TODO Auto-generated method stub
System.out.println("================");
System.out.println("您现在的信息是:");
System.out.println(Start.loginuser.getId()+" | "+Start.loginuser.getUsername()+" | " +
Start.loginuser.getPassword()+" | "+Start.loginuser.getEmail()+" | "+
(Start.loginuser.getPower()==0?"普通用户":"管理员"));
return new NormalMenu();
}
}
这里只列出了部分主要代码,要查看全部代码请到我的Coding里去看
原文地址:https://www.cnblogs.com/liu-ya/p/9277501.html
时间: 2024-10-13 19:01:05