Creating Table:-
Create table rookbook(RoomId varchar(4) PRIMARY KEY, RoomType varchar(20), charges varchar(50), cust varchar(20), mob varchar(20) , status varchar(10))
Insert Values:-
insert into roombook values('1001','Delux','5000','','','Not Booked') ;
insert into roombook values('1002','Super Delux','7000','','','Not Booked');
insert into roombook values('1003','Suit','9500','','','Not Booked') ;
insert into roombook values('2001','Delux','5000','','','Not Booked') ;
insert into roombook values('2002','Super Delux','7000','','','Not Booked');
insert into roombook values('2003','Suit','9500','','','Not Booked') ;
index.html
<html>
<head>
<title>Room Reservation</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="RBServlet" >
Select a room Type
<input type="radio" name="txtType" value="Delux">Delux
<input type="radio" name="txtType" value="Super Delux">Super Delux
<input type="radio" name="txtType" value="Suit">Suit<br> <br>
Enter Your Name<input type="text" name="txtCust" ><br> <br>
Enter Mobile No.<input type="text" name="txtMob" ><br><br>
<input type="reset"value="Reset" >
<input type="submit" value="Book Room">
</form>
</body>
</html>
RBServlet.java
package mypack;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.ejb.EJB;
import mybeans.RRBean;
public class RBServlet extends HttpServlet {
@EJB RRBean obj;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ PrintWriter out=response.getWriter();
String rt=request.getParameter("txtType");
String cn=request.getParameter("txtCust");
String cm=request.getParameter("txtMob");
String msg = obj.roomBook(rt, cn, cm);
out.println(msg);
}
}
RRBean.java
package mybeans;
import javax.ejb.Stateless;
import java.sql.*;
/**
*
* @author RupamSolutions
*/
@Stateless public class RRBean {
public RRBean()
{
}
public String roomBook(String rt, String cn, String cm)
{
String msg="";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:derby://localhost:1527/mysql","root","root");
String query="select * from roombook where RoomType=? and status='Not Booked'";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1,rt);
ResultSet rs= pst.executeQuery();
if(rs.next())
{
String rno=rs.getString(1);
PreparedStatement stm1 = con.prepareStatement("update roombook set cust=? where RoomId=? ");
PreparedStatement stm2 = con.prepareStatement("update roombook set mob=? where RoomId=? ");
PreparedStatement stm3 = con.prepareStatement("update roombook set status=? where
RoomId=? ");
stm1.setString(1,cn);
stm1.setString(2,rno);
stm2.setString(1,cm);
stm2.setString(2,rno);
stm3.setString(1, "Booked");
stm3.setString(2,rno);
stm1.executeUpdate();
stm2.executeUpdate();
stm3.executeUpdate();
msg = "Room "+rno+ " Booked Charges = "+rs.getString(3);
}
else
{
msg = "Room "+rt+ " currently Not available";
}
}
catch(Exception e)
{
msg=""+e;
}
return msg;
}
}
0 Comments