Guest User

Untitled

a guest
Sep 20th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. ManagerBook.java (which is the DAO)
  2.  
  3. public Book findBook(int isbn) throws SQLException, ClassNotFoundException{
  4.     Class.forName("com.mysql.jdbc.Driver");
  5.     Connection con = DriverManager.getConnection(url, user, password);
  6.    
  7.     String find = "SELECT * from book WHERE isbn = ?";
  8.     PreparedStatement stt = con.prepareStatement(find);
  9.     stt.setInt(1, isbn);
  10.     ResultSet rs = stt.executeQuery();
  11.    
  12.     Book b1 = new Book();
  13.      
  14.         if (rs.next()) {
  15.         int isbn1 = rs.getInt("isbn");
  16.         String title = rs.getString("title");
  17.        
  18.         b1.setIsbn(isbn1);
  19.         b1.setTitle(title);
  20.        
  21.      }
  22.      con.close();
  23.      stt.close();
  24.      rs.close();
  25.          return b1;
  26.  }
  27.  
  28.  
  29. find.jsp
  30.  
  31. <jsp:useBean id = "bm" class="book.ManagerBook" scope = "session"/>
  32.        
  33.     <h1> Welcome to ABC Library</h1>
  34.        
  35.     <form>
  36.         <table>
  37.             <tr>
  38.                 <td> Enter Details </td>
  39.                 <td><input type="text" name="isbn"></td>
  40.                 <td><input type="submit" name="find" value="find"></td>
  41.             </tr>
  42.         </table>
  43.             <input type="hidden" name="submitted" value="true">
  44.         </form>
  45.        
  46.        
  47.         <% 
  48.             Boolean submitted = Boolean.parseBoolean(request.getParameter("submitted"));
  49.             if(submitted){
  50.             int isbn = Integer.parseInt(request.getParameter("isbn")); 
  51.            
  52.             Book b2 = bm.findBook(isbn);       
  53.            
  54.             %>
  55.             <table>
  56.             <tr>
  57.             <td colspan=2>
  58.             <h2>Book Found</h2>
  59.             </td>
  60.             </tr>
  61.            
  62.             <tr>
  63.                 <td><h3>ISBN</h3></td>
  64.                 <td><h3>Title</h3></td>
  65.             </tr>  
  66.            
  67.             <tr>
  68.                 <td><%= b2.getIsbn()%></td>
  69.                 <td><%= b2.getTitle() %></td>
  70.             </tr>
  71.             </table>
  72.                
  73.             <%}else if(!submitted){ %>
  74.             <h3> Book Not Found</h3>
  75.             <% } %>
Advertisement
Add Comment
Please, Sign In to add comment