You are on page 1of 41

A Project Report

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)

CAMBRIDGE CONVENT SCHOOL


Affiliated to CBSE New Delhi
Powayan, Shahjahanpur Mob No:- 9415510940
Acknowledgment
I have taken efforts in this project. However, it would not have been
possible without the kind support and help of many teachers and
parents. I would like to extend my sincere thanks to all of them.

I am highly indebted to Cambridge Convent for their guidance and


constant supervision as well as for providing necessary information
regarding the project & also for their support in completing the
project.

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.

I would like to express my special gratitude and thanks to industry


persons for giving me such attention and time.

My thanks and appreciations also go to my colleague in developing the


project and people who have willingly helped me out with their
abilities.

-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.

Coding the Program

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.

Compiling and Running the program

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.

Building main project

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 `bookingtable` (


`book_id` int(3) Primary Key,
`room_no` varchar(3),
`date_fro` date,
`date_to` date,
`no_of_day` int(3)
);

INSERT INTO `bookingtable` (`book_id`,`room_no`,`date_fro`,`date_to`,`no_of_day`)


VALUES
(1,'101','2014-10-21','2014-10-22',NULL)

CREATE TABLE `custdetail` (


`Cust_name` varchar(30),
`Cust_add` varchar(40),
`Cust_city` varchar(30),
`Cust_state` varchar(30),
`Cust_country` varchar(30),
`Cust_ph` varchar(10),
`Cust_proof` varchar(20) DEFAULT NULL,
`Cust_adult` varchar(2),
`Cust_child` varchar(2) DEFAULT NULL,
`Cust_mar_stat` varchar(15),
`Cust_id_no` varchar(30),
`Cust_nation` varchar(30),
`Cust_purpos` varchar(30),
`Cust_bill` int(5),
`book_id` int(3)
);
CREATE TABLE `roomdetail` (
`room_no` varchar(3) Primary Key NOT NULL,
`room_type` varchar(10) NOT NULL,
`room_rate` int(4) NOT NULL,
`room_bed` varchar(6) NOT NULL

);

INSERT INTO `roomdetail` (`room_no`,`room_type`,`room_rate`,`room_bed`) VALUES


('101','Non AC',700,'Single'),
('102','Non AC',700,'Single'),
('103','Non AC',700,'Single'),
('104','Non AC',700,'Single'),
('105','Non AC',700,'Single'),
('106','Non AC',700,'Single'),
('107','Non AC',700,'Single'),
('108','Non AC',700,'Single'),
('109','Non AC',700,'Single'),
('110','Non AC',700,'Single'),
('111','Non AC',1000,'Double'),
('112','Non AC',1000,'Double'),
('113','Non AC',1000,'Double'),
('114','Non AC',1000,'Double'),
('115','Non AC',1000,'Double'),
('116','AC',1500,'Single'),
('117','AC',1500,'Single'),
('118','AC',1500,'Single'),
('119','AC',1500,'Single'),
('120','AC',1500,'Single'),
('121','AC',1500,'Single'),
('122','AC',1500,'Single'),
('123','AC',1500,'Single'),
('124','AC',2000,'Double'),
('125','AC',2000,'Double'),
('126','AC',2000,'Double'),
('127','AC',2000,'Double'),
('128','AC',2000,'Double'),
('129','AC',2000,'Double'),
('130','AC',2000,'Double');

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;

Also add following lines under the class.


Connection con;
Statement stmt;
ResultSet rs;

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:

Coding for Door


pwd.setVisible(true);
uid.setVisible(true);
jLabel1.setVisible(true);
jLabel2.setVisible(true);
jButton2.setVisible(true);
mtype.setVisible(true);
jButton1.setVisible(false);

Coding for Login Button


try{
Class.forName("java.sql.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/hotel","root","tiger");
String member=(String)mtype.getSelectedItem();
String sql = "select * from user where uid='"+uid.getText()+"' and mtype='"+member+"'";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
String password=rs.getString("pwd");
String memtype=rs.getString("mtype");
String entpwd=pwd.getText();

if (password.equals(entpwd) && memtype.equals(member)){


if(member=="Manager"){
new mainmenu().setVisible(true);
this.setVisible(false);}
else{new mainmenur().setVisible(true);
this.setVisible(false);}
JOptionPane.showMessageDialog(rootPane, rs.getString("uid")+", Welcome to Cambridge Hotel.");
}
else{JOptionPane.showMessageDialog(null, "Please try again");
}
}
else{JOptionPane.showMessageDialog(null, "Please try again");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}

Coding for Register Button


new register().setVisible(true);
this.setVisible(false);
JFrame- availability.java
Create a frame with 6 labels, 2 Date Chooser, 1 table and 3 command buttons and
arrange them as shown below. Set the property of jTextField1 as uneditable.

Coding for Check Button


java.util.Date dt1,dt2;
String diffday;
DefaultTableModel model = (DefaultTableModel) JTable.getModel();
int rows = model.getRowCount();
if (rows > 0) {
for (int i = 0; i < rows; i++) {
model.removeRow(0);
}}
try
{
Class .forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
dt1=jDateChooser1.getDate();
dt2=jDateChooser2.getDate();
String strdtver1=(String) sdf.format(jDateChooser1.getDate());
String strdtver2=(String) sdf.format(jDateChooser2.getDate());
long diff=((dt2.getDate()-dt1.getDate()));
diffday=Long.toString(diff);
jTextField1.setText(diffday);
String sql="SELECT room_no, room_type, room_bed, room_rate FROM roomdetail "
+ "where room_no NOT IN (SELECT room_no FROM bookingtable WHERE DATE(date_to) >= '"
+ strdtver1 + "' AND DATE(date_fro) <= '" + strdtver2 + "');";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);

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());
}

Coding for Book Button


Object obj1,obj2,obj3,obj4,obj5,obj6;
int rno=JTable.getSelectedRow();
obj1 = JTable.getValueAt(rno, 0);
obj2 = JTable.getValueAt(rno, 1);
obj3 = JTable.getValueAt(rno, 2);
obj4 = JTable.getValueAt(rno, 3);
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dt1=(String) sdf.format(jDateChooser1.getDate());
String dt2=(String) sdf.format(jDateChooser2.getDate());
String nd=jTextField1.getText();
try
{
Class .forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql="SELECT *from temp";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {}
String del="delete from temp";
int rowsdel=stmt.executeUpdate(del);
String query="insert into temp values('"+obj1+"','"+obj2+"','"+obj3+"','"+obj4+"','"+dt1+"','"+dt2+"','"+nd+"')";
int rowseff=stmt.executeUpdate(query);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
bookroom br=new bookroom();
br.setVisible(true);

Coding for JTable(Event- MouseClicked)


Object obj1,obj2,obj3,obj4,obj5,obj6;
int rno=JTable.getSelectedRow();
Object obj0=JTable.getValueAt(rno, 3);
obj1 = JTable.getValueAt(rno, 0);
obj2 = JTable.getValueAt(rno, 1);
obj3 = JTable.getValueAt(rno, 2);
obj4 = JTable.getValueAt(rno, 3);
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dt1=(String) sdf.format(jDateChooser1.getDate());
String dt2=(String) sdf.format(jDateChooser2.getDate());

Coding for Close Button


this.setVisible(false);
JFrame- bookingreport.java
Create a frame 2 labels, 1 Table and 2 Command Buttons. Arrange them as following. Set the columns
as uneditable in the Table.

Coding for Display All Button


DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
int rows = model.getRowCount();
if (rows > 0) {
for (int i = 0; i < rows; i++) {
model.removeRow(0);
}}
try
{
Class .forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql="SELECT cust_name, cust_id_no, bookingtable.* from custdetail, bookingtable;";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("cust_name");
String idp = rs.getString("cust_id_no");
String bid = rs.getString("book_id");
String rno = rs.getString("room_no");
String datefrom = rs.getString("date_fro");
String dateto = rs.getString("date_to");
String nod = rs.getString("no_of_day");
model.addRow(new Object[] {bid, name, idp, rno, datefrom, dateto, nod});
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
Coding for Exit Button
this.setVisible(false);
JFrame- bookroom.java
Create the following frame with 29 labels, 15 textfields, 1 panel and 2 command buttons. Arrange
them as shown below. Set property of jTextField15, jTextField16 as uneditable.

Coding for Book Button


String roomno,bed,roomt,rate,strdtver1,strdtver2;
String cname,cadd,ccity,cstat,ccoun,cph,cproof;
String cadult,cchild,cmars,cidno,cnation,cpur,cbill;
String diffd;
int bookid=0;
String dt1,dt2;
try{stmt=con.createStatement();
rs=stmt.executeQuery("select *from bookingtable");
rs.last();
bookid=rs.getInt("book_id")+1;

}
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());
}

Coding for Cancel Button


this.setVisible(false);
JFrame- customermanag.java

Click on Source tab and add the following classes


public void disrecors(){
try
{
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String bkid=cno.getText();
String sql = "SELECT * FROM bookingtable where book_id='"+bkid+"'";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
while (rs.next()){
rno.setText(rs.getString("room_no"));
arr.setText(rs.getString("date_fro"));
dep.setText(rs.getString("date_to"));
}}
catch(Exception e){
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}}
------------------------------------------------------------------------------------------------------------------------------------------
public void displayRecord(){
try
{
String bkid=rs.getString("book_id");
cno.setText(rs.getString("book_id"));
vname.setText(rs.getString("cust_name"));
vadd.setText(rs.getString("cust_add"));
vcity.setText(rs.getString("cust_city"));
vstate.setText(rs.getString("cust_state"));
vcountry.setText(rs.getString("cust_country"));
vphone.setText(rs.getString("cust_ph"));
vidproof.setText(rs.getString("cust_proof"));
vidpfno.setText(rs.getString("cust_id_no"));
vmars.setText(rs.getString("cust_mar_stat"));
vnationality.setText(rs.getString("cust_nation"));
noofadult.setText(rs.getString("cust_adult"));
noofchild.setText(rs.getString("cust_child"));
purofvisit.setText(rs.getString("cust_purpos"));
bid.setText(rs.getString("book_id"));
bamt.setText(rs.getString("cust_bill"));
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");

}
catch(Exception ec)
{}
}

Coding for ‘Click Here to Search By Name’ Button


String name=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 custdetail where cust_name like '"+name+"%'";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.first();
displayRecord();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
disrecors();
Coding for ‘Click Here to Search By Booking Id’ Button
String name=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 custdetail where book_id='"+name+"'";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.first();
displayRecord();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
disrecors();

Coding for ‘First’ Button


try
{
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM custdetail";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.first();
displayRecord();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
disrecors();
Coding for ‘Last’ Button
try
{
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM custdetail";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
rs.last();
displayRecord();
}
catch(Exception e){
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
disrecors();
Coding for ‘Clear’ Button
cno.setText("00000");
vname.setText("");
vadd.setText("");
vcity.setText("");
vstate.setText("");
vcountry.setText("");
vphone.setText("");
vidproof.setText("");
vidpfno.setText("");
vmars.setText("");
vnationality.setText("");
noofadult.setText("");
noofchild.setText("");
purofvisit.setText("");
bid.setText("");
bamt.setText("");
rno.setText("00000");
arr.setText("");
dep.setText("");
Coding for ‘Close’ Button
this.setVisible(false);
JFrame- mainmenu.java
Create frame with 1 Menu bar, 6 menus , 11 menuitems and 10 command buttons and
1 label for background. Arrange them as shown below.

Code for ‘Check Availability and Booking’ button


new availability().setVisible(true);
Code for ‘Manage Staff’ button
new staffmng().setVisible(true);
Code for ‘Manage Customers’ button
new customermanag().setVisible(true);
Code for ‘Manage Users’ button
new usermanag().setVisible(true);
Code for ‘Manage Rooms’ button
new roommanag().setVisible(true);
Code for ‘See Booking Report’ button
new bookingreport().setVisible(true);
Code for ‘See Staff List’ button
new staffreport().setVisible(true);
Code for ‘See Room List’ button
new roomreport().setVisible(true);
Code for ‘See User List’ button
new userreport().setVisible(true);
Code for ‘Log Out’ button
int code = JOptionPane.showConfirmDialog(this, "Do you really want to logout?", "Do you really want to
logout?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
this.setVisible(false);
new login().setVisible(true);}
JFrame- register.java
Create frame with 8 labels, 5 textfields, 1 comboBox and 3 command buttons. Arrange
them as shown below. Also assign variable names.

Code for ‘Register’ Button


try{
Class.forName("java.sql.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/hotel","root","tiger");
stmt=con.createStatement();
String sql="select*from user";
rs=stmt.executeQuery(sql);
if(lic.getText().equals("HiM")){
String query="insert into user values('"+uid.getText()+"','"+name.getText()+"','"+type.getSelectedItem()
+"','"+pwd.getText()+"','"+ucno.getText()+"')";
int upd=stmt.executeUpdate(query);
JOptionPane.showMessageDialog(rootPane,"New User: "+uid.getText()+", Created Successfully");
} else{
JOptionPane.showMessageDialog(rootPane,"Please enter correct License code.");}
} catch(Exception ex) {
JOptionPane.showMessageDialog(rootPane, ex.getMessage());
ex.printStackTrace();}
Code for 'Back to Login’ Button
this.setVisible(false);
new login().setVisible(true);

Code for ‘Exit’ Button


System.exit(0);
JFrame- roommanag.java
Create frame with 6 labels, 1 panel, 4 textfields and 11 command. Arrange them as
shown below.

Click on source tab, then create the following public classes


public final void displayRecord(){
try{
roomno.setText(rs.getString("room_no"));
roomtype.setText(rs.getString("room_type"));
roomrate.setText(Integer.toString(rs.getInt("room_rate")));
bedtype.setText(rs.getString("room_bed"));
}
catch(Exception ex){}
}
--------------------------------------------------------------------------------------------------------------------------------
public void fetchFromTextF(){
rno=roomno.getText();
rtype=roomtype.getText();
rrate=Integer.parseInt(roomrate.getText());
rbed=bedtype.getText();
}
---------------------------------------------------------------------------------------------------------------------------------
public void disableTextF(){
roomno.setEditable(false);
roomtype.setEditable(false);
roomrate.setEditable(false);
bedtype.setEditable(false);
}
--------------------------------------------------------------------------------------------------------------------------------
public void clearTextF(){
try
{
roomno.setText(null);
roomtype.setText(null);
roomrate.setText(null);
bedtype.setText(null);
}
catch(Exception ex)
{}
}
--------------------------------------------------------------------------------------------------------------------------
public void enableTextF(){
roomno.setEditable(true);
roomtype.setEditable(true);
roomrate.setEditable(true);
bedtype.setEditable(true);
}
Coding for Click Here to Search By Room No. Button
try{
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM roomdetail where room_no='"
+(JOptionPane.showInputDialog(null,"Enter Room No. to search"))+"'";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
displayRecord();
}
else{
JOptionPane.showMessageDialog(rootPane, "Room Not Exist...");}
}
catch (Exception ex){
JOptionPane.showMessageDialog(rootPane, "Room Not Exist");
}
edit.setEnabled(true);del.setEnabled(true);
Coding for First Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM roomdetail";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
rs.first();
displayRecord();
} catch (Exception ex) {
}
Coding for Last Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM roomdetail";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
rs.last();
displayRecord();
} catch (Exception ex) {
}
Coding for Previous Button
try {
rs.previous();
if (rs.isFirst()) {
rs.first();
}
displayRecord();
} catch (Exception ex) {
}
Coding for Next Button
try {
rs.next();
if (rs.isLast()) {
rs.last();
}
displayRecord();
} catch (Exception ex) {
JOptionPane.showMessageDialog(rootPane, ex.getMessage());}
Coding for Clear Button
clearTextF();
Coding for New Button
clearTextF();
enableTextF();
savecmd.setEnabled(true);
Coding for Edit Button
enableTextF();
updcmd.setEnabled(true);
roomno.setEnabled(false);
Coding for Delete Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM roomdetail";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){}
rno=roomno.getText();
int code = JOptionPane.showConfirmDialog(this, "Room No" + rno + " will be deleted from database.",
"Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String updatequery = "DELETE from roomdetail where room_no='" +rno+ "'";
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();
}
Coding for Save Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM roomdetail";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
fetchFromTextF();
int code = JOptionPane.showConfirmDialog(this, "New Room No: " + rno + " will be created",
"Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String updatequery = "insert into roomdetail values('"+rno+"','"+rtype+"','"+rrate+"','"+rbed+"')";
stmt = con.createStatement();
int success = stmt.executeUpdate(updatequery);
JOptionPane.showMessageDialog(this, "Record Inserted");
} else {JOptionPane.showMessageDialog(this, "Check details...");
}}
else{
JOptionPane.showMessageDialog(rootPane, "Already Exists. Check Details.");}
} catch (Exception ex) {
JOptionPane.showMessageDialog(rootPane, ex.getMessage());
}
savecmd.setEnabled(false);
Coding for Update Button
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM roomdetail";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
fetchFromTextF();
int code = JOptionPane.showConfirmDialog(this, "Room No: " + rno + " will be updated", "Confirm",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String updatequery = "update roomdetail set room_type='"+rtype+"', room_rate='"+rrate+"',
room_bed='"+rbed+"' where room_no='"+rno+"'";
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());
}
updcmd.setEnabled(false);
JFrame- staffmanag.java
Create frame with 12 labels, 1 panel, 8 textfields, 1 comboBox and 10 command
buttons. Arrange them as shown below.

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.

Coding for Display All Button


DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
int rows = model.getRowCount();
if (rows > 0) {
for (int i = 0; i < rows; i++) {
model.removeRow(0);
}}
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String sql = "SELECT * FROM roomdetail";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
while (rs.next()) {
String roomno=rs.getString("room_no");
String roomtype=rs.getString("room_type");
String roomrate=rs.getString("room_rate");
String bedtype=rs.getString("room_bed");
model.addRow(new Object[] {roomno, roomtype, roomrate, bedtype});
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
Coding for Exit Button
this.setVisible(false);
JFrame- staffreport.java
Create frame with 3 labels, 1 textfield, 1 table and 2 command buttons. Arrange them as
shown below.

Coding for Display All Button


DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
int rows = model.getRowCount();
if (rows > 0) {
for (int i = 0; i < rows; i++) {
model.removeRow(0);
}}
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);
while (rs.next()) {
String sid=rs.getString("id");
String sname=rs.getString("name");
String sadd=rs.getString("address");
String scity=rs.getString("city");
String sstate=rs.getString("state");
String scno=rs.getString("cno");
String post=rs.getString("post");
String sal=rs.getString("salary");
model.addRow(new Object[] {sid, sname, sadd, scity, sstate, scno, post, sal});
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
Coding for Exit Button
this.setVisible(false);
Right Click on TextField select Events>>KeyReleased and Add the following
code
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
int rows = model.getRowCount();
if (rows > 0) {
for (int i = 0; i < rows; i++) {
model.removeRow(0);
}}
try {
Class.forName("java.sql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","tiger");
String query = "SELECT * FROM staff where name like'"+name.getText()+"%'";
stmt=con.createStatement();
rs=stmt.executeQuery(query);
while (rs.next()) {
String sid=rs.getString("id");
String sname=rs.getString("name");
String sadd=rs.getString("address");
String scity=rs.getString("city");
String sstate=rs.getString("state");
String scno=rs.getString("cno");
String post=rs.getString("post");
String sal=rs.getString("salary");
model.addRow(new Object[] {sid, sname, sadd, scity, sstate, scno, post, sal});
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
JFrame- userreport.java
Create frame with 2 labels, 1 table and 2 command buttons. Arrange them as shown
below.

Coding for Display All Button


DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
int rows = model.getRowCount();
if (rows > 0) {
for (int i = 0; i < rows; i++) {
model.removeRow(0);
}}
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);
while (rs.next()) {
String uid=rs.getString("uid");
String uname=rs.getString("name");
String ucno=rs.getString("cno");
String type=rs.getString("mtype");
model.addRow(new Object[] {uid, uname, ucno, type});
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
Coding for Exit Button
this.setVisible(false);

You might also like