After learning the basics of Java, I practiced using MySQL as a simple user management system for data storage, this is a management system without interface, it looks more boring, first give a few bar.
Let's start by previewing the package structure.
The main class is 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("谢谢使用,再见,拜拜!"); }}
Use two interfaces to standardize
The first one is the menu interface, which abstracts two abstract methods,
package com.nensoft.bean;public interface Menu { //显示菜单 void showInfro(); //提供选项(对象) Item pickItem();}
The second is, the menu item interface, the abstract is the method of the menus execution
public interface Item { //执行功能 Menu excute();}
UserInfo is used to record the current user information
Package com.nensoft.bean;//entity Class Entitypublic class Userinfo {private int id; Private String username; private String password; Private String Email; private int power; The parameterless constructor is related to the framework 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 Package
Permissions are divided into normal user menu (Normalmenu) and Administrator menu (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 SHOWINFR O () {//TODO auto-generated Method Stub System.out.println ("=============="); System.out.println ("Welcome to the main form!") "); System.out.println (Start.loginuser.getUsername () + "Hello!" "+" your permission is: Administrator "); System.out.println ("Add user---------1"); System.out.println ("Delete user---------2"); SYSTEM.OUT.PRINTLN ("Modify user---------3"); SYSTEM.OUT.PRINTLN ("Query user---------4"); System.out.println ("Exit program---------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 ("Welcome to the main form!") "); System.out.println (Start.loginuser.getUsername () + "Hello!" "+" your permission is: Ordinary user "); SYSTEM.OUT.PRINTLN ("Modify your own information---------1"); SYSTEM.OUT.PRINTLN ("Query your own information---------2"); System.out.println ("Exit program--------------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; }}
There is also a main menu for displaying the login interface
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; }}
Another is the administrator's query menu
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 ("Query all Users--------1"); SYSTEM.OUT.PRINTLN ("Query single user--------2"); System.out.println ("Return--------------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; }}
There is a tool class, including database connection, and the detection of user input data specification, Com.util package
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.clos E (); } catch (SQLException e) {//TODO auto-generated catch blockE.printstacktrace (); }}} public void Close (PreparedStatement item) {if (item!=null) {try {item. Close (); } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } } }}
Tool class
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 ("Please select"); try{int num=scan.nextint (); if (Num<low | | num> high) {System.out.println ("value is illegal, re-select"); }else{return num; }}catch (Exception e) {}}} public static String getString () {W Hile (True) {Scanner scan=new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Please select"); String Str=scan.nextline (); if (str!=null && str.trim ()! = "") {return str; }else{System.out.println ("error"); }}} public static String Getemail () {while (true) {Scanner scan=new Scanner (system.in); String EMAIl=scan.nextline (); /* * Must have @ * cannot have multiple @ and. * Cannot start at. Front * @ cannot begin,. Cannot end */if (Email.indexof ("@") ==-1| | email.indexof (".") ==-1) {System.out.println ("must have @ and." in the mailbox); }else if (Email.indexof ("@")! = Email.lastindexof ("@") | | email.indexof (".")! = Email.lastindexof (".")) {SYSTEM.OUT.PRINTLN ("cannot have more than one @ and." in the mailbox); }else if (Email.startswith ("@") | | | Email.endswith (".")) {System.out.println ("Cannot start and end in mailbox"); }else{return email; } } } }
The main Menu package includes the user login class, the registration class,
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 ("User login Interface"); System.out.println ("=============="); System.out.println ("Please enter your user name:"); String username= consoleutil.getstring (); System.out.println ("Please enter your password:"); 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 ("Login Successful"); 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.printstacktrac E (); } return (Menu) new MainMenu (); }}
Registering classes
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 syste M.OUT.PRINTLN ("User Registration Interface"); System.out.println ("=========="); System.out.println ("Please enter your user name"); String username=consoleutil.getstring (); System.out.println ("Please enter your password:"); String password = consoleutil.getstring (); System.out.println ("Please enter your e-mail:"); 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 ("This user has been occupied"); }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 ("Registered successfully!"); }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } return new MainMenu (); }}
Normal User menu item package Com.normalusermenu
Modify my Information
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 ("Your present information is:"); System.out.println (Start.loginuser.getId () + "| "+start.loginuser.getusername () +" | "+ Start.loginuser.getPassword () +" | "+start.loginuser.getemail () +" | "+ (Start.loginuser.getPower () ==0?" Normal user ":" Administrator ")); System.out.println ("================"); System.out.println ("Please enter the name you want to modify:"); String username=consoleutil.getstring (); System.out.println ("Please enter the password you want to modify:"); String password=consoleutil.getstring (); SysteM.out.println ("Please enter the mailbox you want to edit:"); 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 is already occupied! Please re-enter ... "); 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); After you modify the database information, you also modify the user classInfo) attribute Start.loginuser.setPassword (password); Start.loginuser.setEmail (email); SYSTEM.OUT.PRINTLN ("Modified successfully!"); }} 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 (); }}
Search for personal information
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(); }}
Here are only some of the main code, to see all the code please go to my coding to see
Java to develop a simple user management system