Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Scanner;
- public class Admin {
- public static void AdminMenu() throws Exception { //main menu for administration functions
- System.out.println("Admin Menu \n1 - Add Profile\n2 - Delete Profile\n3 - Update Profile\n4 - Main Menu\n5 - Exit");
- Scanner reader = new Scanner(System.in);
- System.out.println("Enter a number: ");
- int n = reader.nextInt();
- if(n==1){
- Admin.AddProfile();
- }
- if(n==2){
- Admin.DeleteProfile();
- }
- if(n==3){
- UpdateProfile();
- }
- if(n==4){
- Main.Menu();
- }
- if(n==5){
- System.out.println("Application Exited.");
- System.exit(5);
- }
- }
- public static void AddProfile() throws SQLException { //creates new profile for a user
- Connection con = null;
- PreparedStatement state = null;
- Scanner scanner = null;
- try {
- scanner = new Scanner(System.in);
- System.out.print("Enter the password: ");
- String password = scanner.nextLine();
- if( !password.equals(password.toLowerCase()) && //makes sure password has a capital letter, has at least 6 characters and a symbol or number
- !password.equals(password.toUpperCase()) &&
- password.matches(".*\\d+.*") &&
- password.length() >= 6
- ){
- System.out.println("Success");
- }else{
- System.out.println("The password must contain a capital letter, lowercase letter, a symbol and contain at least 6 characters");
- Admin.AddProfile();
- }
- //user enters in relevant details
- System.out.print("Enter the username: ");
- String username = scanner.nextLine();
- System.out.print("Enter the surname: ");
- String surname = scanner.nextLine();
- System.out.print("Enter the middle name(s): ");
- String middleName = scanner.nextLine();
- System.out.print("Enter the first name: ");
- String firstName = scanner.nextLine();
- System.out.print("Enter the birthday (YYYY-MM-DD): ");
- String birthday = scanner.nextLine();
- System.out.print("Enter the home phone: ");
- String homePhone = scanner.nextLine();
- System.out.print("Enter the work phone: ");
- String workPhone = scanner.nextLine();
- System.out.print("Enter the E-Mail: ");
- String eMail = scanner.nextLine();
- System.out.print("Enter the Address: ");
- String address = scanner.nextLine();
- System.out.print("Enter the Role (student/staff/tech): "); //technician being IT technician
- String role = scanner.nextLine();
- System.out.print("Enter the any other relevant information: ");
- String note = scanner.nextLine();
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/admin","root","xcaliz0rz");
- String sql = "insert into profiles " //inserts information into profiles database
- + " (username, password, surname, middleName, firstName, birthday, homePhone, workPhone, eMail, address, note, role )" + " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- state = con.prepareStatement(sql);
- state.setString(1, username);
- state.setString(2, password);
- state.setString(3, surname);
- state.setString(4, middleName);
- state.setString(5, firstName);
- state.setString(6, birthday);
- state.setString(7, homePhone);
- state.setString(8, workPhone);
- state.setString(9, eMail);
- state.setString(10, address);
- state.setString(11, note);
- state.setString(12, role);
- state.executeUpdate(); //execute the function of adding the new information to the database
- System.out.println("Profile details added."); //informs user that the action is complete
- Admin.AdminMenu(); //sends user back to the admin menu
- } catch (Exception exc) {
- exc.printStackTrace();
- } finally {
- if (state != null) {
- state.close();
- }
- if (con != null) {
- con.close();
- }
- if (scanner != null) {
- scanner.close();
- }
- }
- }
- public static void DeleteProfile() throws SQLException { //deletes profile from system
- Connection con = null;
- PreparedStatement state = null;
- Scanner scanner = null;
- try {
- scanner = new Scanner(System.in);
- System.out.print("Enter the username: "); //user enters username of profile to delete
- String input = scanner.nextLine();
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/admin","root","xcaliz0rz");
- String sql = "delete from profiles where username = ?";
- state = con.prepareStatement(sql);
- state.setString(1, input);
- state.executeUpdate();
- System.out.println("Profile deleted.");
- Admin.AdminMenu();
- } catch (Exception exc) {
- exc.printStackTrace();
- } finally {
- if (state != null) {
- state.close();
- }
- if (con != null) {
- con.close();
- }
- if (scanner != null) {
- scanner.close();
- }
- }
- }
- public static void UpdateProfile() throws SQLException { //changes some detail to the correct current form
- Connection con = null;
- PreparedStatement state = null;
- Scanner scanner = null;
- String username;
- String sql = null;
- try {
- scanner = new Scanner(System.in);
- System.out.println("Enter the username: ");
- username = scanner.nextLine();
- System.out.println("Enter the detail type to update: "); //allows user to choose one detail to update, as opposed to entering all of the data again
- String col = scanner.nextLine();
- System.out.println("Enter the new value: ");
- String value = scanner.nextLine();
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/admin","root","xcaliz0rz");
- String verify = "SELECT * from profiles WHERE username = '" + username +"'"; //makes sure the username typed exists
- state = con.prepareStatement(verify);
- ResultSet res = state.executeQuery();
- if(res.next()){
- System.out.println("User selected.");
- }
- else{
- System.out.println("Incorrect user.");
- Admin.UpdateProfile();
- }
- sql = "update profiles set " + col + "= ?" +" where username = ?";
- state = con.prepareStatement(sql);
- state.setString(1, value);
- state.setString(2, username);
- state.executeUpdate();
- System.out.println("Record altered.");
- Admin.AdminMenu();
- } catch (Exception exc) {
- exc.printStackTrace();
- } finally {
- if (state != null) {
- state.close();
- }
- if (con != null) {
- con.close();
- }
- if (scanner != null) {
- scanner.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement