SimpleSystem.java
package com; import java.util.Scanner; public class SimpleSystem { SqlHelper helper = new SqlHelper(); Scanner scanner = new Scanner(System.in); public static void main(String[] args) { new SimpleSystem().go(); } public void go() { try{ helper.sqlSetup(); int rowNum = verify(); doThing(rowNum); exit(); }catch(Exception ex){ex.getMessage();} } public int verify() throws Exception { System.out.println("Welcome to this system"); int rowNum = 0; while(rowNum == 0) { System.out.println("Please type in username"); String username = scanner.nextLine(); System.out.println("Please type in password"); String password = scanner.nextLine(); rowNum = helper.login(username, password); } return rowNum; } public void doThing(int rowNum) throws Exception { if(rowNum == 1) { SuperUser sUser = new SuperUser(); sUser.introduction(); while(true) { System.out.println("Press ‘N‘ to exit or type in your option: "); scanner = new Scanner(System.in); String option = scanner.nextLine().toUpperCase(); switch(option) { case "A": sUser.add(helper); break; case "D": sUser.delete(helper); break; case "E": sUser.edit(helper); break; case "S": sUser.select(helper); break; case"N": return; default: System.out.println("Correct your option!"); break; } } }else { CommonUser cUser = new CommonUser(); cUser.introduction(); while(true) { System.out.println("Press ‘N‘ to exit or type in your option: "); scanner = new Scanner(System.in); String option = scanner.nextLine().toUpperCase(); switch(option) { case "S": cUser.select(helper); break; case"N": return; default: System.out.println("Correct your option!"); break; } } } } public void exit() { scanner.close(); System.out.println("System exited!"); } }
SqlHelper.java
package com; import java.sql.*; public class SqlHelper { private Connection con; private String driver = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://127.0.0.1:3306/?autoReconnect=true&useSSL=false"; private String user = "root"; private String passwd = "lly"; private Statement stmt = null; public void sqlSetup() { try { Class.forName(driver).newInstance(); con = DriverManager.getConnection(url,user,passwd); System.out.println("Server is connected."); stmt= con.createStatement(); stmt.executeQuery("USE prac"); System.out.println("Database accessed."); }catch(Exception e) { System.out.println("MYSQL ERROR: " + e.getMessage()); } } public int login(String username, String password) throws SQLException { int rowNum = 0; ResultSet rs = select(username); if(rs.next()) { if(password.equals(rs.getString(3))) { System.out.println("Successfully login!"); rowNum = rs.getInt(1); } else System.out.println("Wrong password! Try again."); }else System.out.println("User do not exist!"); return rowNum; } public ResultSet select(String name) throws SQLException { return stmt.executeQuery("SELECT * FROM user WHERE name = ‘"+name+"‘"); } public int insert(String name, String pwd) throws SQLException { return stmt.executeUpdate("INSERT INTO user(name, password) VALUES(‘"+name+"‘, ‘"+pwd+"‘)"); } public int delete(String name) throws SQLException { return stmt.executeUpdate("DELETE FROM user WHERE name = ‘"+name+"‘"); } public int edit(String oldName, String newName, String pwd) throws SQLException { return stmt.executeUpdate("UPDATE user SET name = ‘"+newName+"‘, password = ‘"+pwd+"‘ WHERE name = ‘"+oldName+"‘"); } }
User.java
package com; import java.util.Scanner; public abstract class User { Scanner scanner = new Scanner(System.in); public abstract void introduction(); }
SuperUser.java
package com; import java.sql.ResultSet; import java.sql.SQLException; class SuperUser extends User { public void introduction() { System.out.println("You‘re the administrator of this System."); System.out.println("Your rights are as follow:"); System.out.println("A -Add an user."); System.out.println("D -Delete an user"); System.out.println("E -Edit an user infomation"); System.out.println("S -Search users"); System.out.println("N -Exit"); } public void add(SqlHelper helper) throws SQLException { boolean run = true; while(run) { System.out.println("Please type in new username: "); String username = scanner.nextLine(); ResultSet sRs = helper.select(username); if(!sRs.next()) { while(true) { System.out.println("Please type in 8-digits password"); String password = scanner.nextLine(); if(password.length() == 8) { int iRs = helper.insert(username, password); if(iRs == 1) { System.out.println("New user has been added!"); run = false; break; } }else System.out.println("Password is not 8 digit! Try again."); } }else System.out.println("User exist! Try again."); } } public void delete(SqlHelper helper)throws SQLException { System.out.println("Please type in username: "); String username = scanner.nextLine(); if(!username.equals("admin")) { int flag = helper.delete(username); if(flag == 1) System.out.println("User has been deleted!"); else System.out.println("User do not exist!"); }else System.out.println("Administrator accout can not be deleted!"); } public void edit(SqlHelper helper)throws SQLException { System.out.println("Please type in the username you want to edit: "); String oldName = scanner.nextLine(); ResultSet rs = helper.select(oldName); if(rs.next()) { System.out.println("Type in new username: "); String newName = scanner.nextLine(); while(true) { System.out.println("Type in new password: "); String newPwd = scanner.nextLine(); if(newPwd.length() == 8) { int flag = helper.edit(oldName, newName, newPwd); if(flag == 1){ System.out.println("User has been edit!"); break; } }else System.out.println("Password is not 8 digit! Try again."); } }else System.out.println("User do not exist! Try again."); } public void select(SqlHelper helper) throws SQLException { System.out.println("Please type in the username you want to search: "); String username = scanner.nextLine(); ResultSet rs = helper.select(username); if(rs.next()) { System.out.println("User info is as follow: "); System.out.println("User_id: "+rs.getInt(1)+" Username: "+rs.getString(2)+"Password: "+rs.getString(3)); }else System.out.println("User do not exist!"); } }
CommonUser.java
package com; import java.sql.ResultSet; import java.sql.SQLException; class CommonUser extends User { public void introduction() { System.out.println("You‘re the common user of this System."); System.out.println("Your rights are as follow:"); System.out.println("S -Search users"); System.out.println("N -Exit"); } public void select(SqlHelper helper) throws SQLException { System.out.println("Please type in the username you want to search: "); String username = scanner.nextLine(); ResultSet rs = helper.select(username); if(rs.next()) { System.out.println("User info is as follow: "); System.out.println("User_id: "+rs.getInt(1)+" Username: "+rs.getString(2)+"Password: "+rs.getString(3)); }else System.out.println("User do not exist!"); } }
时间: 2024-11-03 05:28:08