java开发简单的用户管理系统 (3)

工具类

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修改成功!"); } } 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(); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgjspz.html