Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. public Book getBookById(String book_id) {
  2.         //<editor-fold defaultstate="collapsed" desc="GET BOOK BY BOOK-ID">
  3.         Connection con = null;
  4.         PreparedStatement ps = null;
  5.         ResultSet rs = null;
  6.         Book b = null;
  7.  
  8.        
  9.         try {
  10.             con = getConnection();
  11.             String query = ("SELECT * FROM book WHERE book_id=" + book_id + ";");
  12.      
  13.             ps = con.prepareStatement(query);
  14.             rs = ps.executeQuery();
  15.  
  16.             if (rs.next()) {
  17.                 b = new Book(rs.getInt("book_id"), rs.getString("bookTitle"), rs.getString("bookAuthor"), rs.getString("bookDesc"), rs.getInt("publish_year"), rs.getInt("stock"));
  18.             }
  19.         } catch (SQLException e) {
  20.             System.out.println("Exception occured in the getBookById() method: " + e.getMessage());
  21.         } finally {
  22.             try {
  23.                 if (rs != null) {
  24.                     rs.close();
  25.                 }
  26.                 if (ps != null) {
  27.                     ps.close();
  28.                 }
  29.                 if (con != null) {
  30.                     freeConnection(con);
  31.                 }
  32.             } catch (SQLException e) {
  33.                 System.out.println("Exception occured in the finally section of the getBookById() method: " + e.getMessage());
  34.             }
  35.         }
  36.         return b;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement