Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Core Java_JDBC_Problem Statement 4 There might be new Users assigned to operate

ID: 3908361 • Letter: C

Question

Core Java_JDBC_Problem Statement 4

There might be new Users assigned to operate the Shipment System every now and then. Write a program to create new Users into the System. The program should get the user details from the Users and insert the new users into the User table. Then display all the available roles in the System as a Menu and the Users has to select a specific role from the Menu post which the program assigns that role to the new Users.

Problem Specifications:
[Note :Strictly adhere to the object oriented specifications given as a part of the problem statement.Use the same class names, attribute names and method names.]

Create a class named User with the following private member variables / attributes   

String name

String userName

String password

String mobileNumber

Role role

1) Include appropriate getters and setters.

2) Include 5 argument constructor with argument order (name, userName, password, mobileNumber, role).


Create a UserDAO class , which includes following method.


Use System.out.format("%-15s %-15s %-15s %-15s %-15s %s ","User id","Name","User name","Password","Mobile number","Role") to format the string.

Create a class named Role with the following private member variables / attributes   

Integer id

String roleName

String description

1) Include appropriate getters and setters.

2) Include 3 argument constructor with argument order (id, roleName, description).


Create a RoleDAO class , which includes following method.

Use System.out.format("%-15s %-25s %s ","Role id","Role name","Role description") to format the string.

Create a class DbConnection with following method:  

Create a Main class to test the above 5 classes. Invoke the method in UserDAO and RoleDAO class and test them.

Input Format:
The input consists of a string that corresponds to user details like name, userName, password, mobileNumber, which is seperated by comma(,).
The another input consists of an integer that corresponds to role id.
Get the role object for the corresponding roll id and map the role object into user class.

Output Format :

Refer sample input and output for formatting specifications.

[ All text in bold corresponds to input and the rest corresponds to output.]

Sample Input and Output :

1. Create User

2. Display Details

3. Exit

Enter the choice :

1

Enter the user details:

Jeremy,jere233,jere@233,+1 716-652-4872
Role details:

Role id Role name Role description

2 Administrator This Office Administrator job description template is optimized for posting on online job.

4 Business analyst Business analysts are true midfielders.

6 Network engineer Network engineering is one of the more technically demanding IT jobs.

1 Software developer Software developer. Becoming a software developer, also known as a computer programmer.

3 Systems analyst Systems analysts investigate and analyse business problems.

5 Technical support These are the professional troubleshooters of the IT world.

Enter the role id:

6

1. Create User

2. Display Details

3. Exit

Enter the choice :

1

Enter the user details:

David,david365,david@365,+1 716-824-1572
Role details:

Role id Role name Role description

2 Administrator This Office Administrator job description template is optimized for posting on online job.

4 Business analyst Business analysts are true midfielders.

6 Network engineer Network engineering is one of the more technically demanding IT jobs.

1 Software developer Software developer. Becoming a software developer, also known as a computer programmer.

3 Systems analyst Systems analysts investigate and analyse business problems.

5 Technical support These are the professional troubleshooters of the IT world.

Enter the role id:

2

1. Create User

2. Display Details

3. Exit

Enter the choice :

2
User details:

User id Name User name Password Mobile number Role

1 Jeremy jere233 jere@233 +1 716-652-4872 Network engineer

2 David david365 david@365 +1 716-824-1572 Administrator

1. Create User

2. Display Details

3. Exit

Enter the choice :

3

Here, I am providing the piece of Code. Please update them and send me back.

UserDAO.java

import java.sql.*;
public class UserDAO {
  
public void createUser(User user) throws Exception
{
//fill code here.
}
  
public void displayDetails() throws Exception
{
System.out.println("User details:");
System.out.format("%-15s %-15s %-15s %-15s %-15s %s ","User id","Name","User name","Password","Mobile number","Role");
//fill code here.
}
  
}

Role.java

public class Role {
private Integer id;
private String roleName;
private String description;

public Role(Integer id, String roleName, String description) {
this.id = id;
this.roleName = roleName;
this.description = description;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getRoleName() {
return roleName;
}

public void setRoleName(String roleName) {
this.roleName = roleName;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}

Main.java

import java.util.*;

import java.io.*;

public class Main {

public static void main(String[] args) throws Exception {

int choice,roleId,c=0;

String userDetails;

Role role=null;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

User user;

while(true)

{

System.out.println("1. Create User 2. Display Details 3. Exit Enter the choice :");

choice = Integer.parseInt(br.readLine());

switch(choice)

{

case 1:

System.out.println("Enter the user details:");

//fill code here.

System.out.println("Role details:");

System.out.format("%-15s %-25s %s ","Role id","Role name","Role description");

//fill code here.

System.out.println("Enter the role id:");

roleId = Integer.parseInt(br.readLine());

//fill code here.

break;

case 2:

//fill code here.

break;

case 3:

System.exit(0);

}

}

}

}

User.java

public class User {
  
private String name;
private String userName;
private String password;
private String mobileNumber;
private Role role;

public User(String name, String userName, String password,String mobileNumber, Role role) {
this.name = name;
this.userName = userName;
this.password = password;
this.role = role;
this.mobileNumber = mobileNumber;
  
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

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 Role getRole() {
return role;
}

public void setRole(Role role) {
this.role = role;
}

public String getMobileNumber() {
return mobileNumber;
}

public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
  
}

RoleDAO.java

import java.sql.*;
import java.util.*;
public class RoleDAO {
  
public List<Role> getAllRoles() throws Exception
{
//fill code here.
}
  
}

mysql.properties

db.url=jdbc:mysql://127.0.0.1:3306/userrole
db.username=root
db.password=test

script.sql

drop table if exists role;

create table role(`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(50) NOT NULL,

`description` varchar(150) NOT NULL,primary key(id));

INSERT INTO `role` (`id`, `name`, `description`) VALUES

(1, 'Software developer', 'Software developer. Becoming a software developer, also known as a computer programmer.'),

(2, 'Administrator', 'This Office Administrator job description template is optimized for posting on online job.'),

(3, 'Systems analyst', 'Systems analysts investigate and analyse business problems.'),

(4, 'Business analyst', 'Business analysts are true midfielders.'),

(5, 'Technical support', 'These are the professional troubleshooters of the IT world.'),

(6, 'Network engineer', 'Network engineering is one of the more technically demanding IT jobs.');

drop table if exists user;

CREATE TABLE `user` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(50) NOT NULL,

`username` varchar(50) NOT NULL,

`password` varchar(50) NOT NULL,

`mobile number` varchar(20) NOT NULL,

`role` varchar(50) NOT NULL, PRIMARY KEY (`id`)

);

INSERT INTO `user` (`id`, `name`, `username`, `password`, `mobile number`, `role`) VALUES

(1, 'Jeremy', 'jere233', 'jere@233', '+1 716-652-4872', 'Network engineer'),

(2, 'David', 'david365', 'david@365', '+1 716-824-1572', 'Administrator'),

(3, 'Michael Buble', 'micbub222', 'micbub@222', '+1 480-926-0345', 'Technical support'),

(4, 'Drake', 'drake8456', 'drake@8456', '+1 952-883-2121', 'Network engineer'),

(5, 'Janet Jackson', 'jajack784', 'jajack@784', '+1 406-656-0808', 'Business analyst'),

(6, 'Phil Collins', 'pcollin', 'p@collin', '+1 812-473-9244', 'Systems analyst'),

(7, 'James Blunt', 'jablu1221', 'jablu@1221', '+1 562-945-7738', 'Software developer'),

(8, 'Liam Gallagher', 'liga789', 'liga@789', '+1 800-922-6250', 'Business analyst'),

(9, 'George Ezra', 'geez159', 'geez@159', '+1 812-473-9244', 'Systems analyst');

DbConnection.java

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.ResourceBundle;

public class DbConnection {

public static Connection getConnection() throws ClassNotFoundException, SQLException {   

ResourceBundle rb= ResourceBundle.getBundle("mysql");

String url = rb.getString("db.url");

String username = rb.getString("db.username");

String password = rb.getString("db.password");

//fill the code

}

}

No Method Name Method Description 1 public void createUser(User user) This method is used to insert the new users data to the user table in database. 2 public void displayDetails() This method is to display all the newly created users with their details. user id INT(11) name VARCHAR(50) role id INT(11) 1 usemame VARCHARSO name VARCHAR(50) password VARCHAR(50) mobile number VARCHAR(20) description VARCHAR(150) Indexes role_id INT (11) Indexes

Explanation / Answer

USERDAO.java

import java.sql.*;

public class UserDAO {

public void createUser(User user) throws Exception

{

    // getting the database connection

    Connection conn = DbConnection.getConnection();

    try {

           

          Statement s = conn.createStatement();

         // executing the insert statement to store the new user

          s.executeUpdate("Insert into User (name, username, password, mobile number, role) Values "

                  + "("+user.getName()+","+user.getUserName()+","+user.getPassword()+","+user.getMobileNumber()+","+user.getRole().getRoleName()+")");

          

        } catch (SQLException sqlexp) {

            System.err.println("Failed to execute the statements.");

            System.err.println(sqlexp.getMessage());

        }

        // closing the connection

        conn.close();

}

public void displayDetails() throws Exception

{

System.out.println("User details:");

System.out.format("%-15s %-15s %-15s %-15s %-15s %s ","User id","Name","User name","Password","Mobile number","Role");

    // getting the database connection

    Connection conn = DbConnection.getConnection();

    try {

           

          Statement s = conn.createStatement();

          // executing the select query

         ResultSet rs = s.executeQuery("Select * from User");

         // looping throug all the rows in the user table

          while(rs.next())

          {

              System.out.format("%-15s %-15s %-15s %-15s %-15s %s ",rs.getInt("name"), rs.getString("username"), rs.getString("password"), rs.getString("mobile number"), rs.getString("role_id"));

          }

          

        } catch (SQLException sqlexp) {

            System.err.println("Failed to execute the statements.");

            System.err.println(sqlexp.getMessage());

        }

        // closing the ocnnection

        conn.close();

}

}

//----------------------------------------------------------------------------------------------------------------------------------

ROLEDAO.java

import java.sql.*;

import java.util.*;

public class RoleDAO {

public List<Role> getAllRoles() throws Exception

{

    List<Role> results = new ArrayList<Role>();

    // getting the database connection

    Connection conn = DbConnection.getConnection();

    try {

          Statement s = conn.createStatement();

          // getting all the roles in the Role table

          ResultSet rs = s.executeQuery("Select * from Role");

          while(rs.next())

          {

              // adding the roles to the results list

              results.add(new Role(rs.getInt("id"), rs.getString("name"), rs.getString("description")));

          }

        

         

        } catch (SQLException sqlexp) {

            System.err.println("Failed to execute the statements.");

            System.err.println(sqlexp.getMessage());

        }

    // closing the connection

    conn.close();

    return results;

}

}

//----------------------------------------------------------------------------------------------------------------------------------

MAIN.java

import java.util.*;

import java.io.*;

public class Main {

public static void main(String[] args) throws Exception {

int choice,roleId,c=0;

String userDetails;

Role role=null;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

User user;

// initializing the userDAO and RoleDAO classes

UserDAO udao = new UserDAO();

RoleDAO rdao = new RoleDAO();

while(true)

{

System.out.println("1. Create User 2. Display Details 3. Exit Enter the choice :");

choice = Integer.parseInt(br.readLine());

switch(choice)

{

case 1:

System.out.println("Enter the user details:");

// taking user details and splitting them to get individual values

userDetails = br.readLine();

String[] details = userDetails.split(",");

String name = details[0];

String username = details[1];

String passwrd = details[2];

String mobilenum = details[3];

System.out.println("Role details:");

System.out.format("%-15s %-25s %s ","Role id","Role name","Role description");

// getting all the roles in the Role table

List<Role> allRoles = rdao.getAllRoles();

for(Role r: allRoles)

{

    System.out.format("%-15s %-25s %s ",r.getId(), r.getRoleName(), r.getDescription());

}

System.out.println("Enter the role id:");

roleId = Integer.parseInt(br.readLine());

// matching the passed role id with the ones present in the Role table

for(Role r: allRoles)

{

    if(r.getId() == roleId)

    {

        role = r;

    }

}

// creating a new user object

user = new User(name, username, passwrd, mobilenum, role);

udao.createUser(user);

break;

case 2:

// displaying all the users in the User table

udao.displayDetails();

break;

case 3:

System.exit(0);

}

}

}

}

//----------------------------------------------------------------------------------------------------------------------------------

DBCOnnection.java

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.ResourceBundle;

public class DbConnection {

public static Connection getConnection() throws ClassNotFoundException, SQLException {  

// getting the database parameter values

ResourceBundle rb= ResourceBundle.getBundle("mysql");

String url = rb.getString("db.url");

String username = rb.getString("db.username");

String password = rb.getString("db.password");

//fill the code

Connection conn = null;

try {

        // creating a connection the database

        Class.forName("com.mysql.jdbc.Driver");

        conn = DriverManager.getConnection(url, username, password);

     

    } catch (SQLException sqlexp) {

      System.err.println("Failed to connect to the database.");

      System.err.println(sqlexp.getMessage());

    }

    // returning the connection

    return conn;

}

}

//----------------------------------------------------------------------------------------------------------------------------------Role.java

public class Role {

private Integer id;

private String roleName;

private String description;

public Role(Integer id, String roleName, String description) {

this.id = id;

this.roleName = roleName;

this.description = description;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getRoleName() {

return roleName;

}

public void setRoleName(String roleName) {

this.roleName = roleName;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

}             

//----------------------------------------------------------------------------------------------------------------------------------User.java

public class User {

private String name;

private String userName;

private String password;

private String mobileNumber;

private Role role;

public User(String name, String userName, String password,String mobileNumber, Role role) {

this.name = name;

this.userName = userName;

this.password = password;

this.role = role;

this.mobileNumber = mobileNumber;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

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 Role getRole() {

return role;

}

public void setRole(Role role) {

this.role = role;

}

public String getMobileNumber() {

return mobileNumber;

}

public void setMobileNumber(String mobileNumber) {

this.mobileNumber = mobileNumber;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote