Insert & Display Halls
Write a program to insert the hall data associated with the given username of the owner and display the list of hall details by retrieving data from the database.
Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names, and method names should be the same as specified in the problem statement.
Consider the class User with the following private attributes,
Attributes | Datatype |
id | Long |
name | String |
mobileNumber | String |
username | String |
password | String |
The methods for getters, setters and constructors are given in the template code.
Consider the class Hall with the following private attributes,
Attributes | Datatype |
id | Long |
name | String |
contactNumber | String |
costPerDay | Double |
owner | User |
The methods for getters, setters and constructors are given in the template code.
Consider the class UserDAO and define the following method.
Methods:
User getUser(String username)
This method gets the user details from the user table whose username is passed as an argument.
It creates a user object from those details and returns the object.
If the user with that username not available, it returns null.
Cosnider the class HallDAO and define the following methods
Method & Description
void saveHall(Hall hall)
This method gets the hall object as an argument and inserts it into the hall table.
List<Hall> getAllHall()
This method returns the list of halls from database in the ascending order of hall id.
Consider the class DBConnection and define the following static method
Method | Description |
static Connection getConnection() | This method uses "oracle.properties" file as a resource file and gets a connection object for the MySQL database and returns the connection object. |
Consider the driver class Main and use the main method for reading inputs and displaying data after performing required operations.
Table properties:
CREATE TABLE "user"(
id number(19) NOT NULL,
name VARCHAR2(45) NOT NULL,
contact_detail VARCHAR2(45) NOT NULL,
username VARCHAR2(45) NOT NULL,
password VARCHAR2(45) NOT NULL,
PRIMARY KEY (id));
CREATE SEQUENCE "user_seq" START WITH 1 INCREMENT BY 1;
CREATE TABLE hall(
id number(19) NOT NULL,
name VARCHAR2(255) NOT NULL,
contact_detail VARCHAR2(255) NOT NULL,
cost_per_day BINARY_DOUBLE NOT NULL,
owner_id NUMBER(19) NOT NULL,
foreign key(owner_id) references "user"(id),
PRIMARY KEY (id));
CREATE SEQUENCE hall_seq START WITH 1 INCREMENT BY 1;
Use the following code snippet to establish DBConnection:
import java.util.ResourceBundle;
ResourceBundle rb = ResourceBundle.getBundle("oracle");
String url = rb.getString("db.url");
String username = rb.getString("db.username");
String password = rb.getString("db.password");
oracle.properties:
db.url = jdbc:oracle:thin:@localhost:1521:xe
db.username = root
db.password = student
Input and Output format:
The first line of input is the hall details in the CSV format(name, contact, cost per day).
The next line of input is the username.
If the username doesn’t exist, then display Username seems to be wrong!! Enter the correct username:
Otherwise, insert the hall details with the given owner and display all the hall details.
The hall list should be sorted in ascending order of id.
Use "%-15s%-15s%-15s%-15s" while displaying hall objects in tabular form.
All double values should be formatted to 1 decimal place.
Refer to Sample Input and Output for further details and format of the output.
[All Texts in bold corresponds to the input and rest are output]
Sample Input and Output :
Enter the details of hall in csv format:
Ball Room,1234567890,15000
Enter the username:
jim
Username seems to be wrong!! Enter the correct username:
johny
The hall details are:
Name Mobile Cost Owner
Party hall 9874653201 5000.0 John
Dining Hall 9876541230 3000.0 Peter
Disco Hall 9871234560 8000.0 Adam
Conference Hall 7891236540 7500.0 Linda
Meeting Hall 8974102365 9000.0 Tony
Ball Room 1234567890 15000.0 John
PROGRAM:-
@MRProgrammer89
CREATE CLASS < User > :
public class User {
private Long id;
private String name;
private String mobileNumber;
private String username;
private String password;
public User(){}
public User(Long id,String name, String mobileNumber, String username, String password){
this.id=id;
this.name=name;
this.mobileNumber=mobileNumber;
this.username=username;
this.password=password;
}
public Long getId()
{
return this.id;
}
public String getName(){
return this.name;
}
public String getMobileNumber(){
return this.mobileNumber;
}
public String getUsername(){
return this.username;
}
public String getPassword(){
return this.password;
}
public void setId(Long id)
{
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setMobileNumber(String mobileNumber){
this.mobileNumber=mobileNumber;
}
public void setUsername(String username){
this.username=username;
}
public void setPassword(String password){
this.password=password;
}
}
@MRProgrammer89
CREATE CLASS < HallDAO > :
import java.util.ArrayList;
import java.util.List;
import java.sql.*;
public class HallDAO {
public void saveHall(Hall hall) throws Exception{
//write your code here
Connection con = DBConnection.getConnection();
String query = "insert into hall(name,contact_detail,cost_per_day,owner_id) values(?,?,?,?)";
PreparedStatement st = con.prepareStatement(query);
st.setString(1,hall.getName());
st.setString(2,hall.getContactNumber());
st.setDouble(3,hall.getCostPerDay());
st.setInt(4,hall.getOwner().getId().intValue());
int i = st.executeUpdate();
st.close();
con.close();
}
public List<Hall> getAllHall() throws Exception{
List<Hall> hallList = new ArrayList<Hall>();
//write your code here
Connection con = DBConnection.getConnection();
Statement st = con.createStatement();
String query = "select * from hall,\"user\" where owner_id = \"user\".id order by hall.id";
ResultSet rs = st.executeQuery(query);
while(rs.next()){
rs.getInt(1);
String name = rs.getString(2);
String contactNumber = rs.getString(3);
Double costPerDay = rs.getDouble(4);
rs.getInt(5);
rs.getString(6);
User owner=new User();
owner.setName(rs.getString(7));
Hall h = new Hall( name, contactNumber, costPerDay, owner);
hallList.add(h);
}
return hallList;
}
}
@MRProgrammer89
CREATE CLASS < UserDAO > :
import java.sql.*;
public class UserDAO {
public User getUser(String username) throws Exception{
User user = null;
//write your code here
Connection con = DBConnection.getConnection();
Statement st = con.createStatement();
String query ="select * from \"user\" where username='"+username+"'";
int i = st.executeUpdate(query);
if(i==1)
{
ResultSet rs = st.executeQuery(query);
while(rs.next()){
Long id =(long) rs.getInt(1);
String name = rs.getString(2);
String mobileNumber = rs.getString(3);
String un = rs.getString(4);
String password = rs.getString(5);
user = new User(id,name,mobileNumber,un,password);
}
return user;
}
return user;
}
}
@MRProgrammer89
CREATE CLASS < DBConnection > :
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("oracle");
String url = rb.getString("db.url");
String username = rb.getString("db.username");
String password = rb.getString("db.password");
Connection con = DriverManager.getConnection(url,username,password);
return con;
}
}
@MRProgrammer89
CREATE CLASS < Hall > :
public class Hall {
private Long id;
private String name;
private String contactNumber;
private Double costPerDay;
private User owner;
public Hall(){}
public Hall(String name, String contactNumber, Double costPerDay, User owner){
this.name=name;
this.contactNumber=contactNumber;
this.costPerDay=costPerDay;
this.owner=owner;
}
public Long getId()
{
return this.id;
}
public String getName(){
return this.name;
}
public String getContactNumber(){
return this.contactNumber;
}
public Double getCostPerDay(){
return this.costPerDay;
}
public User getOwner(){
return this.owner;
}
public void setId(Long id)
{
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setContactNumber(String contactNumber){
this.contactNumber=contactNumber;
}
public void setCostPerDay(Double costPerDay){
this.costPerDay=costPerDay;
}
public void setOwner(User owner){
this.owner=owner;
}
}
@MRProgrammer89
CREATE CLASS < Main > :
import java.util.*;
public class Main {
public static void main(String args[]) throws Exception {
//write your code here
Scanner sc = new Scanner(System.in);
System.out.println("Enter the details of hall in csv format:");
String str = sc.nextLine();
String[] input = str.split(",");
System.out.println("Enter the username:");
while(true){
String username = sc.nextLine();
UserDAO userDAO = new UserDAO();
User user =userDAO.getUser(username);
if(user!=null){
Hall hall = new Hall(input[0],input[1],Double.parseDouble(input[2]),user);
HallDAO halldao = new HallDAO();
halldao.saveHall(hall);
List<Hall> hallList = new ArrayList<>();
hallList = halldao.getAllHall();
System.out.println("The hall details are:");
System.out.format("%-15s%-15s%-15s%-15s\n","Name","Mobile","Cost","Owner");
for(Hall h :hallList){
System.out.format("%-15s%-15s%-15s%-15s\n",h.getName(),h.getContactNumber(),h.getCostPerDay(),h.getOwner().getName());
}
break;
}
else
System.out.println("Username seems to be wrong!! Enter the correct username:");
}
}
}

MOTIVATION :
“The way we spend our time defines who we are ”
Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.
VISITE MORE BLOGS
THANK YOU....😊