Hey ,
I'm trying to do an online library system using jsp, servlet, java and mysql and DAO concepts. i have develop delete book function. before delete it is searching whether that book is in the db or not. if it is yes then user can delete the book. when i run it, it is working perfectly for true values but for false it did not continue as a wrong or right output. why is that
here my jsp form
<div id="page">
<form name="input" action="book" method="post">
Book Id : <input type="text" name="txtBid" value ="<%=request.getAttribute("txtBid")%>" class="resizedTextbox"><br><br>
<input type="submit" name="operation" value="Search Book" onclick= <a href="main/Servelet"</a>
<!--<br><br>
Book Name : <input type="text" name="txtName" value="<%=request.getAttribute("txtName")%>" class="resizedTextbox"><br><br>
<input type="submit" name="operation" value="Delete Book" onclick= <a href="main/Servelet"/>
<!--
<input type="reset" value="Cancel/Clear" id="3" onclick= <a href="main/Servelet"/>
<p><%=request.getAttribute("deleteBookMsg") %></p>
</form>
<br><br><br><br><br><br><br><br><br><br><br>
</div>
here my servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Inside book dopost");
String operation = request.getParameter("operation");
if(operation!=null && operation.equalsIgnoreCase("addBookbtn")){
addBook(request,response);
}else if(operation!=null && operation.equalsIgnoreCase("Delete Book")){
deleteBook(request,response);
}else if(operation!=null && operation.equalsIgnoreCase("search Book")){
searchBook(request,response);
}
}
private void searchBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Inside searchBook");
String bname=null;
book b =new book();
bookService bs = new bookServiceImpl();
String b_id=request.getParameter("txtBid");
b.setB_id(b_id);
bname = bs.searchbook(b);
System.out.println(bname);
if(bname!=null){
request.setAttribute("txtBid", b.getB_id());
String message = "Click Delete button to delete the above book";
request.setAttribute("deleteBookMsg", message);
request.setAttribute("txtName",bname);
}else{
String message = "Book does not Exist";
request.setAttribute("deleteBookMsg", message);
}
RequestDispatcher rd = getServletContext().getRequestDispatcher("/deleteBook.jsp");
rd.forward(request, response);
}
private void deleteBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Inside deleteBook");
book b =new book();
bookService bs = new bookServiceImpl();
String b_id=request.getParameter("txtBid");
b.setB_id(b_id);
System.out.println("llll"+b_id);
if(bs.deletebook(b)){
String message = "Successfully Deleted "+ b.getB_id();
request.setAttribute("deleteBookMsg", message);
}
else{
String message = "No Registered Book";
request.setAttribute("deleteBookMsg", message);
}
RequestDispatcher rd = getServletContext().getRequestDispatcher("/deleteBook.jsp");
rd.forward(request, response);
}
here my service/business logic layer code
public boolean deletebook(book b) {
System.out.println("Inside bs.deletebook");
boolean book_result=false;
bookDAO bd = new bookDAOImpl();
book bk = bd.search(b);
System.out.println("===Inside bs.deletebook");
if(bk!=null){
System.out.println(book_result);
bookDAOImpl bdi = new bookDAOImpl();
bd.deleteBook(b);
book_result=true;
}return book_result;
}
@Override
public String searchbook(book b) {
System.out.println("Inside bs.searchbook");
String book_result="";
bookDAO bd = new bookDAOImpl();
String bk = bd.searchBookName(b);
if(bk!=null){
book_result=bk;
}System.out.println("**"+book_result);
return book_result;
}
here my DAO code
@Override
public void deleteBook(book b) {
Connection conn=null;
PreparedStatement ptmt= null;
boolean result=false;
try{
conn= getConnection();
String queryString = "DELETE FROM book WHERE b_id=?";
ptmt = conn.prepareStatement(queryString);
ptmt.setString(1, b.getB_id());
System.out.println(b.getB_id());
ptmt.execute();
System.out.println(ptmt);
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
ptmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
@Override
public String searchBookName(book b) {
System.out.println("Inside book search");
String b_result = null;
Connection conn= null;
PreparedStatement ptmt= null;
ResultSet rset = null;
try{
conn= getConnection();
String queryString = "SELECT * FROM book WHERE b_id=?";
ptmt = conn.prepareStatement(queryString);
ptmt.setString(1, b.getB_id());
System.out.println(b.getB_id());
rset = ptmt.executeQuery();
System.out.println(rset.first());
if (rset.first()){
System.out.println(rset.first());
b_result = rset.getString(String.valueOf("b_title"));
System.out.println("-.-.-."+b_result);
}
}catch(SQLException ex){
ex.printStackTrace();
}
finally{try {
ptmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}System.out.println("-.-.-."+b_result);
return b_result;
}
Can anyone answer this? followin text is from my console
Inside book dopost
Inside searchBook
Inside bs.searchbook
Inside book search
Welcome
Connected
2
false
**