Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
HTML
<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>      
                <!--<input type="hidden" name="operation" value="searchBookbtn">--><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="hidden" name="operation" value="deleteBookbtn" >-->	
                <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

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


Java
private void searchBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        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 {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        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

Java
public boolean deletebook(book b) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        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) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        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

Java
@Override
    public void deleteBook(book b) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        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) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        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"));
                /*b_result = new book();
                b_result.setB_title(rset.getString(String.valueOf("b_title")));
                b_result.setB_suplier(rset.getString(String.valueOf("b_supplier")));
                b_result.setB_publisher(rset.getString(String.valueOf("b_publisher")));*/
                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
**
Posted
Updated 4-Sep-13 1:09am
v3

1 solution

JavaScript
function fire(action){

document.formname.action = action;
document.formName.submit();
}


HTML
<input type="button" value="Search Book" onclick="fire('searchAction')" />
<input type="button" value="Delete Book" onclick="fire('deleteAction')" />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900