Advertisement
Guest User

Untitled

a guest
Jun 14th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. public abstract class DAOFactory implements DAOFactotyInterface {
  2.  
  3. Connection connection = null;
  4.  
  5. public Connection getConnection() throws DAOException {
  6. InputStream read = null;
  7. try {
  8. read = this.getClass().getResourceAsStream("db.properties");
  9. Properties properties = new Properties();
  10. properties.load(read);
  11. String dbUrl = properties.getProperty("db.url");
  12. String dbUser = properties.getProperty("db.user");
  13. String dbPassword = properties.getProperty("db.password");
  14. String dbDriver = properties.getProperty("db.driver");
  15. Class.forName(dbDriver);
  16. if (connection == null) {
  17. connection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
  18. }
  19. } catch (IOException e) {
  20. throw new DAOException("Properties file is missing ", e);
  21. } catch (ClassNotFoundException e) {
  22. throw new DAOException("Driver missing ", e);
  23. } catch (SQLException e) {
  24. throw new DAOException("No connection ", e);
  25. } finally {
  26. if (read != null) {
  27. try {
  28. read.close();
  29. } catch (IOException e) {
  30. throw new DAOException("InputStream is not closed.", e);
  31. }
  32. }
  33. }
  34. return connection;
  35. }
  36.  
  37. @Override
  38. public ProfileDAO getProfileDAO() {
  39. return new MySQLProfileDAO();
  40. }
  41.  
  42. @Override
  43. public ImagesDAO getImagesDAO() {
  44. return new MySQLImagesDAO();
  45. }
  46.  
  47. @Override
  48. public RelationshipsDAO getRelationshipsDAO() {
  49. return new MySQLRelationshipsDAO();
  50. }
  51.  
  52. @Override
  53. public ScrobblesDAO getScrobblesDAO() {
  54. return new MySQLScrobblesDAO();
  55. }
  56. }
  57.  
  58.  
  59. public class MySQLImagesDAO implements ImagesDAO {
  60.  
  61. PreparedStatement preparedStatement = null;
  62. Connection connection = null;
  63. DAOFactory daofactory = new DAOFactory();
  64.  
  65. private void getPreparedStatement(String sql) throws DAOException {
  66. connection = daofactory.getConnection();
  67. if (preparedStatement == null) {
  68. try {
  69. preparedStatement = connection.prepareStatement(sql);
  70. } catch (Exception e) {
  71. throw new DAOException("Get preparedStatment failed.", e);
  72. }
  73. }
  74. }
  75.  
  76. /**
  77. * @see ImagesDAO#selectAllImages()
  78. */
  79. public List<Images> selectAllImages() throws DAOException {
  80. List<Images> allImages = new ArrayList<>();
  81. Statement st = null;
  82. ResultSet rs = null;
  83. try {
  84. connection = daofactory.getConnection();
  85. st = connection.createStatement();
  86. rs = st.executeQuery(PrepStatName.SELECT_IMAGES);
  87. while (rs.next()) {
  88. Images image = new Images();
  89. image.setId(rs.getInt("id"));
  90. image.setIdProfile(rs.getInt("id_profiles"));
  91. image.setAvatar(rs.getString("avatar"));
  92. allImages.add(image);
  93. }
  94. } catch (Exception e) {
  95. throw new DAOException("List of images is not selected.", e);
  96. } finally {
  97. try {
  98. if (st != null) {
  99. st.close();
  100. }
  101. if (rs != null) {
  102. rs.close();
  103. }
  104. } catch (Exception e) {
  105. throw new DAOException("The ResultSet/Statement is not closed.", e);
  106. }
  107. }
  108. return allImages;
  109. }
  110.  
  111. /**
  112. * @see ImagesDAO#updateImages(Images)
  113. */
  114. public void updateImages(Images images) throws DAOException {
  115. try {
  116. getPreparedStatement(PrepStatName.UPDATE_IMAGES);
  117. preparedStatement.setInt(1, images.getIdProfile());
  118. preparedStatement.setString(2, images.getAvatar());
  119. preparedStatement.setInt(3, images.getId());
  120. preparedStatement.executeUpdate();
  121. } catch (Exception e) {
  122. throw new DAOException("The images is not updated.", e);
  123. }
  124. }
  125.  
  126. /**
  127. * @see ImagesDAO#deleteImages(Images)
  128. */
  129. public void deleteImages(Images images) throws DAOException {
  130. try {
  131. getPreparedStatement(PrepStatName.DEL_IMAGES);
  132. preparedStatement.setInt(1, images.getId());
  133. preparedStatement.executeUpdate();
  134. } catch (Exception e) {
  135. throw new DAOException("The images is not delete.", e);
  136. }
  137. }
  138.  
  139. /**
  140. * @see ImagesDAO#insertImages(Images)
  141. */
  142. public void insertImages(Images images) throws DAOException {
  143. try {
  144. getPreparedStatement(PrepStatName.INSERT_IMAGES);
  145. preparedStatement.setInt(1, images.getIdProfile());
  146. preparedStatement.setString(2, images.getAvatar());
  147. preparedStatement.executeUpdate();
  148. } catch (Exception e) {
  149. throw new DAOException("The images is not create.", e);
  150. }
  151. }
  152.  
  153. public void close() throws DAOException {
  154. try {
  155. if (preparedStatement != null) {
  156. preparedStatement.close();
  157. preparedStatement = null;
  158. }
  159. } catch (Exception e) {
  160. throw new DAOException("The preparedStatement is not closed. ", e);
  161. }
  162. try {
  163. if (connection != null) {
  164. connection.close();
  165. connection = null;
  166. }
  167. } catch (Exception e) {
  168. throw new DAOException("The connection is not closed.", e);
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement