Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package ch.bzz.book.data;
  2.  
  3. import ch.bzz.book.service.Config;
  4.  
  5. import javax.naming.InitialContext;
  6. import javax.naming.NamingException;
  7. import javax.sql.DataSource;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12.  
  13. /**
  14.  * data access for book entity
  15.  * <p>
  16.  * M151: BookDB
  17.  *
  18.  * @author Marcel Suter
  19.  * @version 1.0
  20.  * @since 2019-10-13
  21.  */
  22. public class BookDao {
  23.  
  24.     /**
  25.      * TODO
  26.      *
  27.      * @return
  28.      */
  29.     public Integer count() {
  30.         // TODO establish connection
  31.         // TODO count books in database
  32.         Connection connection = null;
  33.         Integer bookCount = null;
  34.         ResultSet resultSet = null;
  35.         PreparedStatement prepStmt = null;          // import java.sql.PreparedStatement
  36.         try{
  37.             InitialContext initialContext = new InitialContext();
  38.             DataSource dataSource = (DataSource) initialContext.lookup(Config.getProperty("jdbcRessource"));
  39.             connection = dataSource.getConnection();
  40.  
  41.             String sqlQuery = "SELECT count(bookUUID) FROM Book";
  42.  
  43.             try {
  44.                 prepStmt = connection.prepareStatement(sqlQuery);
  45.                 resultSet = prepStmt.executeQuery();
  46.                 if (resultSet.next()) {                // resultSet contains rows
  47.                     bookCount = resultSet.getInt(1);   // get the value of the first attribute
  48.                 }
  49.             } catch (SQLException sqlEx) {
  50.                 sqlEx.printStackTrace();
  51.                 throw new RuntimeException();
  52.             } finally { //schliess die connection
  53.                 try {
  54.                     if (resultSet != null)
  55.                         resultSet.close();
  56.                     if (prepStmt != null)
  57.                         prepStmt.close();
  58.                     if (connection != null)
  59.                         connection.close();
  60.                 } catch (SQLException sqlEx) {
  61.                     sqlEx.printStackTrace();
  62.                     throw new RuntimeException();
  63.                 }
  64.             }
  65.  
  66.         } catch (NamingException nameEx) {                                    // import javax.naming.NamingException
  67.             nameEx.printStackTrace();
  68.             throw new RuntimeException();
  69.         } catch (SQLException sqlEx) {                                        // import java.sql.SQLException
  70.             sqlEx.printStackTrace();
  71.             throw new RuntimeException();
  72.         }
  73.  
  74.         return bookCount; // FIXME return the number of books
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement