Professional Documents
Culture Documents
on
HOTEL MANAGEMENT
SYSTEM USING JAVA
NETBEANS AND MYSQL
Submitted By:
Gurpreet Singh
XII – Sci
Roll No.:15
Under Supervision of:
Mr. Tarun Pandey
PGT (IT)
I would like to express my gratitude towards Tarun Sir for their kind
co-operation and encouragement which help me in completion of this
project.
-Gurpreet Singh
XII – ‘Sci’
Roll No. 15
CONTENTS
Topic Page No.
o Basic Steps for Starting Program……..……..……………… 5
o Managing Database and Tables……………………………… 6
o Creating Project and Frames………………………………….. 7
o Designing and Coding Frames..………………………………. 8
Steps for Starting Program
Creating New Project
Open Netbeans IDE, click on File menu in menu bar and click on New Project, select
JAVA and then JAVA Application, then click Next button. Now assign your Project Name,
select destination directory and click on Finish button. In this project we take
Hotel_Management_System.
Now under desired package, click on Source Packages and right click on desired package
and select New JFrameForm and give desired name to it. Do this for all frames you want
to import for e.g login, main_menu etc.
Designing Frame
To design a frame, drag components from right side corner in Netbeans to frame that
you want e.g. Textbox, Labels, Combobox, Menubar, Command button etc. and arrange
them according to your choice.
Assigning Variables
Now assign variables according to your choice or let them remain as it is according to
your choice. Variables must be unique for every component in the single frame. Variable
are case sensitive.
Right Click on command button or any other component and select events and the
action that you want to be performed. For coding you must declare the datatypes and
identifiers.
JAVA shows compile error as you are writing the program. To run the program go to
‘Run’ in menu bar and click on ‘Run main Project’. Program will be executed in new
window.
To build the main project, go to ‘Run’ in menu bar and click on ‘Clean and Build Main
Project’. The program will be built under the desired directory of project in ‘dist’ folder.
The extension of application will be .jar. Now you can run the program by direct clicking
on it.
Managing Database and Tables
Create Database HOTEL;
USE HOTEL;
);
create table staff(id int(2) Primary Key, name varchar(25), address varchar(100), city
varchar(25), state varchar(25), country varchar(15), cno int(10), post varchar(10), salary
double(8,2));
create table user(uid varchar(15) Primary Key Not Null, name varchar(25), mtype
varchar(15), pwd varchar(25) Not Null, cno varchar(10));
create table temp(rn1 varchar(15), rn2 varchar(15), rn3 varchar(15), rn4 varchar(15),
dt1 date, dt2 date, nd int(3));
Create a project Hotel_Management_System.
Create the following frames under the package.
Login.java
About.java
Availability.java
Bookingreport.java
Bookroom.java
Changepwd.java
Customermanag.java
Login.java
Mainmenu.java
Mainmenur.java
Register.java
Roommanag.java
Roomreport.java
Staffmanag.java
Staffreport.java
Usermanag.java
Userreport.java
Add the following two lines under the package in all frames as required.
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
Now add Library to the project. Right click on the Library folder under the project
directory in the left side pane. Now click on Add Library option and select MySQL JDBC
Driver and click Add. It’s done. Also add jCalender library.
JFrame—login.java
Create following frame with 3 labels, 2 Text Fields, 1 ComboBox and 3 Command
Buttons.
Put background at one label door image on one of the Command Buttons. Arrange the
Items as shown below:
while (rs.next()) {
String rn = rs.getString("room_no");
String rt = rs.getString("room_type");
String bt = rs.getString("room_bed");
String cpr = rs.getString("room_rate");
model.addRow(new Object[] {rn, rt, bt, cpr});
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
catch(Exception ex){}
try {
roomno=jLabel2.getText();
bed=jLabel4.getText();
roomt=jLabel6.getText();
rate=jLabel8.getText();
dt1=jLabel10.getText();
dt2=jLabel12.getText();
cname=jTextField1.getText();
cadd=jTextField3.getText();
ccity=jTextField4.getText();
cstat=jTextField5.getText();
ccoun=jTextField6.getText();
cph=jTextField7.getText();
cproof=jTextField10.getText();
cadult=jTextField12.getText();
cchild=jTextField13.getText();
cmars=jTextField8.getText();
cidno=jTextField11.getText();
cnation=jTextField9.getText();
cpur=jTextField14.getText();
diffd=jTextField15.getText();
String amt=jTextField16.getText();
int code=JOptionPane.showConfirmDialog(this,"Room No. " + roomno + " is booked for Mr./Ms. " + cname + "
for "
+ diffd+"
days..!","Confirmation",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(code==JOptionPane.YES_NO_OPTION) {
String insqlcust="insert into custdetail
(Cust_name,Cust_add,Cust_city,Cust_state,Cust_country,Cust_ph,Cust_proof,"
+ "Cust_adult,Cust_child,Cust_mar_stat,Cust_id_no,Cust_nation,Cust_purpos,Cust_bill,book_id) "
+ "values ('" + cname + "','" + cadd + "','" + ccity + "','" + cstat + "','" + ccoun + "','" + cph + "','" +
cproof
+ "','" + cadult + "','" + cchild + "','" + cmars + "','" + cidno +"','" + cnation + "','" + cpur + "'," + amt
+ ",'" + bookid + "');";
smt1=con.createStatement();
int ins=smt1.executeUpdate(insqlcust);
String insqlbook="insert into bookingtable (book_id,room_no,date_fro,date_to,no_of_day) values( '"
+bookid+ "','" +roomno+ "','" +dt1+ "','" +dt2+ "','" +diffd+ "');";
int ins1=smt1.executeUpdate(insqlbook);
JOptionPane.showMessageDialog( this,"Room Booked...! Plz collect Rs. "+amt+" Booking Id: "+bookid);
this.setVisible(false);
}
else {
JOptionPane.showMessageDialog(rootPane, "Fill Correct Data...");
}
}
catch(Exception ex) {
JOptionPane.showMessageDialog(rootPane, ex.getMessage());
}
}
catch(Exception ec)
{}
}
Declare the following data types under the public class staffmng
String name,address,city,state,country,cno,spost,salary;
int id;
Click on Source Tab and create the following classes
public void displayRecord(){
try
{
sid.setText(rs.getString("id"));
lsid.setText(rs.getString("id"));
sname.setText(rs.getString("name"));
sadd.setText(rs.getString("address"));
scity.setText(rs.getString("city"));
sstate.setText(rs.getString("state"));
scountry.setText(rs.getString("country"));
scno.setText(rs.getString("cno"));
post.setSelectedItem(rs.getString("post"));
sal.setText(rs.getString("salary"));
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
}
catch(Exception ec)
{}
}
----------------------------------------------------------------------------------------------------------------------------------------
public void fetchFromTextF(){
id=Integer.parseInt(sid.getText());
name=sname.getText();
address=sadd.getText();
city=scity.getText();
state=sstate.getText();
country=scountry.getText();
cno=scno.getText();
spost=(String)post.getSelectedItem();
salary=sal.getText();
}
------------------------------------------------------------------------------------------------------------------------------------------
public void clearTextF(){
sid.setText("");
lsid.setText("00");
sname.setText("");
sadd.setText("");
scity.setText("");
sstate.setText("");
scountry.setText("");
scno.setText("");
post.setSelectedItem(null);
sal.setText("");}
Code for ‘Click Here to Search By Name’ Button
String oname=JOptionPane.showInputDialog(null,"Search By Name","Enter Name to Search");
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM staff where name like '"+oname+"%'";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.first();
displayRecord();
} catch(Exception e) {
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
upd.setEnabled(true);
del.setEnabled(true);
Code for ‘Click Here to Search By Booking Id’ Button
String staffid=JOptionPane.showInputDialog(null,"Search By Booking ID","Enter book id to Search");
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM staff where id='"+staffid+"'";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.first();
displayRecord();
} catch(Exception e) {
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
upd.setEnabled(true);
del.setEnabled(true);
Code for ‘First’ Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM staff";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.first();
displayRecord();
} catch(Exception e) {
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
Code for ‘Last’ Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM staff";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.last();
displayRecord();
} catch(Exception e){
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
Code for ‘New’ Button
clearTextF();
savecmd.setEnabled(true);
Code for ‘Save’ Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM staff";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){}
fetchFromTextF();
int code = JOptionPane.showConfirmDialog(this, "New member: " + name + " will be created",
"Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String updatequery = "insert into staff
values('"+id+"','"+name+"','"+address+"','"+city+"','"+state+"','"+country+"','"+cno+"','"+spost+"','"+salary+"')";
stmt = con.createStatement();
int success = stmt.executeUpdate(updatequery);
JOptionPane.showMessageDialog(this, "Record Inserted");
}
else{
JOptionPane.showMessageDialog(rootPane, "Already Exists. Check Details.");}
} catch (Exception ex) {
JOptionPane.showMessageDialog(rootPane, ex.getMessage());
}
savecmd.setEnabled(false);
Code for ‘Update’ Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM staff";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
fetchFromTextF();
int code = JOptionPane.showConfirmDialog(this, "Member: " + id + " will be updated", "Confirm",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String updatequery = "update staff set name='"+name+"', address='"+address+"', city='"+city+"',
state='"+state+"', country='"+country+"', cno='"+cno+"', post='"+spost+"', salary='"+salary+"' where id='"+id+"'";
stmt = con.createStatement();
int success = stmt.executeUpdate(updatequery);
JOptionPane.showMessageDialog(this, "Updated Successfully");
} else {JOptionPane.showMessageDialog(this, "Check details...");
}}
else{
JOptionPane.showMessageDialog(rootPane, "Some Problem. Check Details.");}
} catch (Exception ex) {
JOptionPane.showMessageDialog(rootPane, ex.getMessage());
}
upd.setEnabled(false);
Code for ‘Delete’ Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM staff";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){}
int did=Integer.parseInt(sid.getText());
int code = JOptionPane.showConfirmDialog(this, "Member: " + did + " will be deleted from database.",
"Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String updatequery = "DELETE from staff where id='" +did+ "'";
stmt = con.createStatement();
int success = stmt.executeUpdate(updatequery);
JOptionPane.showMessageDialog(this, "Record Deleted");
clearTextF();
} else {JOptionPane.showMessageDialog(this, "Problem in deletion. Retry");
}
} catch (Exception ex) {
ex.printStackTrace();
}
Code for ‘Close’ Button
this.setVisible(false);
Code for ‘Clear’ Button
clearTextF();
JFrame- usermanag.java
Create frame with 6 labels, 1 panel, 3 textfields, 1 comboBox and 8 command. Arrange
them as shown below.
Declare the following data types under the public class staffmng
String name,userid,cno,mtype;
Click on Source Tab and create the following classes
public void displayRecord(){
try
{
uid.setText(rs.getString("uid"));
luid.setText(rs.getString("uid"));
uname.setText(rs.getString("name"));
ucno.setText(rs.getString("cno"));
type.setSelectedItem(rs.getString("mtype"));
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
}
catch(Exception ec)
{}
}
--------------------------------------------------------------------------------------------------------------------------------------
public void fetchFromTextF(){
userid=uid.getText();
name=uname.getText();
cno=ucno.getText();
mtype=(String)type.getSelectedItem();
}
-----------------------------------------------------------------------------------------------------------------------------------------
public void clearTextF(){
uid.setText("");
luid.setText("00");
uname.setText("");
ucno.setText("");
type.setSelectedItem(null);}
Code for ‘Click here to Search By name’ Button
String oname=JOptionPane.showInputDialog(null,"Search By Name","Enter Name to Search");
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM user where name like '"+oname+"%'";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.first();
displayRecord();
} catch(Exception e) {
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
upd.setEnabled(true);
del.setEnabled(true);
Code for ‘Click here to Search By User Id’ Button
String userid=JOptionPane.showInputDialog(null,"Search By Booking ID","Enter book id to Search");
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM user where uid like '"+userid+"%'";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.first();
displayRecord();
} catch(Exception e) {
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
upd.setEnabled(true);
del.setEnabled(true);
Code for ‘First’ Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM user";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.first();
displayRecord();
} catch(Exception e) {
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
Code for ‘last’ Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM user";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.last();
displayRecord();
} catch(Exception e){
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
Code for ‘Delete’ Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM user";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){}
String usid=uid.getText();
int code = JOptionPane.showConfirmDialog(this, "User: " + usid + " will be deleted from database.",
"Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String updatequery = "DELETE from user where uid='" +usid+ "'";
stmt = con.createStatement();
int success = stmt.executeUpdate(updatequery);
JOptionPane.showMessageDialog(this, "Record Deleted");
clearTextF();
} else {JOptionPane.showMessageDialog(this, "Problem in deletion. Retry");
}
} catch (Exception ex) {
ex.printStackTrace();
}
Code for ‘Update’ Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM user";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
fetchFromTextF();
int code = JOptionPane.showConfirmDialog(this, "User: " + userid + " will be updated", "Confirm",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String updatequery = "update user set name='"+name+"', cno='"+cno+"', mtype='"+mtype+"' where
uid='"+userid+"'";
stmt = con.createStatement();
int success = stmt.executeUpdate(updatequery);
JOptionPane.showMessageDialog(this, "Updated Successfully");
} else {JOptionPane.showMessageDialog(this, "Check details...");
}}
else{
JOptionPane.showMessageDialog(rootPane, "Some Problem. Check Details.");}
} catch (Exception ex) {
JOptionPane.showMessageDialog(rootPane, ex.getMessage());
}
upd.setEnabled(false);
Code for ‘Clear’ Button
clearTextF();
Code for ‘Close’ Button
this.setVisible(false);
JFrame- roomreport.java
Create frame with 2 labels, 1 table and 2 command buttons. Arrange them as shown
below.